2013-05-28 21 views
0

我正在使用Cassandra,並且我正在使用Hector客戶端來讀取和插入Cassandra數據庫中的數據。我試圖使用hector客戶端從Cassandra數據庫中檢索數據,如果我只想檢索一列,我就可以做到這一點。使用Hector客戶端從Cassandra檢索多列值

現在我試圖檢索rowKey as 1011的數據,但將columnNames作爲字符串的集合。下面是我的API,將檢索使用赫克託卡桑德拉數據庫中的數據客戶端 -

public Map<String, String> getAttributes(String rowKey, Collection<String> attributeNames, String columnFamily) { 

    final Cluster cluster = CassandraHectorConnection.getInstance().getCluster();  
    final Keyspace keyspace = CassandraHectorConnection.getInstance().getKeyspace(); 

    try { 

     ColumnQuery<String, String, String> columnQuery = HFactory 
       .createStringColumnQuery(keyspace) 
       .setColumnFamily(columnFamily).setKey(rowKey) 
       .setName("c1"); 

     QueryResult<HColumn<String, String>> result = columnQuery.execute(); 
     System.out.println("Column Name from cassandra: " + result.get().getName() + "Column value from cassandra: " + result.get().getValue()); 

    } catch (HectorException e) { 
     LOG.error("Exception in CassandraHectorClient::getAttributes " +e+ ", RowKey = " +rowKey+ ", Attribute Names = " +attributeNames); 
    } finally { 
     cluster.getConnectionManager().shutdown(); 
    } 


    return null; 

} 

如果你看到我上面的方法,我正在嘗試從卡桑德拉數據庫中的數據進行特定的rowKeycolumn c1。現在我正在嘗試從Cassandra數據庫中檢索數據以收集特定rowKey的列。

含義像這個 -

我想檢索多列,但對同一rowKey數據。我如何使用Hector客戶端來做到這一點?我不想檢索所有列的數據,然後重複查找我正在查找的各個列數據。

回答

2

使用的列名由複合鍵UTF8Type和TIMEUUID 的組合再經過

sliceQuery.setKey("your row key"); 

Composite startRange = new Composite(); 
startRange.addComponent(0, "c1",Composite.ComponentEquality.EQUAL); 

Composite endRange = new Composite(); 
endRange.addComponent(0, "c1",Composite.ComponentEquality.GREATER_THAN_EQUAL); 

sliceQuery.setRange(startRange,endRange, false, Integer.MAX_VALUE); 

QueryResult<ColumnSlice<Composite, String>> result = sliceQuery.execute(); 
ColumnSlice<Composite, String> cs = result.get(); 

上面的代碼將會給你的所有記錄你的那迭代後行鍵 如下

for (HColumn<Composite, String> col : cs.getColumns()) { 
System.out.println("column key's first part : "+col.getName().get(0, HFactoryHelper.stringSerializer).toString()); 
System.out.println("column key's second part : "+col.getName().get(1, HFactoryHelper.uuidSerializer).toString()); 
System.out.println("column key's value : "+col.getValue()); 
} 

某些地方你必須寫邏輯來維護一套記錄

相關問題