java客户端访问hbase

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

使用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

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地址。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值