提问人:Justin Mathew 提问时间:9/8/2022 更新时间:9/8/2022 访问量:132
无法解析 .NET 类“System.DateTime” - apache ignite 与 .Net
Failed to resolve .NET class 'System.DateTime' - apache ignite with .Net
问:
Apache ignite 在执行 Datetime 列的 SqlFieldsQuery 时抛出 System.DateTime 异常,但同时 ignite 成功保存了数据。 实体是通过配置文件创建的,如下所示
<bean class="org.apache.ignite.configuration.CacheConfiguration">
<property name="name" value="TEST"/>
<property name="cacheMode" value="PARTITIONED"/>
<property name="atomicityMode" value="TRANSACTIONAL"/>
<property name="writeSynchronizationMode" value="FULL_SYNC"/>
<!-- Configure type metadata to enable queries. -->
<property name="queryEntities">
<list>
<bean class="org.apache.ignite.cache.QueryEntity">
<property name="keyType" value="java.lang.Long"/>
<property name="valueType" value="TEST"/>
<property name="fields">
<map>
<entry key="ID" value="java.lang.Long"/>
<entry key="DATE" value="java.sql.Timestamp"/>
</map>
</property>
</bean>
</list>
</property>
</bean>
这是 C# 代码,用于上传值和读取...
string cacheName = "TEST";
try
{
using (IIgnite ignite = Ignition.Start("RemoteDevServer.config"))
{
//ignite.GetBinary().GetBinaryType(cacheName);
var cache = ignite.GetCache<long, IBinaryObject>(cacheName).WithKeepBinary<long, IBinaryObject>();
var objectBuilder = ignite.GetBinary().GetBuilder(cacheName);
long Id = 100;
objectBuilder.SetField("ID", Id);
objectBuilder.SetField("DATE", DateTime.Now);
cache[Id] = objectBuilder.Build();
var sqlp = new SqlFieldsQuery("select ID, DATE from TEST ");
using (var cursor = cache.Query(sqlp))
{
foreach (var row in cursor) // throws exception
{
Console.WriteLine(row[0] + "---" + row[1]);
}
}
}
}
catch (Exception ex)
{
throw ex;
}
以下是错误详细信息
Failed to execute map query on remote node [nodeId=fa1cb87b-7a6a-46f0-aa21-0e1f680f241e, errMsg=General error: "class org.apache.ignite.binary.BinaryInvalidTypeException: Failed to resolve .NET class 'System.DateTime' in Java [platformId=0, typeId=-1854586790]."; SQL statement:
SELECT
"__Z0"."ID" AS "__C0_0",
"__Z0"."DATE" AS "__C0_1"
FROM "TEST"."TEST" AS "__Z0" [50000-199]]
我尝试了此处推荐的解决方案 - Apache Ignite Linq over Sql 无法解析 java 类,但没有解决问题。根据此处的链接 - https://www.gridgain.com/docs/latest/sql-reference/data-types java.sql.Timestamp 应该与 System.DateTime 匹配,但不幸的是不匹配。 任何帮助将不胜感激。
答:
0赞
Pavel Tupitsyn
9/8/2022
#1
替换为 .objectBuilder.SetField("DATE", DateTime.Now);
objectBuilder.SetTimestampField("DATE", DateTime.UtcNow);
- SQL 中仅支持 UTC 值。
- 使用时,请改为调用。
IBinaryObjectBuilder
SetTimestampField
SetField
我还建议启用模式以尽早发现这些问题:ForceTimestamp
var igniteCfg = new IgniteConfiguration
{
BinaryConfiguration = new BinaryConfiguration
{
ForceTimestamp = true
}
}
评论
0赞
Justin Mathew
9/8/2022
绝对完美。非常感谢@Pavel。是否可以在配置中放入 ForceTimestamp = true?我尝试了以下方法,但没有成功。<property name=“binaryConfiguration”> <bean class=“org.apache.ignite.configuration.BinaryConfiguration”> <property name=“ForceTimestamp” value=“true”/> </bean> </property>
0赞
Pavel Tupitsyn
9/8/2022
@JustinMathew这是一个 .NET 配置属性,应在 C# 代码或RemoteDevServer.config
评论