Hbase客户端JAVA的API,过滤器

POM依赖

    
        
            org.apache.zookeeper
            zookeeper
            3.4.6
        
        
            junit
            junit
            4.11
            compile
        
        
            org.apache.hbase
            hbase-client
            2.0.4
        
        
            org.apache.hadoop
            hadoop-client
            2.7.7
        
        
            org.apache.hadoop
            hadoop-common
            2.7.7
        
        
            org.apache.hbase
            hbase-server
            2.0.4
        
        
        
            org.apache.hbase
            hbase-mapreduce
            2.0.4
        
        
            com.google.code.gson
            gson
            2.8.5
        
        
        
            org.apache.phoenix
            phoenix-core
            5.0.0-HBase-2.0
        
    

 

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 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

你可能感兴趣的:(Hbase)