HBase的java客户端

本文介绍了一个基于HBase 1.2.0版本的简单Demo,使用IDEA进行开发,演示了如何在特定环境下配置依赖,并提供了增加和查询数据的代码示例。

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

开发环境依赖:

<dependencies>
    <!--dependency>
      <groupId>spark</groupId>
      <artifactId>[the artifact id of the block to be mounted]</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency-->
    <!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase -->

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-api</artifactId>
      <version>RELEASE</version>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-common</artifactId>
      <version>2.6.0-cdh5.12.0</version>
    </dependency>

    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-hdfs</artifactId>
      <version>2.6.0-cdh5.12.0</version>
    </dependency>
<!--hbase的简单的增删改查只需要引入HBase的client即可-->
    <dependency>
      <groupId>org.apache.hbase</groupId>
      <artifactId>hbase-client</artifactId>
      <version>1.2.0-cdh5.12.0</version>
    </dependency>
<!--hbase的高级应用,比如自定义协处理器则需要引入hbase服务端-->
    <dependency>
      <groupId>org.apache.hbase</groupId>
      <artifactId>hbase-server</artifactId>
      <version>1.2.0-cdh5.12.0</version>
    </dependency>
  
  </dependencies>


<!--添加CDH版本的HBase需要添加cloudera仓库,否则无法使用-->

  <repositories>
    <repository>
      <id>cloudera</id>
      <url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
    </repository>

  </repositories>
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------------------------");

    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值