【问题标题】:Hbase extracting value and timestamp from a cellHbase 从单元格中提取值和时间戳
【发布时间】:2016-01-20 20:30:07
【问题描述】:

在 hbase 中,我有多个列:名称、城市、...
并非所有列都有值(例如,某些行只能有“名称”)

我想提取一行中的所有列+列的时间戳(按特定顺序),如果值为null,我想返回空字符串。

我面临的问题,我必须通过'family'和'qualifier'访问Result中的列(我无法通过result.listCells().get(i)的索引访问,因为跳过了空值)

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

【问题讨论】:

标签: java hbase


【解决方案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();
        .....
    }

【讨论】:

  • 我想使用它搜索右列效率不高,因为每次搜索右列名称时我都需要循环扫描仪。以防我需要按特定顺序提取列。
  • 嗯,您在问题中写下您想要获得所有列的内容。您可以记住列名称、时间戳和值,并根据需要对其进行排序。
  • 按特定顺序获取所有列。这就是为什么我想使用result.getValue("personal data", "city"); 这允许我按名称访问我也看不到扫描仪和result.listCells().get(i) 之间的真正区别@
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-10
  • 2016-08-10
  • 1970-01-01
  • 2020-03-11
  • 1970-01-01
  • 2017-02-01
相关资源
最近更新 更多