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

时间:2013-07-30 05:42:49

标签: java eclipse get hbase

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

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

4 个答案:

答案 0 :(得分: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()));
    }
}

答案 1 :(得分: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()));

答案 2 :(得分: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();
                }
            }
        }
    }

答案 3 :(得分: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);
        }
    }