HBase的java客户端

工作中用到的HBase版本是 1.2.0-cdh5.12.0,使用的开发工具是IDEA,简单的写一个Demo。

开发环境依赖:


    
    

    
      junit
      junit
      3.8.1
      test
    
    
      org.junit.jupiter
      junit-jupiter-api
      RELEASE
    
    
      log4j
      log4j
      1.2.17
    
    
      org.apache.hadoop
      hadoop-common
      2.6.0-cdh5.12.0
    

    
      org.apache.hadoop
      hadoop-hdfs
      2.6.0-cdh5.12.0
    

    
      org.apache.hbase
      hbase-client
      1.2.0-cdh5.12.0
    

    
      org.apache.hbase
      hbase-server
      1.2.0-cdh5.12.0
    
  
  




  
    
      cloudera
      https://repository.cloudera.com/artifactory/cloudera-repos/
    

  
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
/*
测试HBase的增加和查询
*/

public class HBaseTest {
    public static void main(String[] args) throws IOException {
        Configuration conf= HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","10.1.34.124");
        conf.set("hbase.zookeeper.quorum.clientPort","2181");
        Connection connection= ConnectionFactory.createConnection(conf);
        TableName tableName=TableName.valueOf("test2");
        Table table=connection.getTable(tableName);
       /* Put put=new Put("001".getBytes());
        put.addColumn("f".getBytes(),"name".getBytes(),"zhgangsan".getBytes());
        table.put(put);*/

       Scan scan=new Scan();
       ResultScanner scanner=table.getScanner(scan);
       for(Result result:scanner){
         /*String row= Bytes.toString(result.getRow());
           System.out.println(row);*/
//Cell是最新的API
         for(Cell cell:result.rawCells()){
             String rowKey=Bytes.toString(CellUtil.cloneRow(cell));
             String family=Bytes.toString(CellUtil.cloneFamily(cell));
             String quailify=Bytes.toString(CellUtil.cloneQualifier(cell));
             String values=Bytes.toString(CellUtil.cloneValue(cell));
             System.out.println("rowKey="+rowKey);
         }
       }
        System.out.println("success------------------------");

    }
}

 

你可能感兴趣的:(HBase的java客户端)