一、背景:
在ubuntu中已经正确安装了hbase
0.94版本,通过./hbase
shell也能够正确访问hbase数据库了,此时想要通过java的api调用去访问数据库。
在idea中创建了maven工程,里面pom文件的配置信息如下:
[dependencies>
[dependency>
[groupId>org.apache.hbase[/groupId>
[artifactId>hbase[/artifactId>
[version>0.94.16[/version>
[/dependency>
[dependency>
[groupId>org.apache.hadoop[/groupId>
[artifactId>hadoop-hdfs[/artifactId>
[version>2.6.0[/version>
[/dependency>
[dependency>
[groupId>org.apache.hadoop[/groupId>
[artifactId>hadoop-common[/artifactId>
[version>2.6.0[/version>
[/dependency>
[/dependencies>
然后执行程序后显示如下信息,卡在这里一直不动
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.
二、问题定位和解决
1、java程序如下:
public static void main(String[] args){
try{
Configuration HBASE_CONFIG = HBaseConfiguration.create();
HBASE_CONFIG.set("hbase.zookeeper.quorum", "hbasestudy");
System.out.println("good1");
HBaseAdmin
admin = new HBaseAdmin(HBASE_CONFIG);
System.out.println("good2");
if(admin.tableExists("te")){
System.out.println("te" + "
exists!");
}else{
HTableDescriptor tableDesc =
new HTableDescriptor("te");
tableDesc.addFamily(new
HColumnDescriptor("co"));
admin.createTable(tableDesc);
System.out.println("create
table success!");
}
}catch(IOException e){
e.printStackTrace();
}
}
2、首先定位到是HBaseAdmin(xx)的这个位置出问题了,通过debug模式一直访问内部代码,最后定位到是HConnectionManager.java文件的如下位置出错了。
for (int tries = 0;
!this.closed && this.master == null && tries <
numRetries;
tries++) {
try {
sn = masterAddressTracker.getMasterAddress();
if (sn == null) {
LOG.info("ZooKeeper available but no active master location
found");
throw new MasterNotRunningException();
}
InetSocketAddress isa =
new InetSocketAddress(sn.getHostname(), sn.getPort());
HMasterInterface tryMaster = rpcEngine.getProxy(
HMasterInterface.class, HMasterInterface.VERSION, isa,
this.conf,
this.rpcTimeout);
if (tryMaster.isMasterRunning()) {
this.master = tryMaster;
this.masterLock.notifyAll();
break;
}
} catch (IOException e) {
if
(!shouldRetryGetMaster(tries, e)) break;
} catch (UndeclaredThrowableException ute) {
if (!shouldRetryGetMaster(tries, ute)) break;
}
3、当执行到上面第2个红色的时候,查看IDEA的调试面板,看到如下信息
detailMessage="Could not set up IO Streams"
cause=java.lang.NoSuchMethodError:
org.apache.hadoop.net.NetUtils.getInputStream(LLjava/io/InputStream;
也就是说找不到某个函数的定义,导致rpc访问hbase的时候报错了。
于是乎我将hadoop的版本从0.26->1.1.2->2.5.0->2.6.0,但是始终会报这个错误,郁闷!!
4、通过翻阅web海量查找,发现这样一个临时方案。
hbase目录下有个lib文件,只要将这个lib引入idea的libraries即可。
于是我尝试后发现果然成功了。
加载中,请稍候......