2013-02-22 9 views
5

Sto usando questo codice per il recupero user_id & user_codecolonna a caso prenderti in Cassandra

Keyspace keyspace = HFactory.createKeyspace("test", cluster); 
CqlQuery<String,String,ByteBuffer> cqlQuery = new CqlQuery<String,String,ByteBuffer>(keyspace, stringSerializer, stringSerializer, new ByteBufferSerializer()); 
cqlQuery.setQuery("select user_id,user_code from User"); 
QueryResult<CqlRows<String,String,ByteBuffer>> result = cqlQuery.execute(); 
Iterator iterator = result.get().iterator(); 
while(iterator.hasNext()) { 
    Row<String, String, ByteBuffer> row = (Row<String, String, ByteBuffer>) iterator.next(); 
    System.out.println("\nInserted data is as follows:\n" + row.getColumnSlice().getColumns().get(0).getValue().getInt()); 
    System.out.println("\nInserted data is as follows:\n" + Charset.forName("UTF-8").decode(row.getColumnSlice().getColumns().get(1).getValueBytes())); 
} 

Ora problema è qui che io sono la conversione dei campi in base al loro tipo specifico

E se interrogazione va casuale? Come gestire questo scenario?

+0

Plz tenta di utilizzare diverso BufferSerialezer invece di ByteBufferSerializer, tramite questo otterrà sempre tutti i dati in un flusso di byte, provare ad estrarre i dati dal database nel formato reale in cui è memorizzato. –

risposta

1

Le query CQL vengono restituite con metadati relativi alle colonne che contengono, analogamente a un set di risultati JDBC.

Non so se o come Hector esponga queste informazioni. Per CQL, una scelta migliore sarebbe il nuovo driver CQL puro qui: https://github.com/datastax/java-driver

+0

Come hai fatto a farlo funzionare? Dai un'occhiata a [mio gist] (https://gist.github.com/shehaaz/5279566) ... È una sessione di Cluster Cassandra ... appena chiamata nuova Cassandra(); Fallisce ... deve farlo con la porta? perché non posso impostarlo su localhost: 9160 .... In ProtocolOpzioni il DEFAULT_PORT è impostato su 9042 ... perché è così? Grazie! :) – Shehaaz