Recent Posts

Saturday, 11 March 2017

HBase Read Data Tutorial


Reading Data using HBase Shell
      Using get command, you can get a single row of data at a time. Its syntax is as follows
get ’<table name>’,’row1’
Example
     The following example shows how to use the get command. Let us scan the first row of the emp table.
hbase> get 'emp', '1'

   COLUMN                     CELL
   
personal : city timestamp = 1217521845985, value = vijayawada

personal : name timestamp = 1217521785385, value = ashok

professional: designation timestamp = 121752125687, value = software engineer

professional: salary timestamp = 1217521902983 value = 25000

4 row(s) in 0.0270 seconds
Reading a Specific Column
     Given below is the syntax to read a specific column using the get method.
hbase> get 'table name', ‘rowid’, {COLUMN ? ‘column family:column name ’}
Example
     Given below is the example to read a specific column in HBase table.
hbase> get 'emp', 'row1', {COLUMN ? 'personal:name'}
 COLUMN                CELL  
personal:name timestamp = 1217521785385, value = ashok
1 row(s) in 0.0060 seconds
Reading Data Using Java API
package com.ashok.hbase;

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;

public class RetriveData{

   public static void main(String[] args) throws IOException, Exception{
   
      // Instantiating Configuration class
      Configuration config = HBaseConfiguration.create();

      // Instantiating HTable class
      HTable table = new HTable(config, "emp");

      // Instantiating Get class
      Get g = new Get(Bytes.toBytes("row1"));

      // Reading the data
      Result result = table.get(g);

      // Reading values from Result class object
      byte [] value = result.getValue(Bytes.toBytes("personal"),Bytes.toBytes("name"));

      byte [] value1 = result.getValue(Bytes.toBytes("personal"),Bytes.toBytes("city"));

      // Printing the values
      String name = Bytes.toString(value);
      String city = Bytes.toString(value1);
      
      System.out.println("Name: " + name + " City: " + city);
   }
}

Output
name: ashok city: Vijayawada

Next Tutorial  HBase Delete Data Tutorial

Previous Tutorial  HBase Update Data Tutorial 
 

No comments:

Post a Comment