HBase数据输入程序运行不正常

时间:2016-04-24 08:43:32

标签: hadoop hbase apache-zookeeper hadoop2

以下是HBase中数据输入的代码:

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;

public class SimpleDataEntry {

   public static void main(String[] args) throws IOException {

      // Instantiating Configuration class
      Configuration config = HBaseConfiguration.create();

      // Instantiating HTable class
      HTable hTable = new HTable(config, "emp");

      // Instantiating Put class
      // accepts a row name.
      Put p = new Put(Bytes.toBytes("row1")); 

      // adding values using add() method
      // accepts column family name, qualifier/row name ,value
      p.add(Bytes.toBytes("personal"),
      Bytes.toBytes("name"),Bytes.toBytes("raju"));

      p.add(Bytes.toBytes("personal"),
      Bytes.toBytes("city"),Bytes.toBytes("hyderabad"));

      p.add(Bytes.toBytes("professional"),Bytes.toBytes("designation"),
      Bytes.toBytes("manager"));

      p.add(Bytes.toBytes("professional"),Bytes.toBytes("salary"),
      Bytes.toBytes("50000"));

      // Saving the put Instance to the HTable.
      hTable.put(p);
      System.out.println("data inserted");

      // closing HTable
      hTable.close();
   }
}

我们在运行此代码时遇到的错误是:

16/04/24 14:07:58 INFO zookeeper.ZooKeeper: Client environment:java.library.path=/home/hadoop1/hadoop1/lib/native
16/04/24 14:07:58 INFO zookeeper.ZooKeeper: Client environment:java.io.tmpdir=/tmp
16/04/24 14:07:58 INFO zookeeper.ZooKeeper: Client environment:java.compiler=<NA>
16/04/24 14:07:58 INFO zookeeper.ZooKeeper: Client environment:os.name=Linux
16/04/24 14:07:58 INFO zookeeper.ZooKeeper: Client environment:os.arch=amd64
16/04/24 14:07:58 INFO zookeeper.ZooKeeper: Client environment:os.version=3.10.0-123.el7.x86_64
16/04/24 14:07:58 INFO zookeeper.ZooKeeper: Client environment:user.name=hadoop1
16/04/24 14:07:58 INFO zookeeper.ZooKeeper: Client environment:user.home=/home/hadoop1
16/04/24 14:07:58 INFO zookeeper.ZooKeeper: Client environment:user.dir=/home/hadoop1
16/04/24 14:07:58 INFO zookeeper.ZooKeeper: Initiating client connection, connectString=localhost:2181 sessionTimeout=90000 watcher=hconnection-0x5542c4ed0x0, quorum=localhost:2181, baseZNode=/hbase
16/04/24 14:07:58 INFO zookeeper.ClientCnxn: Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
16/04/24 14:07:58 WARN zookeeper.ClientCnxn: Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect
java.net.ConnectException: Connection refused
    at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
    at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
    at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:361)
    at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1081)
16/04/24 14:07:58 WARN zookeeper.RecoverableZooKeeper: Possibly transient ZooKeeper, quorum=localhost:2181, exception=org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /hbase/hbaseid
16/04/24 14:07:59 INFO zookeeper.ClientCnxn: Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
16/04/24 14:07:59 WARN zookeeper.ClientCnxn: Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect
java.net.ConnectException: Connection refused
    at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
    at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
    at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:361)
    at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1081)

hbase-site.xml如下:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>

<configuration>
   //Here you have to set the path where you want HBase to store its files.
   <property>
      <name>hbase.rootdir</name>
      <value>hdfs://hadoop-master:9000/hbase</value>
   </property>

   //Here you have to set the path where you want HBase to store its built in zookeeper  files.
   <property>
      <name>hbase.zookeeper.property.dataDir</name>
      <value>/home/hadoop1/zookeeper</value>
   </property>

<property>
   <name>hbase.cluster.distributed</name>
   <value>true</value>
</property>


<property>
    <name>hbase.zookeeper.property.clientPort</name>
    <value>2183</value>
</property>

<property>
    <name>hbase.zookeeper.quorum</name>
    <value>172.17.25.20</value>

</property>
</configuration>

可能存在什么问题及其解决方案?

2 个答案:

答案 0 :(得分:1)

日志中的错误表明hbase-site.xml没有正确加载。检查你的hbase-site.xml:它必须在你的类路径上,因为appearance -> editor -> sidebar.php从你在类路径上设置的路径加载配置(并尝试将它添加到类路径的开头以防止加载hbase-site.xml从嵌入了类似配置文件的其他jar中) 此外,您似乎使用来自Hbase服务器的hbase-site.xml:除了hbase.zookeeper.quorum之外的所有配置密钥都是冗余的,在客户端中没用。

答案 1 :(得分:1)

Configuration config = HBaseConfiguration.create();如果java找不到hbase-site.xml,则只创建一个几乎为空的配置文件 要告诉java conf文件的位置,可以将hbase-site.xml直接放在类路径中,也可以调用conf.addResource(**hbase-site path**)

修改

如拉格朗的评论所述,请尝试conf.set("hbase.zookeeper.quorum","172.17.25.20:2183")