Retrieve multiple columns value from Cassandra using Hector client

对着背影说爱祢 提交于 2020-01-25 08:57:25

问题


I am working with Cassandra and I am using Hector client to read and upsert the data in Cassandra database. I am trying to retrieve the data from Cassandra database using hector client and I am able to do that if I am trying to retrieve only one column.

Now I am trying to retrieve the data for rowKey as 1011 but with columnNames as collection of string. Below is my API that will retrieve the data from Cassandra database using Hector client-

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;

}

If you see my above method, I am trying to retrieve the data from Cassandra database for a particular rowKey and for column c1. Now I am trying to retrieve the data from Cassandra database for collection of columns for a particular rowKey.

Meaning something like this-

I want to retrieve the data for multiple columns but for the same rowKey. How can I do this using Hector client? And I don't want to retrieve the data for all the columns and then iterate to find out the individual columns data I am looking for.


回答1:


Use column name made up with composite key as combination of UTF8Type and TIMEUUID then after

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();

above code will give you all records for you row key after that iterate as follows

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());
}

some where you have to write logic to maintain set of records



来源:https://stackoverflow.com/questions/16800536/retrieve-multiple-columns-value-from-cassandra-using-hector-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!