2016-01-20 35 views
3

在HBase的我列數:姓名,城市,...
並非所有列的值(某些行只能有「名」爲例)HBASE從細胞中提取值和時間戳

我想要提取列中的所有列+列的時間戳(按特定順序),如果值爲空我想返回空字符串。

,我面對的,我必須通過「家庭」和「預選賽」(我不能用的result.listCells().get(i)索引訪問,因爲空值被跳過)在Result訪問列

scan.addColumn(Bytes.toBytes("personal data"), Bytes.toBytes("name")); 
scan.addColumn(Bytes.toBytes("personal data"), Bytes.toBytes("city")); 

ResultScanner scanner = table.getScanner(scan); 

for (Result result = scanner.next(); result != null; result = scanner.next()){ 

    byte [] valCity = result.getValue("personal data", "city"); //can't get timestamp using this 
    //check if valCity null write ("") else write the value 
    //next column... 
} 

回答

1

你可以試試問題爲此使用CellScanner。見下面的例子:

CellScanner cellScanner = result.cellScanner(); 
    while (cellScanner.advance()) { 
     Cell cell = cellScanner.current(); 
     byte[] columnName = Bytes.copy(cell.getQualifierArray(), 
       cell.getQualifierOffset(), 
       cell.getQualifierLength()); 
     byte[] familyName = Bytes.copy(cell.getFamilyArray(), 
       cell.getFamilyOffset(), 
       cell.getFamilyLength()); 
     long timestamp = cell.getTimestamp(); 
     ..... 
    } 
+0

我估計用這個是沒有效率的尋找合適的欄,我將需要循環掃描每個我尋找一個合適的列名的時間。 萬一我需要按特定順序提取列。 – markiz

+0

嗯,你寫在你的問題,你想獲得所有列。您可以記住列的名稱,時間戳和值,並根據需要對它們進行排序。 –

+1

按特定順序獲取所有列。這就是爲什麼我想使用'result.getValue(「個人數據」,「城市」);'這使我可以通過名稱訪問我也看不到掃描儀和'result.listCells()。get我)' – markiz