【问题标题】:Getting MasterNotRunningException via java code通过 java 代码获取 MasterNotRunningException
【发布时间】:2014-06-09 16:48:59
【问题描述】:

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

线程“主”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 警告 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 信息 org.apache.zookeeper.server.ZooKeeperServer:已建立会话 0x146817ea6ff0003,客户端协商超时 40000 /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

【问题讨论】:

    标签: java hadoop hbase


    【解决方案1】:

    你可以使用 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 代码可以正常工作。

    我希望它对你有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-21
      • 1970-01-01
      • 1970-01-01
      • 2011-03-03
      • 2013-12-02
      • 1970-01-01
      • 2016-01-08
      • 2019-03-30
      相关资源
      最近更新 更多