Cassandra Hector:如何检索列族的所有行? [英] Cassandra Hector: How to retrieve all rows of a column family?

查看:176
本文介绍了Cassandra Hector:如何检索列族的所有行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个代码示例来检索列族的所有行和所有列。例如:

I am looking for a code example to retrieve all rows and all columns of a column family. Something like:

SELECT * FROM MyTable

我看到这可以使用RangeSlicesQuery完成,但你仍然需要提供一定的范围。我想你也必须指定列名。有没有干净安全的方法来做到这一点?

I see that this can be done using a RangeSlicesQuery, but you still have to provide a certain range. And I think you have to specify the column names too. Is there a clean and safe way to do this?

使用Hector 1.0和Cassandra 1.0。

Using Hector 1.0 and 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();
    }
}

行。它将只为每一行获取10列(您也希望分页非常长的行)。

This will page through the column family in pages of 100 rows. It will only fetch 10 columns for each row (you will want to page very long rows too).

这是一个列族的uuids行键,字符串列名称和长整型值。希望这应该是明显的如何改变这。

This is for a column family with uuids for row keys, strings for column names and longs for values. Hopefully it should be obvious how to change this.

这篇关于Cassandra Hector:如何检索列族的所有行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆