Cassandra Hector: How to retrieve all rows of a column family?

半城伤御伤魂 提交于 2019-11-30 00:22:53
tom.wilkie

Try something like this:

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();
    }
}

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).

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.

Jimmy

Try this out:

    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();
    }

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