HBase
HBase Architecture, Components, Data Model, and Use Cases
Storage Mechanism in HBase HBase is a column-oriented database and data is stored in tables. The...
In this tutorial, you will learn:
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
Example:
hbase> put 'guru99', 'r1', 'c1', 'value', 10
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".
In this section, we will check the following
From the above screenshot, we can infer
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.
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
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.
From the screen shot above
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"
The above screenshot shows the data is being read from HBase table 'guru99'
Once writing code is done, you have to run java application like this
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
Storage Mechanism in HBase HBase is a column-oriented database and data is stored in tables. The...
Download PDF Following are frequently asked questions in interviews for freshers as well...
In this tutorial- you will learn, Apache HBase Installation Modes How to Download Hbase tar file...
HBase architecture always has " Single Point Of Failure " feature, and there is no exception...
After successful installation of HBase on top of Hadoop, we get an interactive shell to execute...
What is HBase? HBase is an open-source, column-oriented distributed database system in a Hadoop ...