0
votes

I am getting the below error message when i try to create table via java code. no issues when doing this via Hbase shell. I am using pseudo distributed cluster.

Exception in thread "main" org.apache.hadoop.hbase.MasterNotRunningException:

See log file:

2014-06-09 22:11:36,213 INFO org.apache.zookeeper.server.NIOServerCnxnFactory: Accepted socket connection from /0:0:0:0:0:0:0:1:60805 2014-06-09 22:11:36,217 WARN org.apache.zookeeper.server.ZooKeeperServer: Connection request from old client /0:0:0:0:0:0:0:1:60805; will be dropped if server is in r-o mode 2014-06-09 22:11:36,217 INFO org.apache.zookeeper.server.ZooKeeperServer: Client attempting to establish new session at /0:0:0:0:0:0:0:1:60805 2014-06-09 22:11:36,219 INFO org.apache.zookeeper.server.ZooKeeperServer: Established session 0x146817ea6ff0003 with negotiated timeout 40000 for client /0:0:0:0:0:0:0:1:60805 2014-06-09 22:12:15,768 WARN org.apache.zookeeper.server.NIOServerCnxn: caught end of stream exception EndOfStreamException: Unable to read additional data from client sessionid 0x146817ea6ff0003, likely client has closed socket at org.apache.zookeeper.server.NIOServerCnxn.doIO(NIOServerCnxn.java:220) at org.apache.zookeeper.server.NIOServerCnxnFactory.run(NIOServerCnxnFactory.java:208) at java.lang.Thread.run(Thread.java:722) 2014-06-09 22:12:15,778 INFO org.apache.zookeeper.server.NIOServerCnxn: Closed socket connection for client /0:0:0:0:0:0:0:1:60805 which had sessionid 0x146817ea6ff0003

package client;


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;
// ^^ PutExample
import util.HBaseHelper;
// vv PutExample

import java.io.IOException;

public class PutExample {

  public static void main(String[] args) throws IOException {
    Configuration conf = HBaseConfiguration.create(); // co PutExample-1-CreateConf Create the required configuration.

    System.out.println("Start...");
    // ^^ PutExample
    HBaseHelper helper = HBaseHelper.getHelper(conf);
    helper.dropTable("mytable");
    helper.createTable("mytable", "colfam1");
    // vv PutExample
    HTable table = new HTable(conf, "mytable"); // co PutExample-2-NewTable Instantiate a new client.

    Put put = new Put(Bytes.toBytes("row1")); // co PutExample-3-NewPut Create put with specific row.

    put.add(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"),
      Bytes.toBytes("val1")); // co PutExample-4-AddCol1 Add a column, whose name is "colfam1:qual1", to the put.
    put.add(Bytes.toBytes("colfam1"), Bytes.toBytes("qual2"),
      Bytes.toBytes("val2")); // co PutExample-4-AddCol2 Add another column, whose name is "colfam1:qual2", to the put.

    table.put(put); // co PutExample-5-DoPut Store row with column into the HBase table.
    System.out.println("End...");
  }
}
// ^^ PutExample
1

1 Answers

0
votes

You can do it by using HBase admin, it will work fine for you. Code for creating table and column family using HBaseAdmin is given below.

    Configuration configuration = HBaseConfiguration.create();
    configuration.set("hbase.master", host + ":" + port);// host and port of hbase master.
    configuration.set("hbase.zookeeper.quorum", zookeeper-hostname);// ip where zookeeper is running.
    configuration.set("hbase.zookeeper.property.clientPort", zookeeper-port);// port on ehich zookeeper is runnig.
    HBaseAdmin admin = new HBaseAdmin(configuration);

    if (admin.isTableEnabled("mytable"))
    {
        admin.disableTable("mytable");
    }

    admin.deleteTable("tableName");

    HTableDescriptor tableDescriptor = new HTableDescriptor("mytable");

    HColumnDescriptor hColumnDescriptor = new HColumnDescriptor("colfam1");

    tableDescriptor.addFamily(hColumnDescriptor);

    admin.createTable(tableDescriptor);

then after you can put the data in hbase table.

your existing code for put will work fine.

I hope it will work for you.