Past few weeks, I've been working on Hadoop/HBase and just wanted to share how simple it is to use HBase as a non-relational, distributed database.
Tables in HBase can server as the input and output for MapReduce jobs run in Hadoop and may be accessed through the Java API which is vast. But it can also be accessed though the Thrift gateway APIs(Python etc).
Note: This is based on the fact that you know how to create a java project in eclipse and this assumes you already have.
Put Operation using Java API
Make sure that your Java Project created in eclipse has hbase and hadoop jars.
Right click on your Java Project -> Build Path -> Configure Build Path -> Libraries - > Add external Jars -> "Add the HBase and Hadoop relevant jars".
Now you are good to go.
Go to your HBase shell and create a table called 'testtable'.
Copy/Paste the code:
Right click on your Java Project -> Build Path -> Configure Build Path -> Libraries - > Add external Jars -> "Add the HBase and Hadoop relevant jars".
Now you are good to go.
Go to your HBase shell and create a table called 'testtable'.
hbase(main):000:0> create ‘testtable’, ‘cf’ 0 row(s) in 1.0970 seconds
Copy/Paste the code:
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;
import java.io.IOException;
public class ClassName {
public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create(); //create a configuration object.
HTable table = new HTable(conf, "testtable"); // testtable is name of the table
Put put = new Put(Bytes.toBytes("row1")); // insert a row with row1 as the key
put.add(Bytes.toBytes("cf"), Bytes.toBytes("a"),
Bytes.toBytes("val1")); // column family -> "cf" & column qualifier "a". Value is val1.
put.add(Bytes.toBytes("cf"), Bytes.toBytes("b"),
Bytes.toBytes("val2")); // column family -> "cf" & column qualifier "b". Value is val2.
table.put(put); //insert the put object into HBase.
System.out.println("Success!");
}
}
You should get Success if everything goes right!
Get Example using Java API
Now we will retrieve the rows we just put in HBase.
Copy/Paste the following code:
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;
import java.io.IOException;
public class GetExample {
public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
HTable table = new HTable(conf, "testtable"); // Create a configuration object.
Get get = new Get(Bytes.toBytes("row1")); // get object initialized with row1 as key
get.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("a")); // add column family and column qualifier to the get object
Result result = table.get(get);
byte[] val = result.getValue(Bytes.toBytes("cf"),
Bytes.toBytes("a")); // by default result is a byte array
System.out.println("Value: " + Bytes.toString(val)); // convert the byte array into String.
}
}
Happy Coding!!
HBase is very easy to play around with. Comment below if you have any questions. I'm just getting started with the tutorials. Watch out for more :)
HBase is very easy to play around with. Comment below if you have any questions. I'm just getting started with the tutorials. Watch out for more :)
No comments:
Post a Comment