3

I have a column family with multiple columns:

create column family user_attr2
with comparator = 'UTF8Type'
and default_validation_class = 'UTF8Type'
and key_validation_class = 'UTF8Type'
and column_metadata = [
 {column_name: attr_value, validation_class: UTF8Type },
 {column_name: last_sync_timestamp, validation_class: LongType},
 {column_name: last_sync_digest, validation_class: UTF8Type }
];

But Astyanax has only:

public class ColumnFamily<K, C> { ... }

How to read data from above column family using Astyanax?

thx chuck

3 Answers3

3

The Astyanax getting started guide covers an example of doing this. But, based on your question, the confusion lies in the multiple column value types (validators) specified in the column metadata, while the ColumnFamily contract seems to imply that a ColumnFamily's values are of a single type.

Underneath the scenes Astyanax is going to serialize to/from ByteBuffer anyway (using it's serializers for primitives or using the serializers you specify)... but this ColumnFamily api/contract, when taken in the context of reading/writing (as opposed to programmatic column family definition), can be misleading.

First, you'll need to initialize Astyanax (Initialization). Then something like this (modified from the Getting started guide):

ColumnFamily<String, String> CF_USER_ATTR =
  new ColumnFamily<String, String>(
    "user_attr2",              // Column Family Name
    StringSerializer.get(),   // Key Serializer
    StringSerializer.get());  // Column Serializer

OperationResult<ColumnList<String>> result =
  ks.prepareQuery(CF_USER_ATTR)
    .getKey("Key1")
    .execute();
ColumnList<String> columns = result.getResult();

// Lookup columns in response by name 
String attr_value        = columns.getColumnByName("attr_value").getStringValue();
long timestamp   = columns.getColumnByName("last_sync_timestamp").getLongValue();
String digest = columns.getColumnByName("last_sync_digest").getStringValue();

// Or, iterate through the columns
for (Column<String> c : result.getResult()) {
  System.out.println(c.getName());
}

See the Astyanax Getting Started for more.

Matt Self
  • 19,520
  • 2
  • 32
  • 36
1

I don't know if it will help, but examples of reading are at line 106 in this file

https://github.com/deanhiller/playorm/blob/8a4f3405631ad78e6822795633da8c59cb25bb29/input/javasrc/com/alvazan/orm/layer9z/spi/db/cassandra/CursorKeysToRows.java

and line 86 in this file

https://github.com/deanhiller/playorm/blob/8a4f3405631ad78e6822795633da8c59cb25bb29/input/javasrc/com/alvazan/orm/layer9z/spi/db/cassandra/CassandraSession.java

OR you could run playOrm's test cases BUT you have to modify FactorySingleton.java where it says IN_MEMORY and change it to CASSANDRA to run and debug it and watch it in action. This could allow you to copy.

playOrm is a ORM that does S-SQL (Scalable SQL) on top of noSQL such that you can do queries(including joins) within partitions....soon it will have an ad-hoc tool as well for querying partitions.

Dean Hiller
  • 19,235
  • 25
  • 129
  • 212
0

Looks like the C in ColumnFamily is for column name. this is not clear from the java doc of the class.

 * @param <C>

in this case I am lucky - my column names are String.