通过java代码获取MasterNotRunningException

时间:2014-06-09 16:49:00

标签: java hadoop hbase

当我尝试通过java代码创建表时,我收到以下错误消息。通过Hbase shell执行此操作时没有问题。我正在使用伪分布式集群。

线程“main”中的异常org.apache.hadoop.hbase.MasterNotRunningException:

请参阅日志文件:

2014-06-09 22:11:36,213 INFO org.apache.zookeeper.server.NIOServerCnxnFactory:来自/ 0的接受套接字连接:0:0:0:0:0:0:1:60805 2014-06-09 22:11:36,217 WARN org.apache.zookeeper.server.ZooKeeperServer:来自旧客户端的连接请求/ 0:0:0:0:0:0:0:1:60805;如果服务器处于r-o模式,将被删除 2014-06-09 22:11:36,217 INFO org.apache.zookeeper.server.ZooKeeperServer:尝试在/ 0处建立新会话的客户端:0:0:0:0:0:0:1:60805 2014-06-09 22:11:36,219 INFO org.apache.zookeeper.server.ZooKeeperServer:建立会话0x146817ea6ff0003,协商超时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:抓住了流结束异常 EndOfStreamException:无法从客户端sessionid 0x146817ea6ff0003读取其他数据,可能客户端已关闭套接字     在org.apache.zookeeper.server.NIOServerCnxn.doIO(NIOServerCnxn.java:220)     在org.apache.zookeeper.server.NIOServerCnxnFactory.run(NIOServerCnxnFactory.java:208)     在java.lang.Thread.run(Thread.java:722) 2014-06-09 22:12:15,778 INFO org.apache.zookeeper.server.NIOServerCnxn:客户端/ 0的封闭套接字连接:0:0:0:0:0:0:1:60805,其中包含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 个答案:

答案 0 :(得分:0)

您可以使用HBase admin来完成它,它可以正常工作。下面给出了使用HBaseAdmin创建表和列族的代码。

    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);

然后你可以把数据放在hbase表中。

现有的put代码可以正常使用。

我希望它对你有用。