1

我已将数据存储在 Cassandra 数据库的 ComparatorType.BYTESTYPE 中。现在我想按存储顺序检索数据。

我在 Hector 中使用了以下代码来获取数据,但是查询结果似乎没有排序。

Keyspace keyspace = HFactory.createKeyspace("ClusterName", cluster);

Map<String, String> resultMap = new HashMap<String, String>();

SliceQuery<String, String, String> query = HFactory.createSliceQuery(keyspace,   StringSerializer.get(), StringSerializer.get(), StringSerializer.get());
query.setColumnFamily("ColumnFamilyName").setKey("RowKey")
.setRange("", "", false, Integer.MAX_VALUE);
QueryResult<ColumnSlice<String, String>> result = query.execute();

for (HColumn<String, String> column : result.get().getColumns()) {
        resultMap.put(column.getName(), column.getValue());
}

我错过了什么?我必须使用 RangeSliceQueries 和 OrderedRows 吗?

提前致谢。

4

2 回答 2

1

It all depends on the comparator you have set for the column names. If it is BytesType (cassandra's default) as well, then it won't be the order you expect.

if you want alphabetical order you should define your table with something like that in the cassandra-cli:

create column family ColumnFamilyName with comparator = UTF8Type;

or you are still in time maybe to do

update column family ColumnFamilyName with comparator = UTF8Type;

于 2012-08-09T16:09:04.967 回答
0

谢谢你的回答。

但是我通过将集合更改为 LinkedHashMap 而不是 HashMap 来解决它。正如我注意到的

    result.get().getColumns()

按顺序给出结果,但由于 HasMap 不保留插入顺序,因此结果是无序的。因此,当您将 HashMap 更改为 LinkedHashMap 时,上述代码可以正常工作。

    Map<String, String> resultMap = new LinkedHashMap<String, String>();
于 2012-08-12T10:40:13.673 回答