使用Hector客户端从Cassandra检索多个列值 [英] Retrieve multiple columns value from Cassandra using Hector client

查看:146
本文介绍了使用Hector客户端从Cassandra检索多个列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Cassandra,我使用Hector客户端读取和上传Cassandra数据库中的数据。我试图使用hector客户端从Cassandra数据库检索数据,我可以这样做,如果我试图只检索一列。

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.

现在我试图检索 rowKey的数据为1011 ,但使用columnNames作为字符串的集合。下面是我的API,它将使用Hector客户端从Cassandra数据库检索数据 -

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;

}

如果您看到我的上述方法,来自Cassandra数据库的特定 rowKey 列c1 的数据。现在我正在尝试从Cassandra数据库检索数据以收集特定rowKey的列。

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.

意思是这样 -

我想检索多个列的数据,但是为同一个rowKey。我如何使用Hector客户端?我不想检索所有列的数据,然后迭代找到我正在寻找的单个列数据。

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.

推荐答案

使用由复合键组成的列名称作为UTF8Type和TIMEUUID
的组合,然后在

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

以上代码将为您提供行密钥
的所有记录, / p>

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

这篇关于使用Hector客户端从Cassandra检索多个列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆