In this tutorial, you will learn:

Write Data to HBase Table: Shell

The put command is used to store data into a table

Syntax:  put <'tablename'>,<'rowname'>,<'columnvalue'>,<'value'>

This command is used for the following things

  • It will put a cell 'value' at a defined or specified table or row or column.
  • It will optionally coordinate time stamp.

Example:

  • Here we are placing values into table "guru99" under row r1 and column c1
    hbase> put 'guru99', 'r1', 'c1', 'value', 10
  • We have placed three values, 10,15 and 30 in table "guru99" as shown in the screenshot below

HBase Shell and General Commands

  • Suppose if the table "Guru99" having some table reference like say g. We can also run the command on table reference also like

    hbase> g.put 'guru99', 'r1', 'c1', 'value', 10
  • The output will be as shown in the above screenshot after placing values into "guru99".

Read Data from HBase Table: Shell

In this section, we will check the following

  • Values that are inserted into HBase table "guru99"
  • Column names with values present in HBase Table guru99

Create, Insert, Read Tables in HBase

From the above screenshot, we can infer

  • If we run "scan" command in HBase shell it will display the inserted values in "guru99" as follow
  • In HBase shell, it will display values inserted by our code with column and row names
  • Here we can see the column name inserted are "education" and "projects"
  • The values inserted are "BigData" and "HBase Tutorials" into mentioned columns

You can also use the Get command to read data from a table

Syntax: get <'tablename'>, <'rowname'>, {< Additional parameters>}

Here <Additional Parameters> include TIMERANGE, TIMESTAMP, VERSIONS and FILTERS.

By using this command, you will get a row or cell contents present in the table. In addition to that you can also add additional parameters to it like TIMESTAMP, TIMERANGE,VERSIONS, FILTERS, etc. to get a particular row or cell content.

HBase Shell and General Commands

Examples:-

hbase> get 'guru99', 'r1', {COLUMN => 'c1'}

For table "guru99' row r1 and column c1 values will display using this command as shown in the above screenshot

hbase> get 'guru99', 'r1'

For table "guru99" row r1 values will be displayed using this command

hbase> get 'guru99', 'r1', {TIMERANGE => [ts1, ts2]}

For table "guru99" row 1 values in the time range ts1 and ts2 will be displayed using this command

hbase> get 'guru99', 'r1', {COLUMN => ['c1', 'c2', 'c3']}

For table "guru99" row r1 and column families' c1, c2, c3 values will be displayed using this command

Write Data to HBase Table: JAVA API

In this step, we are going to write data into HBase table "guru99"

First, we have to write code for insert and retrieve values from HBase by using-HBaseLoading.java program.

For creating and inserting values into a table at the column level, you have to code like below.

Create, Insert, Read Tables in HBase

From the screen shot above

  1. When we create HBase configuration, it will point to whatever the configurations we set in base-site.xml and hbase-default.xml files during HBase installations
  2. Creation of table "guru99" using HTable method
  3. Adding row1 to table "guru99"
  4. Specifying column names "education" and "projects" and inserting values into column names in the respective row1. The values inserted here are "BigData" and "HBaseTutorials".

Read Data from HBase Table: Java API

Whatever the values that we placed in HBase tables in the above section, here we are going to fetch and display those values.

For retrieving results stored in "guru99"

Create, Insert, Read Tables in HBase

The above screenshot shows the data is being read from HBase table 'guru99'

  1. In this, we are going to fetch the values that are stored in column families i.e "education" and "projects"
  2. Using "get" command we are going to fetch stored values in HBase table
  3. Scanning results using "scan" command. The values that are stored in row1 it will display on the console.

Once writing code is done, you have to run java application like this

  • Right click on HBaseLoading.java -> Run As -> Java Application
  • After running "HBaseLoading .java" the values going to insert into "guru99" in each column in HBase and in the same program it can retrieve values also.

Here is the complete code

import java.io.IOException;							
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.Put;							
import org.apache.hadoop.hbase.client.Result;						
import org.apache.hadoop.hbase.client.ResultScanner;						
import org.apache.hadoop.hbase.client.Scan;						
import org.apache.hadoop.hbase.util.Bytes;							
public class HBaseLoading							
{							
public static void main(String[] args) throws IOException						
{							
/* When you create a HBaseConfiguration, it reads in whatever you've set into your hbase-site.xml and in hbase-default.xml, as long as these can be found on the CLASSPATH*/							

org.apache.hadoop.conf.Configuration config = HBaseConfiguration.create();							

/*This instantiates an HTable object that connects you to the "test" table*/	

HTable table = new HTable(config, "guru99");										

/* To add to a row, use Put. A Put constructor takes the name of the row you want to insert into as a byte array.*/					

Put p = new Put(Bytes.toBytes("row1"));										

/*To set the value you'd like to update in the row 'row1', specify  the column family, column qualifier, and value of the table cell you'd like to update.  The column family must already exist in your table schema.  The qualifier can be anything.*/ 							

p.add(Bytes.toBytes("education"), Bytes.toBytes("col1"),Bytes.toBytes("BigData"));								
p.add(Bytes.toBytes("projects"),Bytes.toBytes("col2"),Bytes.toBytes("HBaseTutorials"));

// Once you've adorned your Put instance with all the updates you want to  make, to commit it do the following 							

table.put(p);	

// Now, to retrieve the data we just wrote.	

  Get g = new Get(Bytes.toBytes("row1"));										
  Result r = table.get(g);					

byte [] value = r.getValue(Bytes.toBytes("education"),Bytes.toBytes("col1"));											
byte [] value1 = r.getValue(Bytes.toBytes("projects"),Bytes.toBytes("col2"));											
String valueStr = Bytes.toString(value);							

String valueStr1 = Bytes.toString(value1);							
System.out.println("GET: " +"education: "+ valueStr+"projects: "+valueStr1);														

  Scan s = new Scan();								

s.addColumn(Bytes.toBytes("education"), Bytes.toBytes("col1"));										
s.addColumn(Bytes.toBytes("projects"), Bytes.toBytes("col2"));										
ResultScanner scanner = table.getScanner(s);							
try							
{							
for (Result rr = scanner.next(); rr != null; rr = scanner.next())									
   {							
System.out.println("Found row : " + rr);										
       }							
} finally							
{							
// Make sure you close your scanners when you are done!						

scanner.close();							
       }							
   }							
}

Summary:

As we discussed in this tutorial, you can use the put command to insert data into a table. You can use the scan, get command to read data from a table

 

YOU MIGHT LIKE: