How to use cql queries to get different datatypes out of cassandra with java client hector

自古美人都是妖i 提交于 2019-12-03 12:41:30

You specify the value type as String in the CqlRows, so every value is expected to be a string. Because you want to mix Value types, you should keep the column metadata, but also specify the default validation class as BytesType in the schema and then use ByteBuffer as the type in CqlRows:

QueryResult<CqlRows<String, String, ByteBuffer>> result = cqlQuery.execute();

Then, when processing the values, you will have to convert to the appropriate type, and instead of iterating through the columns, you will probably get the specific column by name:

ColumnSlice<String, ByteBuffer> slice = row.getColumnSlice();
HColumn<String,ByteBuffer> col = slice.getColumnByName("birth_year");
System.out.println(" birth_year: " + col.getValue().getLong());

Of course, Strings have to be handled differently, using java.nio.charset.Charset:

Charset.defaultCharset().decode(col.getValue()).toString()

You can determine types from the Column meta-data, but I've only done this via the Thrift API (see ColumnDef), so not sure how to do it via Hector API. But HColumn does provide a getValueSerializer() method, so that could be a start.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!