Support Questions
Find answers, ask questions, and share your expertise
Announcements
Alert: Welcome to the Unified Cloudera Community. Former HCC members be sure to read and learn how to activate your account here.

HBASE JAVA API issues

Solved Go to solution

HBASE JAVA API issues

Master Collaborator

I am running a simple HBASE JAva code to create a table but I am getting this error

.6.4.0-91/phoenix/phoenix-client.jar:/usr/hdp/2.6.4.0-91/phoenix/phoenix-server.jar://usr/hdp/2.6.4.0-91/hbase/conf/hbase-site.xml:.
18/06/08 16:19:41 INFO zookeeper.ZooKeeper: Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
18/06/08 16:19:41 INFO zookeeper.ZooKeeper: Client environment:java.io.tmpdir=/tmp
18/06/08 16:19:41 INFO zookeeper.ZooKeeper: Client environment:java.compiler=<NA>
18/06/08 16:19:41 INFO zookeeper.ZooKeeper: Client environment:os.name=Linux
18/06/08 16:19:41 INFO zookeeper.ZooKeeper: Client environment:os.arch=amd64
18/06/08 16:19:41 INFO zookeeper.ZooKeeper: Client environment:os.version=2.6.32-642.6.2.el6.x86_64
18/06/08 16:19:41 INFO zookeeper.ZooKeeper: Client environment:user.name=root
18/06/08 16:19:41 INFO zookeeper.ZooKeeper: Client environment:user.home=/root
18/06/08 16:19:41 INFO zookeeper.ZooKeeper: Client environment:user.dir=/root
18/06/08 16:19:41 INFO zookeeper.ZooKeeper: Initiating client connection, connectString=localhost:2181 sessionTimeout=90000 watcher=org.apache.hadoop.hbase.zookeeper.PendingWatcher@5158b42f
18/06/08 16:19:41 DEBUG zookeeper.ClientCnxn: zookeeper.disableAutoWatchReset is false
18/06/08 16:19:41 INFO zookeeper.ClientCnxn: Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
18/06/08 16:19:41 INFO zookeeper.ClientCnxn: Socket connection established, initiating session, client: /127.0.0.1:39668, server: localhost/127.0.0.1:2181
18/06/08 16:19:41 DEBUG zookeeper.ClientCnxn: Session establishment request sent on localhost/127.0.0.1:2181
18/06/08 16:19:41 INFO zookeeper.ClientCnxn: Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x163891d95120b7c, negotiated timeout = 60000
18/06/08 16:19:41 DEBUG zookeeper.ZooKeeperWatcher: hconnection-0x402bba4f0x0, quorum=localhost:2181, baseZNode=/hbase Received ZooKeeper Event, type=None, state=SyncConnected, path=null
18/06/08 16:19:41 DEBUG zookeeper.ZooKeeperWatcher: hconnection-0x402bba4f-0x163891d95120b7c connected
18/06/08 16:19:41 DEBUG zookeeper.ClientCnxn: Reading reply sessionid:0x163891d95120b7c, packet:: clientPath:null serverPath:null finished:false header:: 1,3  replyHeader:: 1,8595911618,-101  request:: '/hbase/hbaseid,F  response::
18/06/08 16:19:41 INFO client.ZooKeeperRegistry: ClusterId read in ZooKeeper is null
18/06/08 16:19:41 DEBUG client.ConnectionManager$HConnectionImplementation: clusterid came back null, using default default-cluster
18/06/08 16:19:41 DEBUG ipc.AbstractRpcClient: Codec=org.apache.hadoop.hbase.codec.KeyValueCodec@44c03695, compressor=null, tcpKeepAlive=true, tcpNoDelay=true, connectTO=10000, readTO=20000, writeTO=60000, minIdleTimeBeforeClose=120000, maxRetries=0, fallbackAllowed=false, bind address=null
18/06/08 16:19:41 DEBUG zookeeper.ClientCnxn: Reading reply sessionid:0x163891d95120b7c, packet:: clientPath:null serverPath:null finished:false header:: 2,4  replyHeader:: 2,8595911618,-101  request:: '/hbase/meta-region-server,F  response::
18/06/08 16:19:41 DEBUG zookeeper.ZKUtil: hconnection-0x402bba4f-0x163891d95120b7c, quorum=localhost:2181, baseZNode=/hbase Unable to get data of znode /hbase/meta-region-server because node does not exist (not an error)
18/06/08 16:19:41 DEBUG zookeeper.ClientCnxn: Reading reply sessionid:0x163891d95120b7c, packet:: clientPath:null serverPath:null finished:false header:: 3,4  replyHeader:: 3,8595911618,-101  request:: '/hbase/meta-region-server,F  response::
18/06/08 16:19:41 DEBUG zookeeper.ZKUtil: hconnection-0x402bba4f-0x163891d95120b7c, quorum=localhost:2181, baseZNode=/hbase Unable to get data of znode /hbase/meta-region-server because node does not exist (not an error)
18/06/08 16:19:41 DEBUG zookeeper.ClientCnxn: Reading reply sessionid:0x163891d95120b7c, packet:: clientPath:null serverPath:null finished:false header:: 4,4  replyHeader:: 4,8595911618,-101  request:: '/hbase/meta-region-server,F  response::
18/06/08 16:19:41 DEBUG zookeeper.ZKUtil: hconnection-0x402bba4f-0x163891d95120b7c, quorum=localhost:2181, baseZNode=/hbase Unable to get data of znode /hbase/meta-region-server because node does not exist (not an error)
18/06/08 16:19:42 DEBUG zookeeper.ClientCnxn: Reading reply sessionid:0x163891d95120b7c, packet:: clientPath:null serverPath:null finished:false header:: 5,4  replyHeader:: 5,8595911618,-101  request:: '/hbase/meta-region-server,F  response::
18/06/08 16:19:42 DEBUG zookeeper.ZKUtil: hconnection-0x402bba4f-0x163891d95120b7c, quorum=localhost:2181, baseZNode=/hbase Unable to get data of znode /hbase/meta-region-server because node does not exist (not an error)
18/06/08 16:19:42 DEBUG zookeeper.ClientCnxn: Reading reply sessionid:0x163891d95120b7c, packet:: clientPath:null serverPath:null finished:false header:: 6,4  replyHeader:: 6,8595911622,-101  request:: '/hbase/meta-region-server,F  response::
18/06/08 16:19:42 DEBUG zookeeper.ZKUtil: hconnection-0x402bba4f-0x163891d95120b7c, quorum=localhost:2181, baseZNode=/hbase Unable to get data of znode /hbase/meta-region-server because node does not exist (not an error)
18/06/08 16:19:42 DEBUG zookeeper.ClientCnxn: Reading reply sessionid:0x163891d95120b7c, packet:: clientPath:null serverPath:null finished:false header:: 7,4  replyHeader:: 7,8595911622,-101  request:: '/hbase/meta-region-server,F  response::
18/06/08 16:19:42 DEBUG zookeeper.ZKUtil: hconnection-0x402bba4f-0x163891d95120b7c, quorum=localhost:2181, baseZNode=/hbase Unable to get data of znode /hbase/meta-region-server because node does not exist (not an error)
18/06/08 16:19:42 DEBUG zookeeper.ClientCnxn: Reading reply sessionid:0x163891d95120b7c, packet:: clientPath:null serverPath:null finished:false header:: 8,4  replyHeader:: 8,8595911622,-101  request:: '/hbase/meta-region-server,F  response::
18/06/08 16:19:42 DEBUG zookeeper.ZKUtil: hconnection-0x402bba4f-0x163891d95120b7c, quorum=localhost:2181, baseZNode=/hbase Unable to get data of znode /hbase/meta-region-server because node does not exist (not an error)
18/06/08 16:19:42 DEBUG zookeeper.ClientCnxn: Reading reply sessionid:0x163891d95120b7c, packet:: clientPath:null serverPath:null finished:false header:: 9,4  replyHeader:: 9,8595911622,-101  request:: '/hbase/meta-region-server,F  response::
18/06/08 16:19:42 DEBUG zookeeper.ZKUtil: hconnection-0x402bba4f-0x163891d95120b7c, quorum=localhost:2181, baseZNode=/hbase Unable to get data of znode /hbase/meta-region-server because node does not exist (not an error)
18/06/08 16:19:43 DEBUG zookeeper.ClientCnxn: Reading reply sessionid:0x163891d95120b7c, packet:: clientPath:null serverPath:null finished:false header:: 10,4  replyHeader:: 10,8595911622,-101  request:: '/hbase/meta-region-server,F  response::
18/06/08 16:19:43 DEBUG zookeeper.ZKUtil: hconnection-0x402bba4f-0x163891d95120b7c, quorum=localhost:2181, baseZNode=/hbase Unable to get data of znode /hbase/meta-region-server because node does not exist (not an error)
18/06/08 16:19:43 DEBUG zookeeper.ClientCnxn: Reading reply sessionid:0x163891d95120b7c, packet:: clientPath:null serverPath:null finished:false header:: 11,4  replyHeader:: 11,8595911624,-101  request
1 ACCEPTED SOLUTION

Accepted Solutions

Re: HBASE JAVA API issues

Super Mentor

@Sami Ahmad

I made some changes to your code, it should look like following to work properly:

import java.io.IOException;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.conf.Configuration;

public class CreateHbaseTable {

        public void createtable(String name, String[] colfamily)throws Exception {
                Configuration config = HBaseConfiguration.create();
                config.set("hbase.zookeeper.quorum", "hadoop1,hadoop2");
                config.set("hbase.zookeeper.property.clientPort", "2181");
                config.set("hbase.cluster.distributed", "true");
                config.set("zookeeper.znode.parent", "/hbase-unsecure");
                HBaseAdmin admin = new HBaseAdmin(config);
                HTableDescriptor des = new HTableDescriptor(Bytes.toBytes(name));
                for (int i = 0; i < colfamily.length; i++) {
                        des.addFamily(new HColumnDescriptor(colfamily[i]));
                }
                if (admin.tableExists(name)) {
                        System.out.println("Table already exist");
                } else {
                        admin.createTable(des);
                        System.out.println("Table: " + name + " Sucessfully created");
                }
        }

        public static void main(String args[]) throws Exception {
                CreateHbaseTable op = new CreateHbaseTable();
                String tablename = "Acadgild";
                String[] familys = { "Emp_name", "sal" };
                op.createtable(tablename, familys);
        }
}

.

Now in order to compile and run this code please do this:

# export CLASSPATH=`hbase classpath`:.
# javac CreateHbaseTable.java 
# java CreateHbaseTable

OUTPUT:
.
.
2018-06-09 00:40:18,504 INFO  [main] client.HBaseAdmin: Created Acadgild
Table: Acadgild Sucessfully created

.

Additionally if you want to use Maven Based Simple HBase client then please refer to:

https://github.com/jaysensharma/MiddlewareMagicDemos/tree/master/HDP_Ambari/HBase/HBase_Client

9 REPLIES 9

Re: HBASE JAVA API issues

Either the zookeeper or znode is incorrect.
* parent znode for hdp is generally /hbase-unsecure or /hbase-secure (not /hbase)

* check you are giving the right zookeeper address while connecting.

If you are not specifying them manually, then make sure that right hbase-site.xml is in your application classpath.

Re: HBASE JAVA API issues

Master Collaborator

where in this code I will specify the parent znode? are there any examples for HBASE JAVA api ?

also I am specifying the hbase-site.xml in classpath as follows :

java  -classpath /usr/hdp/2.6.4.0-91/hbase/lib/hbase-client-1.1.2.2.6.4.0-91.jar:/usr/hdp/2.6.4.0-91/phoenix/*:/usr/hdp/2.6.4.0-91/hbase/conf/hbase-site.xml:.  CreateHbaseTable
[root@hadoop1 ~]# vi CreateHbaseTable.java
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.util.Bytes;
public class CreateHbaseTable {
Configuration config = HBaseConfiguration.create();
public void createtable(String name,String[] colfamily) throws MasterNotRunningException,
                                               ZooKeeperConnectionException, IOException
{
HBaseAdmin admin = new HBaseAdmin(config);
HTableDescriptor des = new HTableDescriptor(Bytes.toBytes(name));
for(int i=0;i<colfamily.length;i++){
des.addFamily(new HColumnDescriptor(colfamily[i]));
}
if(admin.tableExists(name)){
System.out.println("Table already exist");
}
else{
admin.createTable(des);
System.out.println("Table: "+name+ " Sucessfully created");
}
}
public static void main(String args[]) throws MasterNotRunningException,
                               ZooKeeperConnectionException,IOException{
CreateHbaseTable op = new CreateHbaseTable();
String tablename = "Acadgild";
String[] familys = {"Emp_name","sal"};
op.createtable(tablename, familys);
}
}

<br>

Re: HBASE JAVA API issues

Configuration config = HBaseConfiguration.create();

Either you can add them config.set("zookeeper.znode.parent","/hbase-unsecure") and config.set("hbase.zookeeper.quorum","<server-name>)
Or you can directly include server hbase-site.xml in your application classpath so that HBaseConfiguration.create() will add them automatically.

Re: HBASE JAVA API issues

Master Collaborator

I tried to add it but its giving compilation error.

I showed the command using server based hbase-site.xml. . why its not adding it?

Configuration config = HBaseConfiguration.create();
config.set("zookeeper.znode.parent","/hbase-unsecure");
Highlighted

Re: HBASE JAVA API issues

you can't keep statement outside of method or constructor except declaration, static blocks in java.

Re: HBASE JAVA API issues

Master Collaborator

this code is giving me compilation error ,whats wrong?

[root@hadoop1 ~]# javac  -classpath /usr/hdp/2.6.4.0-91/hbase/lib/hbase-client-1.1.2.2.6.4.0-91.jar:/usr/hdp/2.6.4.0-91/phoenix/*:/usr/hdp/2.6.4.0-91/hbase/conf/hbase-site.xml:.  CreateHbaseTable.java
CreateHbaseTable.java:12: error: <identifier> expected
config.set("zookeeper.znode.parent","/hbase-unsecure");
          ^
CreateHbaseTable.java:12: error: illegal start of type
config.set("zookeeper.znode.parent","/hbase-unsecure");
           ^
CreateHbaseTable.java:12: error: illegal start of type
config.set("zookeeper.znode.parent","/hbase-unsecure");
                                    ^
CreateHbaseTable.java:13: error: <identifier> expected
config.set("hbase.zookeeper.quorum", "hadoop1,hadoop2");
          ^
CreateHbaseTable.java:13: error: illegal start of type
config.set("hbase.zookeeper.quorum", "hadoop1,hadoop2");
           ^
CreateHbaseTable.java:13: error: illegal start of type
config.set("hbase.zookeeper.quorum", "hadoop1,hadoop2");
[root@hadoop1 ~]# more CreateHbaseTable.java
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.util.Bytes;
public class CreateHbaseTable {
Configuration config = HBaseConfiguration.create();
config.set("zookeeper.znode.parent","/hbase-unsecure");
config.set("hbase.zookeeper.quorum", "hadoop1,hadoop2");
config.set("hbase.zookeeper.property.clientPort", "2181");
config.set("hbase.cluster.distributed", "true");
config.set("zookeeper.znode.parent","/hbase-unsecure");
public void createtable(String name,String[] colfamily) throws MasterNotRunningException,
                                               ZooKeeperConnectionException, IOException
{
HBaseAdmin admin = new HBaseAdmin(config);
HTableDescriptor des = new HTableDescriptor(Bytes.toBytes(name));
for(int i=0;i<colfamily.length;i++){
des.addFamily(new HColumnDescriptor(colfamily[i]));
}
if(admin.tableExists(name)){
System.out.println("Table already exist");
}
else{
admin.createTable(des);
System.out.println("Table: "+name+ " Sucessfully created");
}
}
public static void main(String args[]) throws MasterNotRunningException,
                               ZooKeeperConnectionException,IOException{
CreateHbaseTable op = new CreateHbaseTable();
String tablename = "Acadgild";
String[] familys = {"Emp_name","sal"};
op.createtable(tablename, familys);
}
}
You have new mail in /var/spool/mail/root


Re: HBASE JAVA API issues

You need to add the directory containing hbase-site.xml that you want, not the file itself. This is how Java works. You specify jar files, an asterisk wildcard, or a directory.

Re: HBASE JAVA API issues

Super Mentor

@Sami Ahmad

I made some changes to your code, it should look like following to work properly:

import java.io.IOException;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.conf.Configuration;

public class CreateHbaseTable {

        public void createtable(String name, String[] colfamily)throws Exception {
                Configuration config = HBaseConfiguration.create();
                config.set("hbase.zookeeper.quorum", "hadoop1,hadoop2");
                config.set("hbase.zookeeper.property.clientPort", "2181");
                config.set("hbase.cluster.distributed", "true");
                config.set("zookeeper.znode.parent", "/hbase-unsecure");
                HBaseAdmin admin = new HBaseAdmin(config);
                HTableDescriptor des = new HTableDescriptor(Bytes.toBytes(name));
                for (int i = 0; i < colfamily.length; i++) {
                        des.addFamily(new HColumnDescriptor(colfamily[i]));
                }
                if (admin.tableExists(name)) {
                        System.out.println("Table already exist");
                } else {
                        admin.createTable(des);
                        System.out.println("Table: " + name + " Sucessfully created");
                }
        }

        public static void main(String args[]) throws Exception {
                CreateHbaseTable op = new CreateHbaseTable();
                String tablename = "Acadgild";
                String[] familys = { "Emp_name", "sal" };
                op.createtable(tablename, familys);
        }
}

.

Now in order to compile and run this code please do this:

# export CLASSPATH=`hbase classpath`:.
# javac CreateHbaseTable.java 
# java CreateHbaseTable

OUTPUT:
.
.
2018-06-09 00:40:18,504 INFO  [main] client.HBaseAdmin: Created Acadgild
Table: Acadgild Sucessfully created

.

Additionally if you want to use Maven Based Simple HBase client then please refer to:

https://github.com/jaysensharma/MiddlewareMagicDemos/tree/master/HDP_Ambari/HBase/HBase_Client

Re: HBASE JAVA API issues

Master Collaborator
awesome it worked . thanks