Eclipse调试Hbase程序“Failed to detect a valid hadoop home directory java.id.IOException”问题

时间:2022-04-08 08:26:49
在eclipse调试hbase程序时发现如下日志:
2014-11-21 16:27:56,030 DEBUG [org.apache.hadoop.util.Shell] - Failed to detect a valid hadoop home directory
java.io.IOException   : HADOOP_HOME or hadoop.home.dir are not set.
      at org.apache.hadoop.util.Shell.checkHadoopHome(   Shell.java:225 )
      at org.apache.hadoop.util.Shell.<clinit>(   Shell.java:250 )
      at org.apache.hadoop.util.StringUtils.<clinit>( StringUtils.java:76 )
      at org.apache.hadoop.conf.Configuration.getStrings( Configuration.java:1514 )
      at org.apache.hadoop.hbase.zookeeper.ZKConfig.makeZKProps( ZKConfig.java:113 )
      at org.apache.hadoop.hbase.zookeeper.ZKConfig.getZKQuorumServersString( ZKConfig.java:265 )
      at org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher.<init>( ZooKeeperWatcher.java:159 )
      at org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher.<init>( ZooKeeperWatcher.java:134 )
      at org.apache.hadoop.hbase.client.ZooKeeperKeepAliveConnection.<init>( ZooKeeperKeepAliveConnection.java:43 )
      at org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation.getKeepAliveZooKeeperWatcher( HConnectionManager.java:1763 )
      at org.apache.hadoop.hbase.client.ZooKeeperRegistry.getClusterId( ZooKeeperRegistry.java:82 )
      at org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation.retrieveClusterId( HConnectionManager.java:852 )
      at org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation.<init>( HConnectionManager.java:657 )
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(   Native Method )
      at sun.reflect.NativeConstructorAccessorImpl.newInstance( NativeConstructorAccessorImpl.java:39 )
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance( DelegatingConstructorAccessorImpl.java:27 )
      at java.lang.reflect.Constructor.newInstance(   Constructor.java:513 )
      at org.apache.hadoop.hbase.client.HConnectionManager.createConnection( HConnectionManager.java:409 )
      at org.apache.hadoop.hbase.client.HConnectionManager.createConnection( HConnectionManager.java:304 )
      at com.emar.adx.yiqifa.dal.common.hbase.HBaseConnectionFactory.getConnection( HBaseConnectionFactory.java:54 )
      at com.emar.adx.yiqifa.dal.common.hbase.HBaseUtils.getConnnection( HBaseUtils.java:34 )
      at com.emar.adx.yiqifa.dal.common.hbase.HBaseUtilsTest.testGetTable( HBaseUtilsTest.java:39 )
      at sun.reflect.NativeMethodAccessorImpl.invoke0(   Native Method )
      at sun.reflect.NativeMethodAccessorImpl.invoke( NativeMethodAccessorImpl.java:39 )
      at sun.reflect.DelegatingMethodAccessorImpl.invoke( DelegatingMethodAccessorImpl.java:25 )
      at java.lang.reflect.Method.invoke(   Method.java:597 )
      at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall( FrameworkMethod.java:44 )
      at org.junit.internal.runners.model.ReflectiveCallable.run( ReflectiveCallable.java:15 )
      at org.junit.runners.model.FrameworkMethod.invokeExplosively( FrameworkMethod.java:41 )
      at org.junit.internal.runners.statements.InvokeMethod.evaluate( InvokeMethod.java:20 )
      at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored( BlockJUnit4ClassRunner.java:79 )
      at org.junit.runners.BlockJUnit4ClassRunner.runChild( BlockJUnit4ClassRunner.java:71 )
      at org.junit.runners.BlockJUnit4ClassRunner.runChild( BlockJUnit4ClassRunner.java:49 )
      at org.junit.runners.ParentRunner$3.run(   ParentRunner.java:193 )
      at org.junit.runners.ParentRunner$1.schedule(   ParentRunner.java:52 )
      at org.junit.runners.ParentRunner.runChildren( ParentRunner.java:191 )
      at org.junit.runners.ParentRunner.access$000(   ParentRunner.java:42 )
      at org.junit.runners.ParentRunner$2.evaluate( ParentRunner.java:184 )
      at org.junit.runners.ParentRunner.run(   ParentRunner.java:236 )
      at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run( JUnit4TestReference.java:49 )
      at org.eclipse.jdt.internal.junit.runner.TestExecution.run( TestExecution.java:38 )
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests( RemoteTestRunner.java:467 )
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests( RemoteTestRunner.java:683 )
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run( RemoteTestRunner.java:390 )
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main( RemoteTestRunner.java:197 )
从日志上来看这只是一个debug信息,但探索究竟是程序员的天性,于是毅然决然的走上了探寻究竟的道路,行从Shell.java开始,Shell.java中通过System.getProperties("hadoop.home.dir")和System.env("HADOOP_HOME")获取HADOOP_HOME目录,如果在开发环境中没有设置这两个环境变量任何一个,程序运行是就会打出上面的日志。
   知道了日志输出的原因就有了解决办法(假设开发环境中hadoop安装在d:\hadoop目录下):
   方法一:在windows中设置环境变量HOADOOP_HOME=d:\hadoop
   方法二:在测试程序时通过System.setProperty("hadoop.home.dir", "d:\\hadoop")
 
   以上日志不是HBase的程序输出的,在Hadoop开发中也会遇到这样的日志,也可以通过上面的方法解决。