Hi 各位,如果在使用java操作Hbase时,遇到如下报错:

Exception in thread "main" java.lang.NullPointerException
at org.apache.hadoop.hbase.zookeeper.ZKConfig.makeZKProps(ZKConfig.java:60)
at org.apache.hadoop.hbase.zookeeper.ZKConfig.getZKQuorumServersString(ZKConfig.java:245)
at org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher.<init>(ZooKeeperWatcher.java:147)
at org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher.<init>(ZooKeeperWatcher.java:127)
at org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation.getZooKeeperWatcher(HConnectionManager.java:1366)
at org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation.ensureZookeeperTrackers(HConnectionManager.java:611)
at org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation.locateRegion(HConnectionManager.java:871)
at org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation.locateRegion(HConnectionManager.java:846)
at org.apache.hadoop.hbase.client.HTable.finishSetup(HTable.java:271)
at org.apache.hadoop.hbase.client.HTable.<init>(HTable.java:211)
at org.apache.hadoop.hbase.client.HTableFactory.createHTableInterface(HTableFactory.java:36)
at org.apache.hadoop.hbase.client.HTablePool.createHTable(HTablePool.java:265)
at org.apache.hadoop.hbase.client.HTablePool.findOrCreateTable(HTablePool.java:195)
at org.apache.hadoop.hbase.client.HTablePool.getTable(HTablePool.java:174)
at com.yf.test.hbase.HBaseTablePool.getTable(HBaseTablePool.java:69)
at com.yf.test.hbase.HbaseTest.main(HbaseTest.java:43) 

很明显大家都知道这是空指针异常,但是代码片段都没有问题(可能代码还是从网络拷贝出来的),按理更不会出现这种问题。

org.apache.hadoop.hbase.zookeeper.ZKConfig.makeZKProps(ZKConfig.java:60)  //最底层出现的错误应该为ZKConfig类中的方法makeZKProps

反编译hbase jar发现 
ClassLoader cl = HQuorumPeer.class.getClassLoader();//这是干什么的请自行百度

InputStream inputStream = cl.getResourceAsStream();//报错空指针异常,说明cl对象为null。


ClassName既然被正确引入,获取类加载器应不应该出现为null的情况。

ClassLoader cl = ClassName.class.getClassLoader();

所以怀疑是hbase相关开发依赖的jar包引入存在问题,于是删掉所有jar包,重新Build Path,测试通过。

综上,出现此问题非代码问题,而是jar引入存在问题,可先进行如下代码的测试,输出不为null则通过。

ClassLoader cl = HQuorumPeer.class.getClassLoader();
Syste.out.println(cl);

另外附上我的测试代码:

package com.yf.test.hbase;

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.HBaseAdmin;

public class HbaseTest {
	//设备Hbase链接参数
	public static final Configuration configuration;  
	
    static {  
        configuration = HBaseConfiguration.create();
//    	configuration.addResource("hbase-site.xml");
    	configuration.set("hbase.zookeeper.quorum", "nginx2,nginx1");
    	configuration.set("hbase.zookeeper.property.clientPort", "2181");
    } 
    
    public static void main(String args[]){
        //可以先运行下方注释代码进行测试一下,如无空指针则OK
//    	ClassLoader cl = HQuorumPeer.class.getClassLoader();
//    	System.out.println(cl);
    	createTable("test");
    }
    
    public static void createTable(String tableName){
    	System.out.println("Begin to create table...");
    	try {
			HBaseAdmin hBaseAdmin = new HBaseAdmin(configuration); 
			if(hBaseAdmin.tableExists(tableName)){
				hBaseAdmin.disableTable(tableName);
				hBaseAdmin.deleteTable(tableName);
				System.out.println("Table is existed...");
			}
			HTableDescriptor desc = new HTableDescriptor(tableName);
			desc.addFamily(new HColumnDescriptor("column01"));
			desc.addFamily(new HColumnDescriptor("column02"));
			desc.addFamily(new HColumnDescriptor("column03"));
			hBaseAdmin.createTable(desc);
		} catch (MasterNotRunningException e) {
			System.out.println("HBase Master is not running...");
			e.printStackTrace();
		} catch (ZooKeeperConnectionException e) {
			System.out.println("HBase Zookeeper is not running...");
			e.printStackTrace();
		}
    	catch (IOException e) {
			e.printStackTrace();
		}
    	System.out.println("end create table ......");  
    }
    
}


Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐