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);
}
}
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()));
}
}
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()));
here is a code to scan the "marks" column family in the table.
using it you can get the row, column, time stamp and value.
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();
}
}
}
}
With the new HBase APIs, the code is like this:
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);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.