【问题标题】:Get all values of all rows in Hbase using Java使用Java获取Hbase中所有行的所有值
【发布时间】:2013-07-30 05:42:48
【问题描述】:

我有以下代码。我正在尝试检索给定列族的表中的所有行。我能够得到所有的行,但输出不是我所期望的。我得到一个显示密钥和时间戳但不显示值的输出。为什么不显示行的值?请帮忙。输出如下:

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

【问题讨论】:

    标签: java eclipse get hbase


    【解决方案1】:

    要获取所有列的所有行,您无需在 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()));
        }
    }
    

    【讨论】:

    • 这看起来好多了。
    • result.list() 现在已弃用。知道在新 API 中使用什么吗?
    【解决方案2】:

    我想提供没有弃用方法的解决方案

        //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()));
    

    【讨论】:

    【解决方案3】:

    这是扫描表格中“标记”列族的代码。

    使用它可以获取行、列、时间戳和值。

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

    【讨论】:

      【解决方案4】:

      使用新的 HBase API,代码如下:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多