简体   繁体   English

使用Hector客户端从Cassandra检索多个列值

[英]Retrieve multiple columns value from Cassandra using Hector client

I am working with Cassandra and I am using Hector client to read and upsert the data in Cassandra database. 我正在与Cassandra合作,我正在使用Hector客户端来读取和插入Cassandra数据库中的数据。 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. 我正在尝试使用hector客户端从Cassandra数据库中检索数据,如果我只想检索一列,我可以这样做。

Now I am trying to retrieve the data for rowKey as 1011 but with columnNames as collection of string. 现在我尝试将rowKey as 1011的数据检索rowKey as 1011但将columnNames作为字符串集合。 Below is my API that will retrieve the data from Cassandra database using Hector client- 以下是我的API,它将使用Hector客户端从Cassandra数据库中检索数据 -

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 . 如果您看到我的上述方法,我正在尝试从Cassandra数据库中检索特定rowKeycolumn c1 Now I am trying to retrieve the data from Cassandra database for collection of columns for a particular rowKey. 现在我正在尝试从Cassandra数据库中检索数据以收集特定rowKey的列。

Meaning something like this- 意思是这样的 -

I want to retrieve the data for multiple columns but for the same rowKey. 我想检索多列的数据,但是要检索相同的rowKey。 How can I do this using Hector client? 如何使用Hector客户端执行此操作? 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. 我不想检索所有列的数据,然后迭代以找出我正在寻找的各个列数据。

Use column name made up with composite key as combination of UTF8Type and TIMEUUID then after 使用由复合键组成的列名称作为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();

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 一些你必须编写逻辑来维护记录集的地方

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Hector在Cassandra中存储和检索float [] - Store and retrieve a float[] to/from Cassandra using Hector 使用Hector在Cassandra中查询CompositeType列 - Querying CompositeType columns in Cassandra using Hector Cassandra,Hector:如何在1次调用中从列族中检索特定键的特定列集? - Cassandra , Hector :how to retrieve specific set of columns for specific keys from a column family in 1 call? 使用Hector从Cassandra检索(负)双精度值会得出错误的结果 - Retrieve (negative) double values from Cassandra using Hector gives wrong results Cassandra Hector:如何获取家庭中所有键值? - Cassandra Hector : How to retrieve all keyrows value in family? 如何运行Cassandra Hector客户端示例? - How to run Cassandra Hector client example? 如何使用Hector在Cassandra键空间/列家族中检索键列的名称? - How do I retrieve the name of the key column in a Cassandra keyspace/columnfamily using Hector? 使用Cassandra中的hector客户端使用组合键的第一个组件获取行 - Get rows using first component of composite key using hector client in Cassandra Cassandra Java客户端(Hector):在整个应用程序中使用相同的集群和键空间对象 - Cassandra Java Client (Hector) : Using the same cluster and keyspace objects throughout the application 使用hector API在Cassandra中更新架构 - Updating the Schema in Cassandra using hector API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM