Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all values of all rows in Hbase using Java

I have the following code.I am trying to retrieve all the rows in the table given the column family. I was able to get all the rows but the output is not what i expected. I get an output which shows the key and time stamp but not the value. Why isn't the values of the rows getting displayed? Please help. The output is given below:

  keyvalues={Justin/marks:total/1375104216267/Put/vlen=7/ts=0, Justin/marks:markPercentage/  1375104186783/Put/vlen=4/ts=0}

//Code to get all rows from 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);
}
}
like image 742
Sindu_ Avatar asked Jul 30 '13 05:07

Sindu_


2 Answers

For getting all rows with all columns you dont need to make Get call again inside your for loop. Try something like this.

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()));
    }
}
like image 103
Naresh Reddy Avatar answered Sep 29 '22 16:09

Naresh Reddy


I would like to offer solution without deprecated methods

    //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()));
like image 44
Gleb Belyaev Avatar answered Sep 29 '22 16:09

Gleb Belyaev