我需要在所有列中的Cassandra列系列中存储二进制字节数据。下面是我将获取二进制字节数据的代码。我的rowKey将是String,但是我的所有列都必须存储二进制blob数据。
GenericDatumWriter<GenericRecord> writer = new GenericDatumWriter<GenericRecord>(schema);
ByteArrayOutputStream os = new ByteArrayOutputStream();
Encoder e = EncoderFactory.get().binaryEncoder(os, null);
writer.write(record, e);
e.flush();
byte[] byteData = os.toByteArray();
os.close();
// write byteData in Cassandra.
我不确定为上述用例创建Cassandra列系列的正确方法是什么?以下是我创建的列系列,但是我不确定对于上述用例,这样做是否正确?
create column family TESTING
with key_validation_class = 'UTF8Type'
and comparator = 'UTF8Type'
and default_validation_class = 'UTF8Type'
and gc_grace = 86400
and column_metadata = [ {column_name : 'lmd', validation_class : DateType}];
更新:-
我将使用Astyanax Client从Cassandra检索数据。我的用例很简单。
我上面的Cassandra列系列中的所有列将仅存储二进制blob数据。
这个专栏家庭怎么样?看起来合适吗?
create column family TESTING
with key_validation_class = 'UTF8Type'
and comparator = 'TimeUUIDType'
and default_validation_class = 'ByteType'
and gc_grace = 86400
and column_metadata = [ {column_name : 'lmd', validation_class : DateType}];
当我尝试创建上述列系列时,出现了此异常-
[default@profileks] create column family TESTING
... with key_validation_class = 'UTF8Type'
... and comparator = 'TimeUUIDType'
... and default_validation_class = 'ByteType'
... and gc_grace = 86400
... and column_metadata = [ {column_name : 'lmd', validation_class : DateType}];
java.lang.RuntimeException: org.apache.cassandra.db.marshal.MarshalException: Unknown timeuuid representation: lmd
我将存储userId作为rowKey,然后存储我的列名称,该名称将存储二进制blobs数据,最后将lmd存储为DateType列。
最佳答案
@特雷基
如果您正在使用Thrift客户端:
create column family TESTING
with key_validation_class = 'UTF8Type'
and comparator = 'TimeUUIDType'
and default_validation_class = 'ByteType'
* default_validation_class *是用于存储Blob的ByteType。
由于未指定要如何访问数据,因此可以使用TimeUUIDType对列进行自然排序
如果您使用的是CQL3:
CREATE TABLE TESTING(
partition_key text, //corresponds to row key
column_name timeuuid,
data blob,
PRIMARY KEY(partition_key));