Hbase客户端JAVA的API,过滤器

POM依赖

    <dependencies>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.6</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>2.0.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>2.7.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>2.7.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-server</artifactId>
            <version>2.0.4</version>
        </dependency>
        <!-- 使用mr程序操作hbase 数据的导入 -->
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-mapreduce</artifactId>
            <version>2.0.4</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.5</version>
        </dependency>
        <!-- phoenix 凤凰 用来整合Hbase的工具 -->
        <dependency>
            <groupId>org.apache.phoenix</groupId>
            <artifactId>phoenix-core</artifactId>
            <version>5.0.0-HBase-2.0</version>
        </dependency>
    </dependencies>

 

JAVA-API

hbase的java客户端连接hbase的时候只需要连接zookeeper集群即可找到Hbase集群的位置

核心对象如下:

Configuration  : HBaseConfiguration.create();
Connection     : ConnectionFactory.createConnection(conf);
Table               : conn.getTable(TableName.valueOf("tb_b"));  可以对表操作  DML
Admin              : conn.getAdmin();  操作hbase系统   DDL  名称空间 tools ...

 

package com.doit.hbase.java_api;

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;

public class MyHbaseUtil {

    //获取连接
    public static Connection getConnection() throws Exception {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum" , "linux01:2181,linux02:2181,linux03:2181");
        Connection connection = ConnectionFactory.createConnection(conf);
        return connection;
    }


    //获取TABLE
    public static Table getTable(String tableName) throws Exception {
        Connection connection = getConnection();
        Table table = connection.getTable(TableName.valueOf(tableName));
        return table;
    }

    //获取ADMIN
    public static Admin getAdmin() throws Exception{
        return getConnection().getAdmin();
    }

    //展示表数据
    public static void printTableData(String tableName,String rowKey) throws Exception {
        Table table = getTable(tableName);
        Get get = new Get(rowKey.getBytes());
        Result row = table.get(get);
        while(row.advance()){
            Cell cell = row.current();
            byte[] coumnFamily = CellUtil.cloneFamily(cell);
            byte[] coumn = CellUtil.cloneQualifier(cell);
            byte[] value = CellUtil.cloneValue(cell);
            System.out.println(Bytes.toString(coumnFamily)+":"+Bytes.toString(coumn)+",="+Bytes.toString(value));
        }
        table.close();
    }



}

 

    @Test
    public void testFilter()  throws Exception {
        Table table = MyHbaseUtil.getTable("yege:tb_a");
        Scan scan = new Scan();
        //设置过滤器
        RowFilter rowFilter = new RowFilter(CompareOperator.EQUAL, new BinaryComparator("rk001".getBytes()));
        QualifierFilter familyFilter = new QualifierFilter(CompareOperator.EQUAL, new BinaryComparator("name".getBytes()));
        //过滤器集合,继承Filter
        FilterList filterList = new FilterList();
        filterList.addFilter(rowFilter);
        filterList.addFilter(familyFilter);

        scan.setFilter(filterList);
        ResultScanner scanner = table.getScanner(scan);
        Iterator<Result> iterator = scanner.iterator();
        while(iterator.hasNext()){
            Result result = iterator.next();
            while (result.advance()){
                Cell cell = result.current();
                String row = new String(CellUtil.cloneRow(cell));
                String cloumFamily = new String(CellUtil.cloneFamily(cell));
                String colum = new String(CellUtil.cloneQualifier(cell));
                String value = new String(CellUtil.cloneValue(cell));
                System.out.println(row+","+cloumFamily+":"+colum+"===="+value);
            }
        }
    }

 

 

DML和DDL操作参考链接:https://blog.csdn.net/qq_37933018/article/details/106710506

批量写入参考链接:https://blog.csdn.net/asd136912/article/details/100825957

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值