Reply
Explorer
Posts: 16
Registered: ‎08-13-2013
Accepted Solution

Inserting data into Hbase table through JAVA

[ Edited ]

Hello,

 

I am trying to insert data into hbase table though hive table. Though command line , i am able to create and insert data into hbase table through hive.

My query for creating & inserting data into hbase is given below:

 

CREATE TABLE hbase_test5(key int COMMENT 'key', value string COMMENT 'value') STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,cf1:value") TBLPROPERTIES ("hbase.table.name" = "hbase_test5");

 

INSERT OVERWRITE TABLE hbase_test4 SELECT * FROM testhivehbase WHERE key=1;

 

testhivehbase is a table creted in HIVE.

 

 

Now I am trying to create & insert data into hbse table through jdbc.

 

My Java Program is given below:

 

package Hive;

import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;

public class Hbasehive {

private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";

/**
* @param args
* @throws SQLException
*/
public static void main(String[] args) throws SQLException {
try {
System.out.println("hi");
Class.forName(driverName);
Connection con = DriverManager.getConnection("jdbc:hive://192.168.1.177:10001/default", "", "");
System.out.println("hello");
Statement stmt = con.createStatement();
stmt.executeQuery("drop table hbase_test5");
String sql = "CREATE TABLE hbase_test5(key int COMMENT 'key', value string COMMENT 'value') STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' WITH SERDEPROPERTIES (\"hbase.columns.mapping\" = \":key,cf1:value\") TBLPROPERTIES (\"hbase.table.name\" = \"hbase_test5\")";
System.out.println("Running: " + sql);
ResultSet res = stmt.executeQuery(sql);
System.out.println("Create Table Successfully");
String sql1 = "INSERT OVERWRITE TABLE hbase_test5 SELECT * FROM testhivehbase WHERE key=1";
System.out.println("Running: " + sql1);
ResultSet res1 = stmt.executeQuery(sql1);
System.out.println("Inserted Successfully");
}
catch (ClassNotFoundException e) {
System.out.println("--->"+e.getMessage());
e.printStackTrace();
System.exit(1);
}
System.out.println("Create Table-Successful");
// load data into table
// NOTE: filepath has to be local to the hive server
// NOTE: /tmp/a.txt is a ctrl-A separated file with two fields per line
}
}

 

 

Error:

 

Diagnostic Messages for this Task:
java.lang.RuntimeException: Error in configuring object
    at org.apache.hadoop.util.ReflectionUtils.setJobConf(ReflectionUtils.java:106)
    at org.apache.hadoop.util.ReflectionUtils.setConf(ReflectionUtils.java:72)
    at org.apache.hadoop.util.ReflectionUtils.newInstance(ReflectionUtils.java:130)
    at org.apache.hadoop.mapred.MapTask.runOldMapper(MapTask.java:413)
    at org.apache.hadoop.mapred.MapTask.run(MapTask.java:332)
    at org.apache.hadoop.mapred.Child$4.run(Child.java:268)
    at java.security.AccessController.doPrivileged(Native Method)
    at javax.security.auth.Subject.doAs(Subject.java:396)
    at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1408)
    at org.apache.hadoop.mapred.Child.main(Child.java:262)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.ja

FAILED: Execution Error, return code 2 from org.apache.hadoop.hive.ql.exec.MapRedTask
MapReduce Jobs Launched:
Job 0: Map: 1   HDFS Read: 0 HDFS Write: 0 FAIL
Total MapReduce CPU Time Spent: 0 msec

 

 

Please help me by providing some solution.

 

Thanks & Regards

Surbhi Singh

Expert Contributor
Posts: 63
Registered: ‎08-06-2013

Re: Inserting data into Hbase table through JAVA

Add the following JARs to the classpath of the application. The version may be different.

 

/opt/hive/lib/hive-hbase-handler-0.8.0-SNAPSHOT.jar
/opt/hive/lib/hbase-0.90.1-SNAPSHOT.jar
/opt/hive/lib/zookeeper-3.3.1.jar
/hbase-0.94.1/hbase-0.94.6.jar
hive_contrib.jar

 

Highlighted
Expert Contributor
Posts: 63
Registered: ‎08-06-2013

Re: Inserting data into Hbase table through JAVA

Add the 

/opt/hive/lib/hbase-0.90.1-SNAPSHOT.jar

 

instead of the 
/hbase-0.94.1/hbase-0.94.6.jar

 

Add only one of the hbase JARs.

Explorer
Posts: 16
Registered: ‎08-13-2013

Re: Inserting data into Hbase table through JAVA

Above problem has been solved by adding auxpath in hive-site.xml

Announcements