Skip to main content

Apache HBase Client Java API Examples

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.
$ sudo gedit $HOME/.bashrc
$HOME/.bashrc file. Add the following line
export CLASSPATH=$CLASSPATH:/usr/local/hbase/lib/*
Step 2 Reload your changed $HOME/.bashrc settings
$ source $HOME/.bashrc
Step 3 - Change the directory to /usr/local/hbase
$ cd /usr/local/hbase
Step 4 - To start up the initial HBase cluster.
$ ./start-hbase.sh
Add all HBase libraries to your java project's class path.
/usrlocal/hbase/lib/*
Inserting Data into Table Using java API (See, how to create tables.).
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();
 }
}
Updating Data into Table Using java API
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();
 }
}
Retrieving Data from Table Using java API
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();
 }
}
Deleting Data from Table Using java API
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...");
 }
}
Scan Data Using java API
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

Popular posts from this blog

Apache Spark WordCount scala example

Apache Spark is an open source cluster computing framework. Originally developed at the University of California, Berkeley's AMPLab, the Spark codebase was later donated to the Apache Software Foundation, which has maintained it since. Spark provides an interface for programming entire clusters with implicit data parallelism and fault-tolerance. Pre Requirements 1) A machine with Ubuntu 14.04 LTS operating system 2) Apache Hadoop 2.6.4 pre installed ( How to install Hadoop on Ubuntu 14.04 ) 3) Apache Spark 1.6.1 pre installed ( How to install Spark on Ubuntu 14.04 ) Spark WordCount Scala Example Step 1 - Change the directory to /usr/local/spark/sbin. $ cd /usr/local/spark/sbin Step 2 - Start all spark daemons. $ ./start-all. sh Step 3 - The JPS (Java Virtual Machine Process Status Tool) tool is limited to reporting information on JVMs for which it has the access permissions. $ jp...

Hive hiveserver2 and Web UI usage

Hive hiveserver2 and Web UI usage HiveServer2 (HS2) is a server interface that enables remote clients to execute queries against Hive and retrieve the results (a more detailed intro here). The current implementation, based on Thrift RPC, is an improved version of HiveServer and supports multi-client concurrency and authentication. It is designed to provide better support for open API clients like JDBC and ODBC. Step 1 - Change the directory to /usr/local/hive/bin $ cd $HIVE_HOME/bin Step 2 - Start hiveserver2 daemon $ hiveserver2 OR $ hive --service hiveserver2 & Step 3 - You can browse to hiveserver2 web ui at following url http: //localhost:10002/hiveserver2.jsp Step 4 - You can see the hive logs in /tmp/hduser/hive. log To kill hiveserver2 daemon $ ps -ef | grep -i hiveserver2 $ kill - 9 29707 OR $ rm -rf /var/run/hive/hive...

Apache Spark Shell Usage

Apache Spark is an open source cluster computing framework. Originally developed at the University of California, Berkeley's AMPLab, the Spark codebase was later donated to the Apache Software Foundation, which has maintained it since. Spark provides an interface for programming entire clusters with implicit data parallelism and fault-tolerance. Pre Requirements 1) A machine with Ubuntu 14.04 LTS operating system 2) Apache Hadoop 2.6.4 pre installed ( How to install Hadoop on Ubuntu 14.04 ) 3) Apache Spark 1.6.1 pre installed ( How to install Spark on Ubuntu 14.04 ) Spark Shell Usage The Spark shell provides an easy and convenient way to prototype certain operations quickly, without having to develop a full program, packaging it and then deploying it. Step 1 - Change the directory to /usr/local/hadoop/sbin. $ cd /usr/local/hadoop/sbin Step 2 - Start all hadoop daemons. $ ./start-all. sh ...