在连接HBase之前需要设置其基本配置信息,通常通过两种方式,一种是直接在工程中添加hbase-site.xml的配置文件,一种是通过Java代码直接设置,如
cfg.set("hbase.rootdir", "hdfs://master:9000/hbase");
cfg.set("hbase.master", "hdfs://master:60000");
cfg.set("hbase.zookeeper.quorum", "slave1,slave2,slave3");
如果不是通过ip地址直接设置的,需要在windows系统的hosts文件中添加主机名称和ip地址映射关系
在实际读取HBase时可能会出现下面错误
警告: Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect
java.net.ConnectException: Connection refused: no further informationat sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:735)
at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:361)
at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1081)
十二月 22, 2015 3:37:30 下午 org.apache.zookeeper.ClientCnxn$SendThread logStartConnect
信息: Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
15/12/22 15:37:30 WARN zookeeper.RecoverableZooKeeper: Possibly transient ZooKeeper, quorum=localhost:2181, exception=org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /hbase/hbaseid
15/12/22 15:37:30 INFO util.RetryCounter: Sleeping 1000ms before retry #0...
十二月 22, 2015 3:37:31 下午 org.apache.zookeeper.ClientCnxn$SendThread run
警告: Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect
java.net.ConnectException: Connection refused: no further information
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:735)
at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:361)
at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1081)
解决方法:在hosts文件中添加下面代码
##connect to zookeeper server
ip地址 localhost.localdomain localhost
其中IP地址是指zookeeper所在服务器的ip地址,通常zookeeper不只一台,指定其中任意一台即可。