在cassandra(astyanax)中使用cql准备语句时出错

时间:2013-06-25 11:39:23

标签: cassandra astyanax

我正在尝试使用astyana写操作,并且正在使用cqlsh。 如果我只是使用查询,我就能得到结果

keyspace.prepareQuery(COLUMN_FAMILY).withCql("insert into scan_request (customer_id, uuid, scan_type, status, content_size, request_time, request_content_hash) values ("+dao.getCustomerId()+","+"'"+dao.getUuId()+"',"+dao.getScanType()+","+dao.getStatus()+","+dao.getContentSize()+",'2012-12-12 12:12:12', '"+dao.getRequestContentHash()+"');")
            .execute();

但是如果我使用预处理语句来做同样的事情,我会得到以下错误。

        this.getKeyspace()
        .prepareQuery(COLUMN_FAMILY)
            .withCql(INSERT_STATEMENT)
        .asPreparedStatement()
            .withIntegerValue(dao.getCustomerId())
            .withStringValue(dao.getUuId())
            .withIntegerValue(dao.getScanType())
            .withIntegerValue(dao.getStatus())
            .withIntegerValue(dao.getContentSize())
            .withStringValue("'2012-12-12 12:12:12'")
            .withStringValue(dao.getRequestContentHash())
        .execute();

我收到以下错误

Exception in thread "main" java.lang.RuntimeException: failed to write data to C*
    at com.tools.dbaccess.cassandra.astyanax.AstyanaxClient.write(AstyanaxClient.java:155)
    at com.tools.dbaccess.cassandra.astyanax.AstyanaxClient.main(AstyanaxClient.java:164)
Caused by: com.netflix.astyanax.connectionpool.exceptions.BadRequestException: BadRequestException: [host=localhost(127.0.0.1):9160, latency=11(11), attempts=1]InvalidRequestException(why:Expected 8 or 0 byte long for date (21))
    at com.netflix.astyanax.thrift.ThriftConverter.ToConnectionPoolException(ThriftConverter.java:159)
    at com.netflix.astyanax.thrift.AbstractOperationImpl.execute(AbstractOperationImpl.java:65)
    at com.netflix.astyanax.thrift.AbstractOperationImpl.execute(AbstractOperationImpl.java:28)
    at com.netflix.astyanax.thrift.ThriftSyncConnectionFactoryImpl$ThriftConnection.execute(ThriftSyncConnectionFactoryImpl.java:151)
    at com.netflix.astyanax.connectionpool.impl.AbstractExecuteWithFailoverImpl.tryOperation(AbstractExecuteWithFailoverImpl.java:69)
    at com.netflix.astyanax.connectionpool.impl.AbstractHostPartitionConnectionPool.executeWithFailover(AbstractHostPartitionConnectionPool.java:256)
    at com.netflix.astyanax.thrift.AbstractThriftCqlQuery$3.execute(AbstractThriftCqlQuery.java:80)
    at com.tools.dbaccess.cassandra.astyanax.AstyanaxClient.write(AstyanaxClient.java:144)
    ... 1 more
Caused by: InvalidRequestException(why:Expected 8 or 0 byte long for date (21))
    at org.apache.cassandra.thrift.Cassandra$execute_prepared_cql3_query_result.read(Cassandra.java:41868)
    at org.apache.thrift.TServiceClient.receiveBase(TServiceClient.java:78)
    at org.apache.cassandra.thrift.Cassandra$Client.recv_execute_prepared_cql3_query(Cassandra.java:1689)
    at org.apache.cassandra.thrift.Cassandra$Client.execute_prepared_cql3_query(Cassandra.java:1674)
    at com.netflix.astyanax.thrift.ThriftCql3Query.execute_prepared_cql_query(ThriftCql3Query.java:29)
    at com.netflix.astyanax.thrift.AbstractThriftCqlQuery$3$1.internalExecute(AbstractThriftCqlQuery.java:92)
    at com.netflix.astyanax.thrift.AbstractThriftCqlQuery$3$1.internalExecute(AbstractThriftCqlQuery.java:82)
    at com.netflix.astyanax.thrift.AbstractOperationImpl.execute(AbstractOperationImpl.java:60)

当我尝试将时间戳存储到表中时,我认为它是错误的。但我没有在preparedStatement中找到任何存储时间戳的东西。数据库中字段的数据类型是“timestamp”。

2 个答案:

答案 0 :(得分:1)

我应该用

.withByteBufferValue(new Date(),DateSerializer.get())。

或者,如果您有要序列化的自定义对象,请扩展AbstractSerializer类。

答案 1 :(得分:0)

因为你得到的是NullPointerException你有没有检查过你的某些值不是空的?