2013-07-30 9 views
5

私は次のコードを持っています。私は列のファミリを与えられたテーブル内のすべての行を取得しようとしています。私はすべての行を得ることができましたが、出力は私が期待したものではありません。キーとタイムスタンプは表示されますが、値は表示されません。行の値が表示されないのはなぜですか?助けてください。出力は以下の通りである:Javaを使用してHbaseのすべての行のすべての値を取得

keyvalues={Justin/marks:total/1375104216267/Put/vlen=7/ts=0, Justin/marks:markPercentage/ 1375104186783/Put/vlen=4/ts=0} 

//コードをここにHBaseの

public class GetHbaseData { 
public static void getdata() throws IOException{ 
@SuppressWarnings("resource") 
HTable table = new HTable(HBaseConfiguration.create(), "Student"); 
Scan scan = new Scan(); 
scan.setCaching(20); 

scan.addFamily(Bytes.toBytes("marks")); 
ResultScanner scanner = table.getScanner(scan); 

for (Result result = scanner.next(); (result != null); result = scanner.next()) { 
    Get get = new Get(result.getRow()); 
    Result entireRow = table.get(get); 
    System.out.println(entireRow); 
} 
} 

答えて

5

からすべての行を取得するには、テーブルに「マーク」の欄の家族をスキャンするためのコードです。

これを使用すると、行、列、タイムスタンプと値を取得できます。

Scan scan = new Scan(); 
    scan.setCaching(hBaseScanCacheSize); 
    scan.setBatch(hbaseScanBatchSize); 
    scan.addFamily(Bytes.toBytes("marks")); 

    ResultScanner resultScanner = table.getScanner(scan); 
    Iterator<Result> iterator = resultScanner.iterator(); 
    while (iterator.hasNext()) 
    { 
     Result next = iterator.next(); 
     for(Entry<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> columnFamilyMap : next.getMap().entrySet()) 
     { 
      for (Entry<byte[], NavigableMap<Long, byte[]>> entryVersion : columnFamilyMap.getValue().entrySet()) 
      { 
       for (Entry<Long, byte[]> entry : entryVersion.getValue().entrySet()) 
       { 
        String row = Bytes.toString(next.getRow()); 
        String column = Bytes.toString(entryVersion.getKey()); 
        byte[] value = entry.getValue(); 
        long timesstamp = entry.getKey(); 
       } 
      } 
     } 
    } 
13

すべての列を持つすべての行を取得するには、forループ内でGet呼び出しを再度行う必要はありません。このようなことを試してみてください。

for (Result result = scanner.next(); (result != null); result = scanner.next()) { 
    for(KeyValue keyValue : result.list()) { 
     System.out.println("Qualifier : " + keyValue.getKeyString() + " : Value : " + Bytes.toString(keyValue.getValue())); 
    } 
} 
+0

これは非常に良く見えます。 – Tariq

8

私は非推奨メソッド新しいHBaseのAPIと

//Get 
    Get theGet = new Get(Bytes.toBytes("rowkey1")); 
    Result result = table.get(theGet); 
    //get value first column 
    String inValue1 = Bytes.toString(result.value()); 
    //get value by ColumnFamily and ColumnName 
    byte[] inValueByte = result.getValue(Bytes.toBytes(columnFamily), Bytes.toBytes(columnQualifier1)); 
    String inValue2 = Bytes.toString(inValueByte); 

    //loop for result 
    for (Cell cell : result.listCells()) { 
     String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell)); 
     String value = Bytes.toString(CellUtil.cloneValue(cell)); 
     System.out.printf("Qualifier : %s : Value : %s", qualifier, value); 
    } 

    //create Map by result and print it 
    Map<String, String> getResult = result.listCells().stream().collect(Collectors.toMap(e -> Bytes.toString(CellUtil.cloneQualifier(e)), e -> Bytes.toString(CellUtil.cloneValue(e)))); 
    getResult.entrySet().stream().forEach(e-> System.out.printf("Qualifier : %s : Value : %s", e.getKey(), e.getValue())); 
+0

さらに多くの例があります。https://github.com/glebmtb/hbase_example/blob/master/src/main/java/ru/n5g/hbaseclient/SimpleHBaseClient.java –

0

せずにソリューションを提供したいと思い、コードは次のようである:

for (Result result = scanner.next(); (result != null); result = scanner.next()) { 
     for(Cell cell : result.listCells()) { 
      String qualifier = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength()); 
      String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()); 
      System.out.println("Qualifier : " + qualifier 
        + " : Value : " + value); 
     } 
    } 
関連する問題