Scala操作Hbase空指针异常java.lang.NullPointerException处理

时间:2023-03-08 16:25:34
Scala操作Hbase空指针异常java.lang.NullPointerException处理

Hbase版本:Hortonworks Hbase 1.1.2

问题描述:
使用Scala操作Hbase时,发生空指针异常(java.lang.RuntimeException: java.lang.NullPointerException),异常如下:

// :: ERROR Executor: Exception in task 1.0 in stage 361.0 (TID )
java.lang.RuntimeException: java.lang.NullPointerException
at org.apache.hadoop.hbase.client.RpcRetryingCaller.callWithoutRetries(RpcRetryingCaller.java:)
at org.apache.hadoop.hbase.client.ClientSmallReversedScanner.loadCache(ClientSmallReversedScanner.java:)
at org.apache.hadoop.hbase.client.ClientSmallReversedScanner.next(ClientSmallReversedScanner.java:)
at org.apache.hadoop.hbase.client.ConnectionManager$HConnectionImplementation.locateRegionInMeta(ConnectionManager.java:)
at org.apache.hadoop.hbase.client.ConnectionManager$HConnectionImplementation.locateRegion(ConnectionManager.java:)
at org.apache.hadoop.hbase.client.AsyncProcess.submit(AsyncProcess.java:)
at org.apache.hadoop.hbase.client.AsyncProcess.submit(AsyncProcess.java:)
at org.apache.hadoop.hbase.client.BufferedMutatorImpl.backgroundFlushCommits(BufferedMutatorImpl.java:)
at org.apache.hadoop.hbase.client.BufferedMutatorImpl.flush(BufferedMutatorImpl.java:)
at org.apache.hadoop.hbase.client.HTable.flushCommits(HTable.java:)
at org.apache.hadoop.hbase.client.HTable.put(HTable.java:)
at scala.OperateMysqlTable$$anonfun$main$$$anonfun$apply$$$anonfun$apply$.apply(AccompanyAnalyze.scala:)
at scala.OperateMysqlTable$$anonfun$main$$$anonfun$apply$$$anonfun$apply$.apply(AccompanyAnalyze.scala:)
at org.apache.spark.rdd.RDD$$anonfun$foreachPartition$$$anonfun$apply$.apply(RDD.scala:)
at org.apache.spark.rdd.RDD$$anonfun$foreachPartition$$$anonfun$apply$.apply(RDD.scala:)
at org.apache.spark.SparkContext$$anonfun$runJob$.apply(SparkContext.scala:)
at org.apache.spark.SparkContext$$anonfun$runJob$.apply(SparkContext.scala:)
at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:)
at org.apache.spark.scheduler.Task.run(Task.scala:)
at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:)
at java.lang.Thread.run(Thread.java:)
Caused by: java.lang.NullPointerException
at org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher.getMetaReplicaNodes(ZooKeeperWatcher.java:)
at org.apache.hadoop.hbase.zookeeper.MetaTableLocator.blockUntilAvailable(MetaTableLocator.java:)
at org.apache.hadoop.hbase.client.ZooKeeperRegistry.getMetaRegionLocation(ZooKeeperRegistry.java:)
at org.apache.hadoop.hbase.client.ConnectionManager$HConnectionImplementation.locateMeta(ConnectionManager.java:)
at org.apache.hadoop.hbase.client.ConnectionManager$HConnectionImplementation.locateRegion(ConnectionManager.java:)
at org.apache.hadoop.hbase.client.RpcRetryingCallerWithReadReplicas.getRegionLocations(RpcRetryingCallerWithReadReplicas.java:)
at org.apache.hadoop.hbase.client.ScannerCallableWithReplicas.call(ScannerCallableWithReplicas.java:)
at org.apache.hadoop.hbase.client.ScannerCallableWithReplicas.call(ScannerCallableWithReplicas.java:)
at org.apache.hadoop.hbase.client.RpcRetryingCaller.callWithoutRetries(RpcRetryingCaller.java:)
... more

问题分析:

该异常为客户端去zookeeper获取Hbase相关信息时,没有找到,抛出空指针异常

问题原因:
不同发行商的hbase的在zookeeper的znode设置不同
Hortonworks的hbase在zookeeper中znode路径为/hbase-unsecure

查看该路径下hbase的相关信息

[zk: localhost:(CONNECTED) ] ls /hbase-unsecure
[replication, meta-region-server, rs, splitWAL, backup-masters, table-lock, flush-table-proc, master-maintenance, region-in-transition, online-snapshot, acl, switch, master, running, recovering-regions, draining, namespace, hbaseid, table]

解决办法:
我使用的是Hortonworks的Hbase,所以要在Hbase的conf中添加如下配置信息

conf.set("zookeeper.znode.parent", "/hbase-unsecure")

如果是Cloudera的Hbase,添加配置信息如下:

conf.set("zookeeper.znode.parent", "/hbase")

添加后,重新执行程序,Hbase表创建成功,问题解决