简体   繁体   English

Hbase从单元格中提取值和时间戳

[英]Hbase extracting value and timestamp from a cell

In hbase I have number of columns: name, city,... 在hbase中我有多列:名称,城市,......
Not all columns have values ( some rows can have only 'name' for example) 并非所有列都具有值(例如,某些行只能包含'name')

I want to extract all columns in a row + timestamp of column (in specific order), in case value is null I want to return empty string. 我想提取一行中的所有列+列的时间戳(按特定顺序),以防值为null我想返回空字符串。

The problem that I facing, I must access column in Result by 'family' and 'qualifier' (I can't access by index of result.listCells().get(i) because null values are skipped) 我面临的问题是,我必须通过'family'和'qualifier'访问Result的列(我无法通过result.listCells().get(i)的索引访问result.listCells().get(i)因为跳过了null值)

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

You can try to use a CellScanner for this. 您可以尝试使用CellScanner。 See example below: 见下面的例子:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM