使用JDBC连接将记录插入Apache Ignite Cluster时出现异常

时间:2017-12-19 04:53:38

标签: apache insert key ignite

我一直在努力使用JDBC连接在Apache Ignite Cache上运行插入查询。我使用以下配置创建了我的缓存:

  

CacheConfiguration cacheCfg = new   CacheConfiguration<>(" SQL_PUBLIC_MYAPPLICATION&#34);   cacheCfg.setCacheMode(CacheMode.REPLICATED);   cacheCfg.setIndexedTypes(Long.class,MyApplication.class);   cacheCfg.setSqlSchema("公众&#34);         cacheCfg.setCacheStoreFactory(FactoryBuilder.factoryOf(CacheMyApplicationStore.class));         cacheCfg.setReadThrough(真);
  cacheCfg.setWriteThrough(真);
        FifoEvictionPolicy逐出=新的   FifoEvictionPolicy();         eviction.setMaxSize(100_000);
   IgniteCache   cache = getIgnite()。getOrCreateCache(cacheCfg);         cache.loadCache(null,100_000);

MyApplication.java

公共类MyApplication {

@QuerySqlField(index = true,orderedGroups = {@QuerySqlField.Group(name = "idx3", order = 0)})
private Long ID;

@QuerySqlField(index = true,orderedGroups = {@QuerySqlField.Group(name = "idx3", order = 1)})
private String NAME;

@QuerySqlField
private String DESCRIPTION;

我能够使用JDBC连接更新或删除缓存中的记录,但插入不起作用。当我尝试使用以下代码插入时,我收到异常:

Connection conn = DriverManager.getConnection("jdbc:ignite:thin://url:4000");

try (PreparedStatement stmt = conn.prepareStatement("insert into MyApplication (ID, NAME, DESCRIPTION) values (1, 'TestApp', 'To be deleted')")) {
                stmt.setLong(1, 1L);
                stmt.setString(2, "TestApp");
                stmt.setString(3, "To be deleted");
                stmt.executeUpdate();
          }
}

异常:

[17:15:14,894][ERROR][sql-connector-#58%agrid%][JdbcRequestHandler] Failed
 to  execute SQL query [reqId=1, req=JdbcQueryExecuteRequest [schemaName=null, pageS ize=1024, maxRows=0, 
sqlQry=insert into MyApplication (ID, NAME, DESCRIPTION) values (?,?,?), args=[1, TestApp, To be deleted]]] class org.apache.ignite.internal.processors.query.IgniteSQLException: Failed to execute DML statement [stmt=insert into MyApplication (ID, NAME, DESCRIPTION) values (?,?,?), params=[1, TestApp, To be deleted]]]
            at org.apache.ignite.internal.processors.query.h2.IgniteH2Indexing.queryDistributedSqlFields(IgniteH2Indexing.java:1324)
            at org.apache.ignite.internal.processors.query.GridQueryProcessor$6.applyx(GridQueryProcessor.java:1856)
            at org.apache.ignite.internal.processors.query.GridQueryProcessor$6.applyx(GridQueryProcessor.java:1852)
            at org.apache.ignite.internal.util.lang.IgniteOutClosureX.apply(IgniteOutClosureX.java:36)
            at org.apache.ignite.internal.processors.query.GridQueryProcessor.executeQuery(GridQueryProcessor.java:2293)
            at org.apache.ignite.internal.processors.query.GridQueryProcessor.querySqlFieldsNoCache(GridQueryProcessor.java:1860)
            at org.apache.ignite.internal.processors.odbc.jdbc.JdbcRequestHandler.executeQuery(JdbcRequestHandler.java:188)
            at org.apache.ignite.internal.processors.odbc.jdbc.JdbcRequestHandler.handle(JdbcRequestHandler.java:122)
            at org.apache.ignite.internal.processors.odbc.SqlListenerNioListener.onMessage(SqlListenerNioListener.java:152)
            at org.apache.ignite.internal.processors.odbc.SqlListenerNioListener.onMessage(SqlListenerNioListener.java:44)
            at org.apache.ignite.internal.util.nio.GridNioFilterChain$TailFilter.onMessageReceived(GridNioFilterChain.java:279)
            at org.apache.ignite.internal.util.nio.GridNioFilterAdapter.proceedMessageReceived(GridNioFilterAdapter.java:109)
            at org.apache.ignite.internal.util.nio.GridNioAsyncNotifyFilter$3.body(GridNioAsyncNotifyFilter.java:97)
            at org.apache.ignite.internal.util.worker.GridWorker.run(GridWorker.java:110)
            at org.apache.ignite.internal.util.worker.GridWorkerPool$1.run(GridWorkerPool.java:70)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
            at java.lang.Thread.run(Unknown Source) Caused by: class org.apache.ignite.IgniteCheckedException: Key is missing from query
            at org.apache.ignite.internal.processors.query.h2.dml.UpdatePlanBuilder.createSupplier(UpdatePlanBuilder.java:331)
            at org.apache.ignite.internal.processors.query.h2.dml.UpdatePlanBuilder.planForInsert(UpdatePlanBuilder.java:196)
            at org.apache.ignite.internal.processors.query.h2.dml.UpdatePlanBuilder.planForStatement(UpdatePlanBuilder.java:82)
            at org.apache.ignite.internal.processors.query.h2.DmlStatementsProcessor.getPlanForStatement(DmlStatementsProcessor.java:438)
            at org.apache.ignite.internal.processors.query.h2.DmlStatementsProcessor.updateSqlFields(DmlStatementsProcessor.java:164)
            at org.apache.ignite.internal.processors.query.h2.DmlStatementsProcessor.updateSqlFieldsDistributed(DmlStatementsProcessor.java:222)
            at org.apache.ignite.internal.processors.query.h2.IgniteH2Indexing.queryDistributedSqlFields(IgniteH2Indexing.java:1321)
            ... 17 more

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

只需使用CREATE TABLECREATE INDEX命令配置缓存和SQL索引,尤其是,如果您计划主要使用SQL操作进行Ignite访问。

如果仍然需要键值,计算或机器学习API,那么请查看this project,其中展示了在使用SQL DDL命令定义高速缓存时如何执行此操作。

答案 1 :(得分:0)

根本原因如下:

Caused by: class org.apache.ignite.IgniteCheckedException: Key is missing from query

您在INSERT语句中提供的所有值都属于value对象,因此Ignite无法为该条目创建密钥对象并抛出此异常。

要解决此问题,您应该为关键对象创建一个类(例如MyApplicationKey)并在那里移动ID字段。