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地址。


你可能感兴趣的:(hbase)