2013-07-30 49 views
5

Tôi có mã sau đây.Tôi đang cố gắng truy xuất tất cả các hàng trong bảng cho họ cột. Tôi đã có thể nhận được tất cả các hàng nhưng đầu ra không phải là những gì tôi mong đợi. Tôi nhận được một đầu ra cho thấy chìa khóa và thời gian đóng dấu nhưng không phải là giá trị. Tại sao không phải là giá trị của các hàng được hiển thị? Hãy giúp tôi. Kết quả này được đưa ra dưới đây:Nhận tất cả các giá trị của tất cả các hàng trong HBase bằng cách sử dụng Java

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

// Mã để có được tất cả các hàng từ 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); 
} 
} 

Trả lời

5

đây là một mã số để quét các "dấu" Gia đình cột trong bảng.

sử dụng nó, bạn có thể nhận hàng, cột, dấu thời gian và giá trị.

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

Để nhận tất cả các hàng có tất cả các cột bạn không cần thực hiện Gọi lại trong vòng lặp của bạn. Hãy thử một cái gì đó như thế này.

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

này trông đẹp hơn nhiều. – Tariq

8

tôi muốn cung cấp giải pháp mà không phương pháp phản

//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

bạn có thể tìm thêm ví dụ tại https://github.com/glebmtb/hbase_example/blob/master/src/main/java/ru/n5g/hbaseclient/SimpleHBaseClient.java –

0

Với API HBase mới, mã này là như thế này:

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); 
     } 
    } 
Các vấn đề liên quan