Matthew Herbst
Matthew Herbst

Reputation: 31983

How do I retrieve the name of the key column in a Cassandra keyspace/columnfamily using Hector?

I want to convert a Cassandra keyspace/columnfamily row into a JSON string. My current code (fragments):

RangeSlicesQuery<K, N, V> rangeSlicesQuery = HFactory
    .createRangeSlicesQuery(this.keyspace, this.kSerializer, this.nSerializer, this.vSerializer)
    .setColumnFamily(this.columnFamily)
    .setRange(null, null, false, 100)
    .setRowCount(1000);

this.results = this.rangeSlicesQuery.execute();
this.resultRows = this.results.get();
this.resultRowsIterator = this.resultRows.iterator();

Row<K, N, V> row = this.resultRowsIterator.next();

From the row, I can get all column names/values easily (I am using the Jackson JSON API to create the JSON):

List<HColumn<N, V>> columns = row.getColumnSlice().getColumns();

for(HColumn<N, V> column : columns) {
    node.put(column.getName().toString(), column.getValue().toString());
}
System.out.println(node.toString());

My question is, how do I get the key column name? I know I can get the row's key value by doing:

row.getKey();

But I haven't found any way to get the key column's name for example, something like row.getKeyColumnName(). I've tried looking at KeySpaceDefinition, ColumnFamilyDefinition, ColumnDefinition, and ColumnFamilyTemplate. None of them seem to have this ability (though maybe there is a query that can be run against the template?)

Upvotes: 0

Views: 836

Answers (1)

Caleb Rackliffe
Caleb Rackliffe

Reputation: 575

The row keys of Thrift column families do not have explicit names.

If you're using a CQL table, the columns that constitute your primary key have names, but Hector may not be a good fit for accessing them, as it was developed before CQL became the dominant way to model data. In either case, I would strongly recommend looking at the Datastax driver at https://github.com/datastax/java-driver if you're using Cassandra 1.2+.

Upvotes: 1

Related Questions