我正在寻找一个代码示例来检索列族的所有行和所有列。类似的东西:
SELECT * FROM MyTable
我看到这可以使用RangeSlicesQuery完成,但您仍需提供一定的范围。我认为你也必须指定列名。有干净安全的方法吗?
使用Hector 1.0和Cassandra 1.0。
答案 0 :(得分:15)
尝试这样的事情:
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列(你也想要分页很长的行)。
这适用于具有行键的uuids,列名称的字符串和值的long的列族。希望如何改变这一点应该是显而易见的。
答案 1 :(得分:2)
试试这个:
int rowCount = MAX;
RangeSlicesQuery<String, String, String> rangeSlicesQuery = HFactory
.createRangeSlicesQuery(keyspace2, STRINGSERIALIZER,
STRINGSERIALIZER, STRINGSERIALIZER)
.setColumnFamily(columnFamily)
.setRange(null, null, false, rowCount).setRowCount(rowCount);
String lastKey = null;
// Query to iterate over all rows of cassandra Column Family
rangeSlicesQuery.setKeys(lastKey, null);
QueryResult<OrderedRows<String, String, String>> result = rangeSlicesQuery
.execute();
OrderedRows<String, String, String> rows = result.get();
for (Row<String, String, String> row : rows) {
String cassandra_key = row.getKey();
}
}