gpt4 book ai didi

Cassandra ·赫克托 : How to retrieve all rows of a column family?

转载 作者:行者123 更新时间:2023-12-02 19:52:00 24 4
gpt4 key购买 nike

我正在寻找一个代码示例来检索列族的所有行和所有列。像这样的东西:

SELECT * FROM MyTable

我发现这可以使用 RangeSlicesQuery 来完成,但您仍然必须提供一定的范围。我认为您也必须指定列名称。有没有一种干净、安全的方法来做到这一点?

使用 Hector 1.0 和 Cassandra 1.0。

最佳答案

尝试这样的事情:

public class Dumper {
private final Cluster cluster;
private final Keyspace keyspace;

public Dumper() {
this.cluster = HFactory.getOrCreateCluster("Name", "hostname");
this.keyspace = HFactory.createKeyspace("Keyspace", cluster, new QuorumAllConsistencyLevelPolicy());
}

public void run() {
int row_count = 100;

RangeSlicesQuery<UUID, String, Long> rangeSlicesQuery = HFactory
.createRangeSlicesQuery(keyspace, UUIDSerializer.get(), StringSerializer.get(), LongSerializer.get())
.setColumnFamily("Column Family")
.setRange(null, null, false, 10)
.setRowCount(row_count);

UUID last_key = null;

while (true) {
rangeSlicesQuery.setKeys(last_key, null);
System.out.println(" > " + last_key);

QueryResult<OrderedRows<UUID, String, Long>> result = rangeSlicesQuery.execute();
OrderedRows<UUID, String, Long> rows = result.get();
Iterator<Row<UUID, String, Long>> rowsIterator = rows.iterator();

// we'll skip this first one, since it is the same as the last one from previous time we executed
if (last_key != null && rowsIterator != null) rowsIterator.next();

while (rowsIterator.hasNext()) {
Row<UUID, String, Long> row = rowsIterator.next();
last_key = row.getKey();

if (row.getColumnSlice().getColumns().isEmpty()) {
continue;
}


System.out.println(row);
}

if (rows.getCount() < row_count)
break;
}
}

public static void main(String[] args) {
new Dumper().run();
}
}

这将以 100 行为一页对列族进行分页。它只会为每行获取 10 列(您也需要对很长的行进行分页)。

这适用于列族,其中 uuid 为行键,字符串为列名,long 为值。希望如何改变这一点应该是显而易见的。

关于 Cassandra ·赫克托 : How to retrieve all rows of a column family?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8418448/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com