2011-12-07 82 views
17

我要尋找一個代碼示例檢索所有的行和列家族的所有列。像這樣:卡桑德拉赫克託:如何檢索列家族的所有行?

SELECT * FROM MyTable 

我看到這可以使用RangeSlicesQuery來完成,但你仍然必須提供一定的範圍。我想你也必須指定列名稱。有沒有一種乾淨而安全的方法來做到這一點?

使用赫1.0和1.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列(你也會想要分頁非常長的行)。

這是一個系列列的UUID的行鍵,字符串列名,渴望值。希望它應該是顯而易見的,如何改變這一點。

+0

謝謝您的回答。但這是我所做的。我只需設置rangeSlicesQuery.setKeys(「」,「」),並且不設置任何行數。這返回了列族中的所有行。看起來沒有必要翻閱欄目。 –

+0

要繼續我以前的評論,那樣做,我需要指定列名。 –

+3

我很確定Hector沒有爲你實現分頁。您的代碼可能會失敗,超時(或者更糟,導致卡桑德拉到OOM)當你的數據集變得更大,因爲這樣做你認爲是什麼原因導致卡桑德拉到整個數據集加載到RAM中。 –

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

} 
相關問題