質問

I am using Java Hector API to retrieve data from Cassandra databse as following:

public static void retrieveData() {
    try {
        //Create a cluster object from your existing Cassandra cluster
        Cluster cluster = HFactory.getOrCreateCluster("Test Cluster", "localhost:9160");

        //Create a keyspace object from the existing keyspace we created using CLI
        Keyspace keyspace = HFactory.createKeyspace("TestDB", cluster);

        SliceQuery<String, String, String> sliceQuery = HFactory.createSliceQuery(keyspace, stringSerializer, stringSerializer, stringSerializer);
        sliceQuery.setColumnFamily("ClientHeaders").setKey("1234");
        sliceQuery.setRange("", "", false, 10);
        sliceQuery.setColumnNames("ip_address","uuid");
        QueryResult<ColumnSlice<String, String>> result = sliceQuery.execute();                 
        System.out.println("\nInserted data is as follows:\n" + result.get());   
    } catch (Exception ex) {
        System.out.println("Error encountered while retrieving data!!");
        ex.printStackTrace() ;
    }

So I get the retrieved values according to query in this order:

ColumnSlice([HColumn(ip_address=203.110.85.171), HColumn(uuid=a3363400-abfd-0130-e2cf-07b5c765964c)])

However I want to extract the result in some string variable (string ip=ip_address etc) and use. But I couldn't find out how to do that ? Please help. Thanks.

役に立ちましたか?

解決

Try doing this:

 if (result != null && result.get() != null) {
            List<HColumn<String, String>> resultCols = result.get().getColumns();
            for (HColumn<String, String> col : resultCols)
            {
               System.out.println(col.getValue());
            }

他のヒント

Based on Hector's JavaDoc (QueryResult, ColumnSlice and HColumn), you should be able to do this:

ColumnSlice columnSlice = result.get();
HColumn<String, String> column = columnSlice.getColumnByName("ip_address");
System.out.println("The value of ip_address is " + column.getValue());

Or

for (HColumn<String, String> column : result.get().getColumns()) {
    System.out.println("The value of " + column.getName() + " is " + column.getValue());
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top