当前位置: 首页 > 知识库问答 >
问题:

Datastax cassandra QueryBuilder插入错误(java)

鄂育
2023-03-14

环境:

>

  • java 7

    Cassandra 2.1.2在Windows 8.1的一个简单节点上运行在我的本地开发工作站上

    驱动核心

    运行环境: apache karaf 2.3.8

    我试图通过在一个由简单类型(坐标为x和y)定义的列中提供一个json来插入一行。

    我构建这个java语句:

    Statement statement = QueryBuilder
    .insertInto("myKeySpace", "myTable")
    .value("myKeyColumn", "myKeyValue")
    .value("coordinates", "{\"x\":10.4,\"y\":20.3}");
    

    在执行此操作时:

    mySession.execute(statement);
    

    我有以下错误(完整堆栈跟踪在此消息末尾):

    com.datastax.driver.core.exceptions.InvalidQueryException: Not enough bytes to read 0th field java.nio.HeapByteBuffer[pos=0 lim=1 cap=1]
    

    [在下面的CQL语句中,我对列名进行了匿名化,可能引号是错误的,但问题出在上面的java语句中]

    我的cassandra表和类型:

    CREATE TABLE IF NOT EXISTS "myTable" ( 
        "myKeyColumn" text,
        coordinates FROZEN<coordinates>,
        PRIMARY KEY ("myKeyColumn") 
    );
    
    CREATE TYPE IF NOT EXISTS coordinates (
          x double,
          y double
    );
    

    在Datastax DevCenter中执行以下查询时,效果良好:

    INSERT INTO "myTable"("myKeyColumn","coordinates")
    VALUES ('myKeyValue',{"x":10.4,"y":20.3});
    

    欢迎任何帮助!:-)

    完整堆栈跟踪:

    2014-11-27 11:38:07,532 | WARN  | tp1271566160-230 | ServletHandler                   | pse.jetty.servlet.ServletHandler  563 | 135 - org.eclipse.jetty.aggregate.jetty-all-server - 8.1.15.v20140411 | /cxf/rest/myProject/add
    java.lang.RuntimeException: org.apache.cxf.interceptor.Fault: com.datastax.driver.core.exceptions.InvalidQueryException: Not enough bytes to read 0th field java.nio.HeapByteBuffer[pos=0 lim=1 cap=1]
        at org.apache.cxf.interceptor.AbstractFaultChainInitiatorObserver.onMessage(AbstractFaultChainInitiatorObserver.java:116)[164:org.apache.cxf.cxf-api:2.7.12]
        at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:336)[164:org.apache.cxf.cxf-api:2.7.12]
        at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)[164:org.apache.cxf.cxf-api:2.7.12]
        at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:241)[171:org.apache.cxf.cxf-rt-transports-http:2.7.12]
        at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:248)[171:org.apache.cxf.cxf-rt-transports-http:2.7.12]
        at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:222)[171:org.apache.cxf.cxf-rt-transports-http:2.7.12]
        at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:153)[171:org.apache.cxf.cxf-rt-transports-http:2.7.12]
        at org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:171)[171:org.apache.cxf.cxf-rt-transports-http:2.7.12]
        at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:286)[171:org.apache.cxf.cxf-rt-transports-http:2.7.12]
        at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doPost(AbstractHTTPServlet.java:206)[171:org.apache.cxf.cxf-rt-transports-http:2.7.12]
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:595)[83:org.apache.geronimo.specs.geronimo-servlet_3.0_spec:1.0]
        at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:262)[171:org.apache.cxf.cxf-rt-transports-http:2.7.12]
        at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:684)[135:org.eclipse.jetty.aggregate.jetty-all-server:8.1.15.v20140411]
        at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:503)[135:org.eclipse.jetty.aggregate.jetty-all-server:8.1.15.v20140411]
        at org.ops4j.pax.web.service.jetty.internal.HttpServiceServletHandler.doHandle(HttpServiceServletHandler.java:69)[145:org.ops4j.pax.web.pax-web-jetty:3.1.1]
        at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:137)[135:org.eclipse.jetty.aggregate.jetty-all-server:8.1.15.v20140411]
        at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:557)[135:org.eclipse.jetty.aggregate.jetty-all-server:8.1.15.v20140411]
        at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:231)[135:org.eclipse.jetty.aggregate.jetty-all-server:8.1.15.v20140411]
        at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1086)[135:org.eclipse.jetty.aggregate.jetty-all-server:8.1.15.v20140411]
        at org.ops4j.pax.web.service.jetty.internal.HttpServiceContext.doHandle(HttpServiceContext.java:240)[145:org.ops4j.pax.web.pax-web-jetty:3.1.1]
        at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:429)[135:org.eclipse.jetty.aggregate.jetty-all-server:8.1.15.v20140411]
        at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:193)[135:org.eclipse.jetty.aggregate.jetty-all-server:8.1.15.v20140411]
        at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1020)[135:org.eclipse.jetty.aggregate.jetty-all-server:8.1.15.v20140411]
        at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135)[135:org.eclipse.jetty.aggregate.jetty-all-server:8.1.15.v20140411]
        at org.ops4j.pax.web.service.jetty.internal.JettyServerHandlerCollection.handle(JettyServerHandlerCollection.java:77)[145:org.ops4j.pax.web.pax-web-jetty:3.1.1]
        at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)[135:org.eclipse.jetty.aggregate.jetty-all-server:8.1.15.v20140411]
        at org.eclipse.jetty.server.Server.handle(Server.java:370)[135:org.eclipse.jetty.aggregate.jetty-all-server:8.1.15.v20140411]
        at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:494)[135:org.eclipse.jetty.aggregate.jetty-all-server:8.1.15.v20140411]
        at org.eclipse.jetty.server.AbstractHttpConnection.content(AbstractHttpConnection.java:982)[135:org.eclipse.jetty.aggregate.jetty-all-server:8.1.15.v20140411]
        at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.content(AbstractHttpConnection.java:1043)[135:org.eclipse.jetty.aggregate.jetty-all-server:8.1.15.v20140411]
        at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:865)[135:org.eclipse.jetty.aggregate.jetty-all-server:8.1.15.v20140411]
        at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:240)[135:org.eclipse.jetty.aggregate.jetty-all-server:8.1.15.v20140411]
        at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)[135:org.eclipse.jetty.aggregate.jetty-all-server:8.1.15.v20140411]
        at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:696)[135:org.eclipse.jetty.aggregate.jetty-all-server:8.1.15.v20140411]
        at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:53)[135:org.eclipse.jetty.aggregate.jetty-all-server:8.1.15.v20140411]
        at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)[135:org.eclipse.jetty.aggregate.jetty-all-server:8.1.15.v20140411]
        at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)[135:org.eclipse.jetty.aggregate.jetty-all-server:8.1.15.v20140411]
        at java.lang.Thread.run(Thread.java:724)[:1.7.0_25]
    Caused by: org.apache.cxf.interceptor.Fault: com.datastax.driver.core.exceptions.InvalidQueryException: Not enough bytes to read 0th field java.nio.HeapByteBuffer[pos=0 lim=1 cap=1]
        at org.apache.cxf.service.invoker.AbstractInvoker.createFault(AbstractInvoker.java:170)[164:org.apache.cxf.cxf-api:2.7.12]
        at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:136)[164:org.apache.cxf.cxf-api:2.7.12]
        at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:204)[177:org.apache.cxf.cxf-rt-frontend-jaxrs:2.7.12]
        at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:101)[177:org.apache.cxf.cxf-rt-frontend-jaxrs:2.7.12]
        at org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:58)[164:org.apache.cxf.cxf-api:2.7.12]
        at org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:94)[164:org.apache.cxf.cxf-api:2.7.12]
        at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:272)[164:org.apache.cxf.cxf-api:2.7.12]
        ... 36 more
    Caused by: org.mycompany.service.container.InvocationException: com.datastax.driver.core.exceptions.InvalidQueryException: Not enough bytes to read 0th field java.nio.HeapByteBuffer[pos=0 lim=1 cap=1]
        at org.mycompany.service.container.interceptors.ServiceInterceptor.invoke(ServiceInterceptor.java:73)
        at org.mycompany.service.container.InvocationChain.invokeNext(InvocationChain.java:82)
        at org.mycompany.service.container.interceptors.SecurityInterceptor.invoke(SecurityInterceptor.java:84)
        at org.mycompany.service.container.InvocationChain.invokeNext(InvocationChain.java:82)
        at org.mycompany.service.container.ServiceInvocationHandler.invoke(ServiceInvocationHandler.java:66)
        at com.sun.proxy.$Proxy24.add(Unknown Source)
        at sun.reflect.GeneratedMethodAccessor74.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)[:1.7.0_25]
        at java.lang.reflect.Method.invoke(Method.java:606)[:1.7.0_25]
        at org.apache.cxf.service.invoker.AbstractInvoker.performInvocation(AbstractInvoker.java:188)[164:org.apache.cxf.cxf-api:2.7.12]
        at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:104)[164:org.apache.cxf.cxf-api:2.7.12]
        ... 41 more
    Caused by: com.datastax.driver.core.exceptions.InvalidQueryException: Not enough bytes to read 0th field java.nio.HeapByteBuffer[pos=0 lim=1 cap=1]
        at com.datastax.driver.core.exceptions.InvalidQueryException.copy(InvalidQueryException.java:35)
        at com.datastax.driver.core.DefaultResultSetFuture.extractCauseFromExecutionException(DefaultResultSetFuture.java:258)
        at com.datastax.driver.core.DefaultResultSetFuture.getUninterruptibly(DefaultResultSetFuture.java:174)
        at com.datastax.driver.core.AbstractSession.execute(AbstractSession.java:52)
        at mypackage.data.internal.mydao.add(Mydao.java:140)
        ... 51 more
    Caused by: com.datastax.driver.core.exceptions.InvalidQueryException: Not enough bytes to read 0th field java.nio.HeapByteBuffer[pos=0 lim=1 cap=1]
        at com.datastax.driver.core.Responses$Error.asException(Responses.java:97)[120:com.datastax.driver.core:2.1.0]
        at com.datastax.driver.core.DefaultResultSetFuture.onSet(DefaultResultSetFuture.java:110)[120:com.datastax.driver.core:2.1.0]
        at com.datastax.driver.core.RequestHandler.setFinalResult(RequestHandler.java:235)[120:com.datastax.driver.core:2.1.0]
        at com.datastax.driver.core.RequestHandler.onSet(RequestHandler.java:367)[120:com.datastax.driver.core:2.1.0]
        at com.datastax.driver.core.Connection$Dispatcher.messageReceived(Connection.java:584)[120:com.datastax.driver.core:2.1.0]
        at org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:70)[118:org.jboss.netty:3.9.3.Final]
        at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)[118:org.jboss.netty:3.9.3.Final]
        at org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:791)[118:org.jboss.netty:3.9.3.Final]
        at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:296)[118:org.jboss.netty:3.9.3.Final]
        at org.jboss.netty.handler.codec.oneone.OneToOneDecoder.handleUpstream(OneToOneDecoder.java:70)[118:org.jboss.netty:3.9.3.Final]
        at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)[118:org.jboss.netty:3.9.3.Final]
        at org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:791)[118:org.jboss.netty:3.9.3.Final]
        at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:296)[118:org.jboss.netty:3.9.3.Final]
        at org.jboss.netty.handler.codec.oneone.OneToOneDecoder.handleUpstream(OneToOneDecoder.java:70)[118:org.jboss.netty:3.9.3.Final]
        at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)[118:org.jboss.netty:3.9.3.Final]
        at org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:791)[118:org.jboss.netty:3.9.3.Final]
        at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:296)[118:org.jboss.netty:3.9.3.Final]
        at org.jboss.netty.handler.codec.frame.FrameDecoder.unfoldAndFireMessageReceived(FrameDecoder.java:462)[118:org.jboss.netty:3.9.3.Final]
        at org.jboss.netty.handler.codec.frame.FrameDecoder.callDecode(FrameDecoder.java:443)[118:org.jboss.netty:3.9.3.Final]
        at org.jboss.netty.handler.codec.frame.FrameDecoder.messageReceived(FrameDecoder.java:303)[118:org.jboss.netty:3.9.3.Final]
        at org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:70)[118:org.jboss.netty:3.9.3.Final]
        at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)[118:org.jboss.netty:3.9.3.Final]
        at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:559)[118:org.jboss.netty:3.9.3.Final]
        at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:268)[118:org.jboss.netty:3.9.3.Final]
        at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:255)[118:org.jboss.netty:3.9.3.Final]
        at org.jboss.netty.channel.socket.nio.NioWorker.read(NioWorker.java:88)[118:org.jboss.netty:3.9.3.Final]
        at org.jboss.netty.channel.socket.nio.AbstractNioWorker.process(AbstractNioWorker.java:108)[118:org.jboss.netty:3.9.3.Final]
        at org.jboss.netty.channel.socket.nio.AbstractNioSelector.run(AbstractNioSelector.java:318)[118:org.jboss.netty:3.9.3.Final]
        at org.jboss.netty.channel.socket.nio.AbstractNioWorker.run(AbstractNioWorker.java:89)[118:org.jboss.netty:3.9.3.Final]
        at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:178)[118:org.jboss.netty:3.9.3.Final]
        at org.jboss.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:108)[118:org.jboss.netty:3.9.3.Final]
        at org.jboss.netty.util.internal.DeadLockProofWorker$1.run(DeadLockProofWorker.java:42)[118:org.jboss.netty:3.9.3.Final]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)[:1.7.0_25]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)[:1.7.0_25]
        ... 1 more
    
  • 共有2个答案

    施自怡
    2023-03-14

    我从github下载了cassandra驱动程序parent,并查看了core的测试。我找到了另一种调用这些类型的方法:

    UDTValue udtValue = keyspaceBuilder.getCluster().getMetadata().getKeyspace("myKeyspace").getUserType("coordinates").newValue()
        .setDouble("x", x)
        .setDouble("y", y);
    

    然后,在QueryBuilder中,我添加:

    .value("coordinates", udtValue);
    

    这很有效。

    周鸿运
    2023-03-14

    驱动程序v2中的UDT存在已知问题。1.2这将在v2中解决。1.3. 同时,您能否尝试一下这里给出的解决方法:

    https://datastax-oss.atlassian.net/browse/JAVA-500

    如在:

    Cluster.builder().withProtocolVersion(ProtocolVersion.V3)
    

    看看这是否有帮助

     类似资料:
    • 问题内容: 我正在尝试使用jdbc将一些数据插入到我的数据库中。我正在使用正确的表,数据库名称和参数。一切检查完毕。 代码 这是当我尝试运行它时给我的错误: 问题答案: 您在此行中有语法错误: 您缺少values子句的右括号。

    • 问题内容: SQL查询: MySQL说: 即使在行1062处没有条目,它也会显示此重复的条目错误。(ID是主键,并且是unique(ID_Category,Keyword))。你能帮我吗?… 问题答案: 您的数据库中已经有一行带有值“ 18”和“免费邮件”的行。由于存在唯一性约束,因此不能有两个这样的行。您有一些选择: 删除原始行,然后再次尝试插入:。 删除唯一性约束以允许两行同时存在。 使用忽略

    • 问题内容: 插入数据库时​​出现错误。 代码: 忽略dbquery,与mysql_query完全一样。 我收到的错误是: 不知道为什么会引发此错误! 问题答案: 教一个人如何钓鱼。 如果查询失败,那么您应该做的第一件事就是回显您要发送的查询: 通常,最终查询到底出了什么问题,这是很明显的。要特别注意查询中的动态内容,通常要注意MySQL抱怨的区域。 如果看起来还可以,那么您会寻找可能需要转义的单词

    • 我有一个问题插入查询在红宝石与卡桑德拉, 这是我的表: 在ruby中,我做了一个prepare语句: 我把这些值用于测试: 我得到了这个错误: /home/florian/。rvm/gems/ruby-2 . 1 . 4/gems/Cassandra-driver-1 . 0 . 0 . RC . 1/lib/Cassandra/protocol/cql _ byte _ buffer . Rb:

    • 我在尝试将记录插入数据库时遇到错误。 错误如下: 守则: 数据库: