ITeye博客

java客户端访问hbase

hunray · 2013-01-23 17:33 · hbase · 开源软件 · Markdown

hbase的安装和配置请看 http://hunray.iteye.com/admin/blogs/1774583

使用shell建表 create 'gpsinfo','gpsdata' gpsinfo为表名 gpsdata为columnfamily


import java.io.IOException;
import java.util.Date;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;

public class HbaseSave {

    public static void main(String[] args) throws Exception {
        //建表 create 'gpsinfo','gpsdata'
        Configuration HBASE_CONFIG = new Configuration();    
        HBASE_CONFIG.set("hbase.zookeeper.quorum", "192.168.3.206");  
        Configuration configuration = HBaseConfiguration.create(HBASE_CONFIG);  
        try {  
            HBaseAdmin admin =new HBaseAdmin(configuration);
            if (admin.tableExists("gpsinfo")) {
                System.out.println("表已经存在!");
                HTable table = new HTable(configuration, "gpsinfo");  
                Put put = new Put(Bytes.toBytes("0800025364"+new Date().getTime()));  
                put.add(Bytes.toBytes("gpsdata"),Bytes.toBytes("unitid"),Bytes.toBytes("0800025364"));  
                put.add(Bytes.toBytes("gpsdata"),Bytes.toBytes("lon"),Bytes.toBytes(111.321f));  
                put.add(Bytes.toBytes("gpsdata"),Bytes.toBytes("lat"),Bytes.toBytes(23.4687f));  
                put.add(Bytes.toBytes("gpsdata"),Bytes.toBytes("direction"),Bytes.toBytes(180));  
                put.add(Bytes.toBytes("gpsdata"),Bytes.toBytes("speed"),Bytes.toBytes(62.2f));  
                put.add(Bytes.toBytes("gpsdata"),Bytes.toBytes("locationstatus"),Bytes.toBytes(1));  
                put.add(Bytes.toBytes("gpsdata"),Bytes.toBytes("vehiclestatus"),Bytes.toBytes("点火"));  
                put.add(Bytes.toBytes("gpsdata"),Bytes.toBytes("gpstime"),Bytes.toBytes(new Date().getTime()));  
                put.add(Bytes.toBytes("gpsdata"),Bytes.toBytes("referenceposition"),Bytes.toBytes("意隆路22号"));  
                put.add(Bytes.toBytes("gpsdata"),Bytes.toBytes("rawdata"),Bytes.toBytes("7b334673124545711133447852217d"));  
                table.put(put);  
                table.close();
            }else{
                System.out.println("表不存在");
            }
            configuration.clear();
        } catch (IOException e) {  
            e.printStackTrace();  
        }
    }
 
}


在此过程中碰到的一些问题:

  1. hadoop namenode -format

-bash: hadoop: command not found 将hadoop/bin路径加入PATH,需要重启机器

vi /etc/profile

export PATH=$JAVA_HOME/bin:$PATH:/root/Desktop/hadoop-1.1.1/bin

  1. myeclipse连接报错: 警告: Possibly transient ZooKeeper exception: org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /hbase/master 2013-1-22 10:22:36 org.apache.hadoop.hbase.util.RetryCounter sleepUntilNextRetry 确保hbase正常启动了 禁用IPV6,将/etc/hosts文件里面的::1 localhost那一行删掉重启 关闭防火墙

  2. hadoop启动,namenode日志文件中 org.apache.hadoop.hdfs.server.common.InconsistentFSStateException: Directory /var/log/hadoop_data/dfs/name is in an inconsistent state: storage directory does not exist or is not accessible. at org.apache.hadoop.hdfs.server.namenode.FSImage.recoverTransitionRead(FSImage.java:303) at org.apache.hadoop.hdfs.server.namenode.FSDirectory.loadFSImage(FSDirectory.java:100) at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.initialize(FSNamesystem.java:411) at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.(FSNamesystem.java:379) at org.apache.hadoop.hdfs.server.namenode.NameNode.initialize(NameNode.java:277) at org.apache.hadoop.hdfs.server.namenode.NameNode.(NameNode.java:529) at org.apache.hadoop.hdfs.server.namenode.NameNode.createNameNode(NameNode.java:1403) at org.apache.hadoop.hdfs.server.namenode.NameNode.main(NameNode.java:1412) 格式化namenode $ hadoop namenode –format

  3. 客户端连接hbase报错: java.net.UnknownHostException: unknown host: xiekang-pc 在本地配置host 192.168.3.206 xiekang-pc

  4. java.net.ConnectException: Connection refused: no further information org.apache.hadoop.hbase.ipc.HBaseClient$FailedServerException: This server is in the failed servers list: localhost/127.0.0.1:60000 服务器上master地址和localhost/127.0.0.1:60000对不上。 查看http://192.168.3.206:60010/master-status的master地址。

评论(5)

  1. #5 楼 · 因为青所以涩 · 2013-11-11 15:23

    我把配置中的所有地址用IP代替,终于解决了

  2. #4 楼 · 因为青所以涩 · 2013-11-08 15:38

    你好!我服务器运行hbase后,页面访问http://192.168.184.128:60010/master-status,显示的Master: node1:60000。 服务器中/etc/hosts内容如下,(只用了一台虚拟机来搭环境): 127.0.0.1 node1 localhost ::1 localhost6.localdomain6 localhost6 192.168.184.128 node1 =====-------------- 但是我使用客户端连接,老是抛出这个异常 org.apache.hadoop.hbase.ipc.HBaseClient$FailedServerException: This server is in the failed servers list: node1/192.168.184.128:60000 你能帮我分析一下原因么?谢谢啦。 调用的代码为 Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.property.clientPort", "2181"); conf.set("hbase.zookeeper.quorum", "node1"); conf.set("hbase.master", "node1:60000"); conf.set(TableOutputFormat.OUTPUT_TABLE, "xtab"); HBaseAdmin admin = new HBaseAdmin(conf);

  3. #3 楼 · a420144030 · 2013-05-08 11:58

    是这个问题,已经解决,谢谢

  4. #2 楼 · hunray · 2013-04-28 08:45

    127.0.0.1 localhost

    The following lines are desirable for IPv6 capable hosts

    ::1 localhost ip6-localhost ip6-loopback
    fe00::0 ip6-localnet
    ff00::0 ip6-mcastprefix
    ff02::1 ip6-allnodes
    ff02::2 ip6-allrouters
    ff02::3 ip6-allhosts

    192.168.1.106 hrkjsoft01-desktop
    192.168.1.99 slave99
    192.168.1.104 slave98

    master和slave都用这个配置试一试。

  5. #1 楼 · a420144030 · 2013-04-22 17:02

    你好,能帮我看看吗?我的hbase出现问题:org.apache.hadoop.hbase.ipc.HBaseClient$FailedServerException: This server is in the failed servers list: hrkjsoft01-desktop/192.168.1.106:60000

    master /etc/hosts

    
    #127.0.0.1      localhost
    127.0.0.1       hrkjsoft01-desktop
    
    # The following lines are desirable for IPv6 capable hosts
    ::1     localhost ip6-localhost ip6-loopback
    fe00::0 ip6-localnet
    ff00::0 ip6-mcastprefix
    ff02::1 ip6-allnodes
    ff02::2 ip6-allrouters
    ff02::3 ip6-allhosts
    
    192.168.1.106 master
    192.168.1.99 slave99
    192.168.1.104 slave98
    
    

    slave99 /etc/hosts

    
    #127.0.0.1      localhost
    127.0.0.1       hsweb localhost
    
    # The following lines are desirable for IPv6 capable hosts
    ::1     ip6-localhost ip6-loopback
    fe00::0 ip6-localnet
    ff00::0 ip6-mcastprefix
    ff02::1 ip6-allnodes
    ff02::2 ip6-allrouters
    
    
    192.168.1.106 master  hrkjsoft01-desktop
    192.168.1.99 slave99
    192.168.1.104 slave98
    
    

评论功能维护中,当前仅展示历史评论。