HBAseのjavaクライアント

3564 ワード

作業で使用したHBAseバージョンは 1.2.0-cdh 5.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------------------------");

    }
}