HBase Client java API
This post describes the java client API for HBase that is used to perform CRUD operations on HBase tables (See, how to create tables.). HBase is written in Java and has a Java Native API. Therefore it provides programmatic access to Data Manipulation Language (DML).
Step 1 - Edit $HOME/.bashrc file by adding the java and hadoop path.
$HOME/.bashrc file. Add the following line
Step 2 Reload your changed $HOME/.bashrc settings
Step 3 - Change the directory to /usr/local/hbase
Step 4 - To start up the initial HBase cluster.
Add all HBase libraries to your java project's class path.
Inserting Data into Table Using java API (See, how to create tables.).
InsertData.java
Updating Data into Table Using java API
UpdateData.java
Retrieving Data from Table Using java API
RetriveData.java
Deleting Data from Table Using java API
DeleteData.java
Scan Data Using java API
ScanTable.java
This post describes the java client API for HBase that is used to perform CRUD operations on HBase tables (See, how to create tables.). HBase is written in Java and has a Java Native API. Therefore it provides programmatic access to Data Manipulation Language (DML).
Step 1 - Edit $HOME/.bashrc file by adding the java and hadoop path.
$ sudo gedit $HOME/.bashrc
export CLASSPATH=$CLASSPATH:/usr/local/hbase/lib/*
$ source $HOME/.bashrc
$ cd /usr/local/hbase
$ ./start-hbase.sh
/usrlocal/hbase/lib/*
InsertData.java
import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.util.Bytes; public class InsertData { @SuppressWarnings("deprecation") public static void main(String[] args) throws IOException { // Instantiating Configuration class Configuration config = HBaseConfiguration.create(); // Instantiating HTable class HTable hTable = new HTable(config, "emp"); // Instantiating Put class // accepts a row name. Put p = new Put(Bytes.toBytes("2")); // adding values using add() method // accepts column family name, qualifier/row name ,value p.add(Bytes.toBytes("personal data"), Bytes.toBytes("name"), Bytes.toBytes("praveen")); p.add(Bytes.toBytes("personal data"), Bytes.toBytes("city"), Bytes.toBytes("bangalore")); p.add(Bytes.toBytes("professional data"), Bytes.toBytes("designation"), Bytes.toBytes("software developer")); p.add(Bytes.toBytes("professional data"), Bytes.toBytes("salary"), Bytes.toBytes("50000")); // Saving the put Instance to the HTable. hTable.put(p); System.out.println("data inserted"); // closing HTable hTable.close(); } }
UpdateData.java
import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.util.Bytes; public class UpdateData { @SuppressWarnings("deprecation") public static void main(String[] args) throws IOException { // Instantiating Configuration class Configuration config = HBaseConfiguration.create(); // Instantiating HTable class HTable hTable = new HTable(config, "emp"); // Instantiating Put class // accepts a row name Put p = new Put(Bytes.toBytes("1")); // Updating a cell value p.add(Bytes.toBytes("personal data"), Bytes.toBytes("city"), Bytes.toBytes("hyderabad")); // Saving the put Instance to the HTable. hTable.put(p); System.out.println("data Updated"); // closing HTable hTable.close(); } }
RetriveData.java
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 { @SuppressWarnings("deprecation") 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("2")); // Reading the data Result result = table.get(g); // Reading values from Result class object byte[] value = result.getValue(Bytes.toBytes("personal data"), Bytes.toBytes("name")); byte[] value1 = result.getValue(Bytes.toBytes("personal data"), Bytes.toBytes("city")); // Printing the values String name = Bytes.toString(value); String city = Bytes.toString(value1); System.out.println("name: " + name +" "+ "city: " + city); table.close(); } }
DeleteData.java
import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.client.Delete; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.util.Bytes; public class DeleteData { @SuppressWarnings("deprecation") public static void main(String[] args) throws IOException { // Instantiating Configuration class Configuration conf = HBaseConfiguration.create(); // Instantiating HTable class HTable table = new HTable(conf, "emp"); // Instantiating Delete class Delete delete = new Delete(Bytes.toBytes("2")); delete.deleteColumn(Bytes.toBytes("personal data"), Bytes.toBytes("name")); delete.deleteFamily(Bytes.toBytes("professional data")); // deleting the data table.delete(delete); // closing the HTable object table.close(); System.out.println("data deleted..."); } }
ScanTable.java
import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.client.HTable; 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 ScanTable { @SuppressWarnings("deprecation") public static void main(String args[]) throws IOException { // Instantiating Configuration class Configuration config = HBaseConfiguration.create(); // Instantiating HTable class HTable table = new HTable(config, "emp"); // Instantiating the Scan class Scan scan = new Scan(); // Scanning the required columns scan.addColumn(Bytes.toBytes("personal data"), Bytes.toBytes("name")); scan.addColumn(Bytes.toBytes("personal data"), Bytes.toBytes("city")); // Getting the scan result ResultScanner scanner = table.getScanner(scan); // Reading values from scan result for (Result result = scanner.next(); result != null; result = scanner .next()) System.out.println("Found row : " + result); // closing the scanner scanner.close(); } }
Comments
Post a Comment