`
hunray
  • 浏览: 219354 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

java客户端访问hbase

阅读更多
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. [root@localhost bin]# hadoop namenode -format
-bash: hadoop: command not found
将hadoop/bin路径加入PATH,需要重启机器
[root@localhost bin]# vi /etc/profile
export PATH=$JAVA_HOME/bin:$PATH:/root/Desktop/hadoop-1.1.1/bin

2. 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那一行删掉重启
关闭防火墙

3. 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.<init>(FSNamesystem.java:379)
at org.apache.hadoop.hdfs.server.namenode.NameNode.initialize(NameNode.java:277)
at org.apache.hadoop.hdfs.server.namenode.NameNode.<init>(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

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

5. 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 楼 因为青所以涩 2013-11-11  
我把配置中的所有地址用IP代替,终于解决了
4 楼 因为青所以涩 2013-11-08  
你好!我服务器运行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 楼 a420144030 2013-05-08  
是这个问题,已经解决,谢谢
2 楼 hunray 2013-04-28  
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都用这个配置试一试。
1 楼 a420144030 2013-04-22  
你好,能帮我看看吗?我的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

相关推荐

    HBase权威指南

    使用本地Java客户端,或者通过提供了REST、Avro和Thrift应用编程接口的网关服务器来访问HBase;了解HBase架构的细节,包括存储格式、预写日志、后台进程等;在HBase中集成MapReduce框架;了解如何调节集群、设计模式...

    hbase权威指南

    使用本地Java客户端,或者通过提供了REST、Avro和Thrift应用编程接口的网关服务器来访问HBase;了解HBase架构的细节,包括存储格式、预写日志、后台进程等;在HBase中集成MapReduce框架;了解如何调节集群、设计模式...

    HBase权威指南中文版

    使用本地Java客户端,或者通过提供了REST、Avro和Thrift应用编程接口的网关服务器来访问HBase;让你了解HBase架构的细节,包括存储格式、预写日志、后台进程等等;在HBase中集成用于海量并行数据处理任务的Hadoop的...

    HBASE指南中文

    使用本地Java客户端,或者通过提供了REST、Avro和Thrift应用编程接口的网关服务器来访问HBase;了解HBase架构的细节,包括存储格式、预写日志、后台进程等;在HBase中集成MapReduce框架;了解如何调节集群、设计模式...

    HBase权威指南(中文版).pdf

    使用本地Java客户端,或者通过提供了REST、Avro和Thrift应用编程接口的网关服务器来访问HBase;了解HBase架构的细节,包括存储格式、预写日志、后台进程等;在HBase中集成MapReduce框架;了解如何调节集群、设计模式...

    Hbase权威指南(HBase: The Definitive Guide)

     ■使用本地java客户端,或者通过提供了rest、avro和thrift应用编程接口的网关服务器来访问hbase  ■了解hbase架构的细节,包括存储格式、预写日志、后台进程等等  ■在hbase中集成用于海量并行数据处理任务的...

    Hbase中文文档

    8.1. 安全客户端访问 HBase 8.2. 访问控制 9. 架构 9.1. 概述 9.2. Catalog Tables 9.3. 客户端 9.4. Client Request Filters 9.5. Master 9.6. RegionServer 9.7. Regions 9.8. Bulk Loading 9.9. HDFS 10. 外部 ...

    hbase权威指南中文版

    使用本地Java客户端,或者通过提供了REST、Avro和Thrift应用编程接口的网关服务器来访问HBase;了解HBase架构的细节,包括存储格式、预写日志、后台进程等;在HBase中集成MapReduce框架;了解如何调节集群、设计模式...

    Hbase权威指南

    使用本地java客户端,或者通过提供了rest、avro和thrift应用编程接口的网关服务器来访问hbase;了解hbase架构的细节,包括存储格式、预写日志、后台进程等;在hbase中集成mapreduce框架;了解如何调节集群、设计模式...

Global site tag (gtag.js) - Google Analytics