我的问题是我尝试使用Java连接到独立的HBase实例,程序只是无限期挂起。
我在运行OS X 10.8.5的iMac上设置了HBase 0.96.1.1-hadoop2和Hadoop 2.2.0的新实例。似乎两者都运行正常,因为我可以正常访问web ui(例如,localhost:60010 for HBase)。
运行jps给了我这个:
bash-3.2$ jps
77164 SecondaryNameNode
81089 Jps
80685 HMaster
77360 NodeManager
77273 ResourceManager
77061 DataNode
76975 NameNode
我可以使用HBase shell创建和扫描表,如HBase快速入门指南中的示例所示。我也在同一个用户运行Hbase和Hadoop(不确定这是否重要)。
这是我试图运行的Java程序:
package com.mypackage.dao.hbase;
import java.io.IOException;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HConnection;
import org.apache.hadoop.hbase.client.HConnectionManager;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.conf.Configuration;
public class HBaseDao {
private static final String DICTIONARY_TABLE = "mytable";
private static final String DICTIONARY_CF = "mycf";
private static final String DICTIONARY_CF_TYPE = "type";
private final Configuration conf;
public HBaseDao() throws IOException {
Configuration config = HBaseConfiguration.create();
config.clear();
config.set("hbase.zookeeper.quorum", "localhost");
config.set("hbase.zookeeper.property.clientPort","2181");
config.set("hbase.master", "localhost:60000");
config.addResource(new Path("/usr/local/hbase/hbase/conf/hbase-site.xml"));
this.conf = config;
}
public void addNameToTable(HTableInterface table, String name, String type) throws IOException {
Put p = new Put(Bytes.toBytes(name));
p.add(Bytes.toBytes(DICTIONARY_CF),
Bytes.toBytes(DICTIONARY_CF_TYPE),
Bytes.toBytes(type));
System.out.println("About to put");
table.put(p);
}
public void loadDictionaryIntoTable() throws IOException {
HConnection connection = HConnectionManager.createConnection(this.conf);
HTableInterface table = connection.getTable(DICTIONARY_TABLE);
try {
// Use the table as needed, for a single operation and a single thread
String s = "TestName2";
String t = "TestType2";
addNameToTable(table,s,t);
} finally {
System.out.println("Got here");
table.close();
connection.close();
}
}
public static void main(String[] args) throws IOException {
HBaseDao dao = new HBaseDao();
dao.loadDictionaryIntoTable();
}
}
最后,我在我的pom文件中有这种依赖:
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>0.96.1.1-hadoop2</version>
</dependency>
我看到的输出是:
Ok we started
log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
SLF4J: The requested version 1.6 by your slf4j binding is not compatible with [1.5.5, 1.5.6]
SLF4J: See http://www.slf4j.org/codes.html#version_mismatch for further details.
2014-01-28 16:19:48.453 java[81102:1903] Unable to load realm info from SCDynamicStore
它只是无限期挂起......
答案 0 :(得分:1)
从代码中删除config.clear();
,看看它是否有帮助。此外,由于客户端从ZK仲裁中获取有关HM的信息,因此不需要config.set("hbase.master", "localhost:60000");
。
如果问题仍然存在,请向我们显示HM日志文件。
答案 1 :(得分:0)
我通过意识到我的Zookeeper的clientPort是2183而不是2181来解决这个问题...我在HBase web UI localhost:60010上注意到了这一点,我在hbase-site.xml中确认了这一点。