2013-06-25 47 views
-1

我正在嘗試使用astyana寫入操作,並且正在使用cqlsh。 如果我只是使用查詢,我能夠得到的結果在cassandra(astyanax)中使用cql準備語句時出錯

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) 

我認爲它錯了,當我嘗試時間戳存儲到表中。但是我沒有在準備好的語句中找到任何存儲時間戳的東西。數據庫中字段的數據類型是「timestamp」。

+1

不要只是轉儲代碼,給我們一些上下文。你的代碼示例有一些註釋,爲什麼?在尋求我們的幫助之前做出努力。 – Theo

+0

@Theo糾正問題 –

回答

1

我應該用

.withByteBufferValue(新的Date(),DateSerializer.get())。

或者如果您有自定義對象序列化,請擴展AbstractSerializer類。

0

既然你得到的是一個NullPointerException你檢查過你的一些值是否爲空?