使用Java获取Hbase中所有行的所有值

问题描述 投票:5回答:4

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

  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
4个回答
13
投票

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

8
投票

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

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

5
投票

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

使用它,您可以获得行,列,时间戳和值。

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

0
投票

使用新的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);
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.