HBASE详细概述

HBASE架构

在这里插入图片描述

HBASEshell

不做概述

HBASE的JavaAPI操作

hbase的api操作总结下来就是一句话,就是
需要对表做一些操作,就使用getAdmin()(修改表结构什么的)
需要对数据做一些操作,就使用getTable()

配置连接并尝试创建一个表:

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;

import java.io.IOException;

public class Demo01Test {
    public static void main(String[] args) throws IOException {
        //创建配置,指定zk的集群地址
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","master,node1,node2");

        //创建连接
        Connection conn = ConnectionFactory.createConnection(conf);

        //创建Admin对象
        Admin admin = conn.getAdmin();

        //创建test_01表
        HTableDescriptor test_01 = new HTableDescriptor(TableName.valueOf("test_01"));

        //创建一个列簇
        HColumnDescriptor cf1 = new HColumnDescriptor("cf1");

        //配置列簇
        cf1.setTimeToLive(10000);//设置过期时间
        cf1.setMaxVersions(3);//设置最大版本数
        //增加一个列簇
        test_01.addFamily(cf1);

        admin.createTable(test_01);

        //关闭连接
        conn.close();
    }
}

实现各类不同的操作:

import javafx.scene.control.Tab;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class Demo02API {

    Connection conn;

    @Before
    public void init() throws IOException {
        HBaseConfiguration conf = new HBaseConfiguration();
        conf.set("hbase.zookeeper.quorum","master,node1,node2");
        conn = ConnectionFactory.createConnection(conf);
    }

    //创建表
    @Test
    public void create() throws IOException {
        Admin admin = conn.getAdmin();
        if(!admin.tableExists(TableName.valueOf("test03"))){
            HTableDescriptor test_03 = new HTableDescriptor(TableName.valueOf("test_03"));

            HColumnDescriptor cf3 = new HColumnDescriptor("cf3");
            cf3.setTimeToLive(1000);//设置过期时间
            cf3.setMaxVersions(3);//设置版本数
            test_03.addFamily(cf3);

            admin.createTable(test_03);
        }else {
            System.out.println("表已存在");
        }
     }

    //删除表
    @Test
    public void delete() throws IOException {

        Admin admin = conn.getAdmin();
//        Scanner sc = new Scanner(System.in);
//        System.out.println("请输入要删除的表名");
//        String name = sc.next();
        if (admin.tableExists(TableName.valueOf("test_02"))){
            admin.disableTable(TableName.valueOf("test_02"));
            admin.deleteTable(TableName.valueOf("test_02"));
        }else {
            System.out.println("表不存在");
        }
    }

    //修改表
    @Test
    public void alter() throws IOException {
        Admin admin = conn.getAdmin();
        TableName tableName = TableName.valueOf("test_01");
        //获取表
        HTableDescriptor hTableDescriptor = admin.getTableDescriptor(tableName);

        //获取表的列簇数组
        HColumnDescriptor[] columnFamilies = hTableDescriptor.getColumnFamilies();
        //遍历所有的列簇
        for (HColumnDescriptor columnFamily : columnFamilies) {
            //获取所有的列簇名称
            String cfname = columnFamily.getNameAsString();
            if ("cf1".equals(cfname)){
                columnFamily.setMaxVersions(5);//修改版本数为5
                columnFamily.setTimeToLive(20000);//修改版本数为20000
            }
        }
        admin.modifyTable(tableName,hTableDescriptor);

    }

    //插入数据
    @Test
    public void Put() throws IOException{
        Table test_01 = conn.getTable(TableName.valueOf("test_01"));

        Put put = new Put("001".getBytes());
//        put.addColumn("cf1".getBytes(),"name".getBytes(),"张三".getBytes());
        put.addColumn("cf1".getBytes(),"name".getBytes(),Bytes.toBytes("张三"));
        test_01.put(put);
    }

    //get获取数据
    @Test
    public void Get() throws IOException{
        Table test_01 = conn.getTable(TableName.valueOf("test_01"));

        Get get = new Get("001".getBytes());
        Result result = test_01.get(get);
        byte[] value = result.getValue("cf1".getBytes(), "name".getBytes());
//        System.out.println(value.toString());//[B@1dac5ef
        System.out.println(Bytes.toString(value));
    }

    //scan获取数据的第一种方式
    @Test
    public void Scan() throws IOException{

        Table test_01 = conn.getTable(TableName.valueOf("test_01"));

        Scan scan = new Scan();
        scan.setLimit(5);//最多只显示五条数据
        scan.withStartRow("0".getBytes());//从这个行键开始读取数据
        scan.withStopRow("1500".getBytes());//到这个行键结束读取数据

        ResultScanner scanner = test_01.getScanner(scan);
        //获取了一条条数据
        for (Result result : scanner) {
            String id = Bytes.toString(result.getRow());
            String name = Bytes.toString(result.getValue("info".getBytes(), "name".getBytes()));
            String age = Bytes.toString(result.getValue("info".getBytes(), "age".getBytes()));
            String gender = Bytes.toString(result.getValue("info".getBytes(), "gender".getBytes()));
            String clazz = Bytes.toString(result.getValue("info".getBytes(), "clazz".getBytes()));

            System.out.println(id+","+name+","+age+","+gender+","+clazz);
        }
    }

    //scan获取数据的第二种方式
    //适用于每条数据结构不唯一的情况下,直接遍历每条数据包含的所有的cell
    @Test
    public void scanWithUtil() throws IOException{

        Table test_01 = conn.getTable(TableName.valueOf("test_01"));
        Scan scan = new Scan();
        scan.setLimit(5);//最多只显示5条数据
        scan.withStartRow("0".getBytes());
        scan.withStopRow("150".getBytes());

        ResultScanner scanner = test_01.getScanner(scan);
        for (Result result : scanner) {
            String rowkey = Bytes.toString(result.getRow());
            System.out.println(rowkey+" ");
            for (Cell cell:result.listCells()){
                //列名
                String column = Bytes.toString(CellUtil.cloneQualifier(cell));
                //列簇名
                String cf = Bytes.toString(CellUtil.cloneFamily(cell));


            }
            
        }


    }

    //putall读取students.txt的数据,并写入hbase
    //批量插入
    @Test
    public void PutAll() throws IOException{
        //创建students表
        Admin admin = conn.getAdmin();
        if(!admin.tableExists(TableName.valueOf("students"))){
            HTableDescriptor students = new HTableDescriptor(TableName.valueOf("students"));
            HColumnDescriptor info = new HColumnDescriptor("info");
            students.addFamily(info);
            admin.createTable(students);
        }

        Table students = conn.getTable(TableName.valueOf("students"));

        BufferedReader br = new BufferedReader(new FileReader("D:\\BigDaTa\\JAVA项目\\ShuJia01\\data\\students.txt"));
        String line = null;
        ArrayList<Put> puts = new ArrayList<Put>();
        int size=10;
        while ((line=br.readLine())!=null){

            //读取每一行数据
            String[] split = line.split(",");
            String id = split[0];
            String name = split[1];
            String age = split[2];
            String gender = split[3];
            String clazz = split[4];

            Put put = new Put(id.getBytes());
            put.addColumn("info".getBytes(),"name".getBytes(),name.getBytes());
            put.addColumn("info".getBytes(),"age".getBytes(),age.getBytes());
            put.addColumn("info".getBytes(),"gender".getBytes(),gender.getBytes());
            put.addColumn("info".getBytes(),"clazz".getBytes(),clazz.getBytes());

            puts.add(put);
            if (puts.size()==size){
                students.put(puts);
                puts=new ArrayList<Put>();
            }


        }
        if (puts.size()!=0){
            students.put(puts);
        }


    }

    //putall读取students.txt的数据,并写入hbase
    //逐个插入
    @Test
    public void PutAllAgain() throws IOException{

        Admin admin = conn.getAdmin();
        if(!admin.tableExists(TableName.valueOf("students"))){
            HTableDescriptor test_01 = new HTableDescriptor(TableName.valueOf("students"));
            HColumnDescriptor info = new HColumnDescriptor("info");
            test_01.addFamily(info);
            admin.createTable(test_01);
        }

        Table test_01 = conn.getTable(TableName.valueOf("students"));

        BufferedReader br = new BufferedReader(new FileReader("D:\\BigDaTa\\JAVA项目\\ShuJia01\\data\\students.txt"));
        String line=null;
        while ((line=br.readLine())!=null){
            String[] split = line.split(",");
            String id = split[0];
            String name = split[1];
            String age = split[2];
            String gender = split[3];
            String clazz = split[4];

            Put put = new Put(id.getBytes());
            put.addColumn("info".getBytes(),"name".getBytes(),name.getBytes());
            put.addColumn("info".getBytes(),"age".getBytes(),age.getBytes());
            put.addColumn("info".getBytes(),"gender".getBytes(),gender.getBytes());
            put.addColumn("info".getBytes(),"clazz".getBytes(),clazz.getBytes());

            test_01.put(put);

        }

    }

     @After
    public void close() throws IOException {
        conn.close();
     }

}

HBASE连接Hive

将hbase中如下图的数据样式导入到hive中,在hive中创建一个外部表(主要用hive的外部表来接收不同数据源的数据),hbase表中是一个行键(id),一个列簇下面对应四个列(name,age,gender,clazz),在hive中创建对应的表即可
在这里插入图片描述
hive中创建相应的表:

create external table students_hbase
(
id string,
name string,
age string,
gender string, 
clazz string
)
stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
with serdeproperties ("hbase.columns.mapping" = "
:key,
info:name,
info:age,
info:gender,
info:clazz
")
tblproperties("hbase.table.name" = "default:students");

在这里插入图片描述
数据已导入

HBASE过滤器

我们会遇到一些这样的操作:比如过滤出“文科一班”的学生,在hive里面,直接一条sql就解决了问题,但相同的问题放到hbase里面,就需要用到过滤器来做操作

我理解的过滤器,其实不过如此(过滤器一般来说会和比较器一起使用):
简单来说,就是找到自己的需求,比如是对列进行操作,对列簇进行操作,还是对某一列的值操作,或者又是对所有列的值进行操作;在这基础上,在加上一个比较器,比如是正则比较器、二进制比较器,或者又是前缀比较器,二者合在一起,就是比较过滤器

比较器

比较运算符

  • LESS <
  • LESS_OR_EQUAL <=
  • EQUAL =
  • NOT_EQUAL <>
  • GREATER_OR_EQUAL >=
  • GREATER >
  • NO_OP 排除所有

常见的比较器

  • BinaryComparator
    按照字节索引排序比较指定字节数据
  • BinaryPrefixComparator
    只是比较左端前缀的数据是否相同
  • NullComparator
    判断给定的值是否为空
  • BitComparator
    按位比较
  • RegexStringComparator
    提供一个正则的比较器,仅支持EQUAL和非EQUAL
  • SubstringComparator
    判断提供的子串是否出现在原有的字符串中

过滤器

常见的过滤器

  • rowKey过滤器:RowFilter
  • 列簇过滤器:FamilyFilter
  • 列过滤器:QualifierFilter
  • 列值过滤器:ValueFilter
  • 单列值过滤器:SingleColumnValueFilter
  • 列值排除过滤器:SingleColumnValueExcludeFilter
  • rowkey前缀过滤器:PrefixFilter
  • 分页过滤器PageFilter

过滤器举例

使用列值过滤器单列值过滤器分别过滤出“文科一班”的所有学生
搭好架构:

Connection conn;
    Admin admin;
    Table students;
    TableName studentsT = TableName.valueOf("students");

    public void printScannerWithFilter() throws IOException {
        Scan scan = new Scan();
        ResultScanner scanner = students.getScanner(scan);
        for (Result result : scanner) { //一个resule就代表一行数据
            List<Cell> cells = result.listCells();

            String id = Bytes.toString(result.getRow());
            System.out.print(id + "\t");
            for (Cell cell : cells) {
//                String id = Bytes.toString(CellUtil.cloneRow(cell));
                String value = Bytes.toString(CellUtil.cloneValue(cell));
                System.out.print(value + "\t");
            }
            System.out.println();
        }
    }

    @Before
    public void init() throws IOException{
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","master,node1,node2");
        conn = ConnectionFactory.createConnection(conf);
        admin = conn.getAdmin();
        students = conn.getTable(studentsT);
    }

 @After
    public void close() throws IOException{
        conn.close();
    }

使用列值过滤器过滤出“文科一班”的学生:

//这个过滤,过滤的是所有的列,只要前面的任何一列是过滤符合标准的,就会对其进行过滤
    //使用正则比较器+列值过滤器->过滤出    文科一班    的学生
    //RegexStringComparator+ValueFilter
    //ValueFilter 会将比较器作用到所有的列值上,也就相当于作用在每一个cell上面,
    //把符合比较器规则的cell过滤出来,不符合的直接丢弃
    @Test
    public void RegexStringComparatorValueFilter() throws IOException {
        //过滤说到底也就是一种查询,查询有两种,一个get,一个scan,过滤是scan
        Scan scan = new Scan();

        //第一个填的是比较运算符(大于、小于、等于,什么的)
        //第二个填的是比较器,这里用的是正则表达式的比较器                                      ^文科.*班
        RegexStringComparator regexStringComparator = new RegexStringComparator(".*班");//文科开头,班结尾的班级
        ValueFilter valueFilter = new ValueFilter(
            CompareFilter.CompareOp.EQUAL,
                regexStringComparator
        );
        scan.setFilter(valueFilter);

        ResultScanner scanner = students.getScanner(scan);
        for (Result result : scanner) {
            List<Cell> cells = result.listCells();
//            for (Cell cell:cells){
//                String value = Bytes.toString(CellUtil.cloneValue(cell));
//                System.out.println(value);
//            }
//            for (Cell cell : cells) {
//                String column = Bytes.toString(cell.getFamilyArray());
//                String value = Bytes.toString(cell.getQualifierArray());
//                System.out.println(value);
//            }
            String name = Bytes.toString(result.getValue("info".getBytes(), "name".getBytes()));
            String age = Bytes.toString(result.getValue("info".getBytes(), "age".getBytes()));
            String gender = Bytes.toString(result.getValue("info".getBytes(), "gender".getBytes()));
            String clazz = Bytes.toString(result.getValue("info".getBytes(), "clazz".getBytes()));
            System.out.println(name+","+age+","+gender+","+clazz);
        }
    }

在这里插入图片描述
最后发现结果是这样的,也就是说,这个过滤,过滤的是所有的列,只要前面的任何一列是过滤符合标准的,就会对其进行过滤

使用单列值过滤器过滤出“文科一班”的学生:

//SingleColumnValueFilter               这个最后会输出这一列
    //SingleColumnValueExcludeFilter        这个最后会排除这一列
    //单列值过滤器
    @Test
    public void RegexStringComparatorSingleColumnValueFilter() throws IOException {
        Scan scan = new Scan();
//        SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter(
//                "info".getBytes(),
//                "clazz".getBytes(),
//                CompareFilter.CompareOp.EQUAL,
//                "文科一班".getBytes() //这里既可以传入字符数组,也可以传入一个比较器
//        );
        SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter(
                "info".getBytes(),
                "clazz".getBytes(),
                CompareFilter.CompareOp.EQUAL,
                new RegexStringComparator("^文科.*班$")
        );
        scan.setFilter(singleColumnValueFilter);
        
        ResultScanner scanner = students.getScanner(scan);
        for (Result result : scanner) { //一个resule就代表一行数据
            List<Cell> cells = result.listCells();

            String id = Bytes.toString(result.getRow());
            System.out.print(id+"\t");
            for (Cell cell : cells) {
//                String id = Bytes.toString(CellUtil.cloneRow(cell));
                String value = Bytes.toString(CellUtil.cloneValue(cell));
                System.out.print(value+"\t");
            }
            System.out.println();

//            String name = Bytes.toString(result.getValue("info".getBytes(), "name".getBytes()));
//            String age = Bytes.toString(result.getValue("info".getBytes(), "age".getBytes()));
//            String gender = Bytes.toString(result.getValue("info".getBytes(), "gender".getBytes()));
//            String clazz = Bytes.toString(result.getValue("info".getBytes(), "clazz".getBytes()));
//            System.out.println(name+","+age+","+gender+","+clazz);
        }

    }

在这里插入图片描述
过滤出了文科一班的所有学生

过滤器举例的所有代码

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.filter.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.List;

public class Demo4Filter {

    Connection conn;
    Admin admin;
    Table students;
    TableName studentsT = TableName.valueOf("students");

    public void printScannerWithFilter() throws IOException {
        Scan scan = new Scan();
        ResultScanner scanner = students.getScanner(scan);
        for (Result result : scanner) { //一个resule就代表一行数据
            List<Cell> cells = result.listCells();

            String id = Bytes.toString(result.getRow());
            System.out.print(id + "\t");
            for (Cell cell : cells) {
//                String id = Bytes.toString(CellUtil.cloneRow(cell));
                String value = Bytes.toString(CellUtil.cloneValue(cell));
                System.out.print(value + "\t");
            }
            System.out.println();
        }
    }

    @Before
    public void init() throws IOException{
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","master,node1,node2");
        conn = ConnectionFactory.createConnection(conf);
        admin = conn.getAdmin();
        students = conn.getTable(studentsT);
    }

    //这个过滤,过滤的是所有的列,只要前面的任何一列是过滤符合标准的,就会对其进行过滤
    //使用正则比较器+列值过滤器->过滤出    文科一班    的学生
    //RegexStringComparator+ValueFilter
    //ValueFilter 会将比较器作用到所有的列值上,也就相当于作用在每一个cell上面,
    //把符合比较器规则的cell过滤出来,不符合的直接丢弃
    @Test
    public void RegexStringComparatorValueFilter() throws IOException {
        //过滤说到底也就是一种查询,查询有两种,一个get,一个scan,过滤是scan
        Scan scan = new Scan();

        //第一个填的是比较运算符(大于、小于、等于,什么的)
        //第二个填的是比较器,这里用的是正则表达式的比较器                                      ^文科.*班
        RegexStringComparator regexStringComparator = new RegexStringComparator(".*班");//文科开头,班结尾的班级
        ValueFilter valueFilter = new ValueFilter(
            CompareFilter.CompareOp.EQUAL,
                regexStringComparator
        );
        scan.setFilter(valueFilter);

        printScannerWithFilter();

//        ResultScanner scanner = students.getScanner(scan);
//        for (Result result : scanner) {
//            List<Cell> cells = result.listCells();
            for (Cell cell:cells){
                String value = Bytes.toString(CellUtil.cloneValue(cell));
                System.out.println(value);
            }
            for (Cell cell : cells) {
                String column = Bytes.toString(cell.getFamilyArray());
                String value = Bytes.toString(cell.getQualifierArray());
                System.out.println(value);
            }
//            String name = Bytes.toString(result.getValue("info".getBytes(), "name".getBytes()));
//            String age = Bytes.toString(result.getValue("info".getBytes(), "age".getBytes()));
//            String gender = Bytes.toString(result.getValue("info".getBytes(), "gender".getBytes()));
//            String clazz = Bytes.toString(result.getValue("info".getBytes(), "clazz".getBytes()));
//            System.out.println(name+","+age+","+gender+","+clazz);
//        }
    }
    /*
    过滤条件是这样的:^文科.*班
    //null,null,null,文科五班
    //null,null,null,文科六班
    //null,null,null,文科六班
    //null,null,null,文科一班
    //null,null,null,文科一班
    //null,null,null,文科四班
    //null,null,null,文科六班
    //null,null,null,文科三班
    //null,null,null,文科五班
    最后输出的结果格式是这样的

    如果修改了一下最后一条数据
    过滤条件是这样的    .*班
    null,null,null,理科五班
    null,null,null,文科三班
    null,null,null,理科六班
    null,null,null,理科四班
    null,null,null,文科五班
    班班班,null,null,理科六班
    最后的输出结构就这样了
     */


    //SingleColumnValueFilter               这个最后会输出这一列
    //SingleColumnValueExcludeFilter        这个最后会排除这一列
    //单列值过滤器
    @Test
    public void RegexStringComparatorSingleColumnValueFilter() throws IOException {
        Scan scan = new Scan();
//        SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter(
//                "info".getBytes(),
//                "clazz".getBytes(),
//                CompareFilter.CompareOp.EQUAL,
//                "文科一班".getBytes() //这里既可以传入字符数组,也可以传入一个比较器
//        );
        SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter(
                "info".getBytes(),
                "clazz".getBytes(),
                CompareFilter.CompareOp.EQUAL,
                new RegexStringComparator("^文科.*班$")
        );
        scan.setFilter(singleColumnValueFilter);

        printScannerWithFilter();

//        ResultScanner scanner = students.getScanner(scan);
//        for (Result result : scanner) { //一个resule就代表一行数据
//            List<Cell> cells = result.listCells();
//
//            String id = Bytes.toString(result.getRow());
//            System.out.print(id+"\t");
//            for (Cell cell : cells) {
                String id = Bytes.toString(CellUtil.cloneRow(cell));
//                String value = Bytes.toString(CellUtil.cloneValue(cell));
//                System.out.print(value+"\t");
//            }
//            System.out.println();
//
            String name = Bytes.toString(result.getValue("info".getBytes(), "name".getBytes()));
            String age = Bytes.toString(result.getValue("info".getBytes(), "age".getBytes()));
            String gender = Bytes.toString(result.getValue("info".getBytes(), "gender".getBytes()));
            String clazz = Bytes.toString(result.getValue("info".getBytes(), "clazz".getBytes()));
            System.out.println(name+","+age+","+gender+","+clazz);
//        }
//
    }

    //过滤出列里面包含字母a的所有列的数据
    @org.junit.Test
    public void SubstringComparatorSingleColumnValueFilter() throws IOException {
        Table students = conn.getTable(TableName.valueOf("students"));

        SubstringComparator a = new SubstringComparator("a");
        QualifierFilter qualifierFilter = new QualifierFilter(
                CompareFilter.CompareOp.EQUAL,
                a
        );

        Scan scan = new Scan();
        scan.setFilter(qualifierFilter);
        ResultScanner scanner = students.getScanner(scan);
        for (Result result : scanner) {
            List<Cell> cells = result.listCells();
            for (Cell cell : cells) {
                String s = Bytes.toString(cell.getValue());
                System.out.print(s+"\t");
            }
            System.out.println();
        }



        students.getScanner(scan);


    }

    @org.junit.Test
    //过滤出rowkey比1500100010小的所有值来
    public void BinaryPrefixComparatorRowFilter() throws IOException {
        Table students = conn.getTable(TableName.valueOf("students"));
        Scan scan = new Scan();
        BinaryComparator binaryComparator = new BinaryComparator("1500100010".getBytes());
        RowFilter rowFilter = new RowFilter(
                CompareFilter.CompareOp.LESS,
                binaryComparator
        );
        scan.setFilter(rowFilter);
        ResultScanner scanner = students.getScanner(scan);

        for (Result result : scanner) {
            String id = Bytes.toString(result.getRow());
            List<Cell> cells = result.listCells();
            System.out.print(id+"\t");
            for (Cell cell : cells) {
                String value = Bytes.toString(CellUtil.cloneValue(cell));
                System.out.print(value+"\t");
            }
            System.out.println();
        }

    }

    @org.junit.Test
    //查询列簇名包含in的所有列簇下面的数据
    public void SubstringFamilyFilter() throws IOException {
        Table students = conn.getTable(TableName.valueOf("students"));

        SubstringComparator substringComparator = new SubstringComparator("in");
        FamilyFilter familyFilter = new FamilyFilter(
                CompareFilter.CompareOp.EQUAL,
                substringComparator
        );
        Scan scan = new Scan();
        scan.setFilter(familyFilter);
        ResultScanner scanner = students.getScanner(scan);
        for (Result result : scanner) {
            String id = Bytes.toString(result.getRow());
            System.out.print(id);
            List<Cell> cells = result.listCells();
            for (Cell cell : cells) {
                String value = Bytes.toString(CellUtil.cloneValue(cell));
                System.out.print(value+"\t");
            }
            System.out.println();
        }

    }

    @org.junit.Test
    //查询列簇以info开头的列簇下面的数据
    public void BinaryPrefixComparatorFamilyFilter() throws IOException {
        Table students = conn.getTable(TableName.valueOf("students"));

        BinaryPrefixComparator binaryPrefixComparator = new BinaryPrefixComparator("info".getBytes());
        FamilyFilter familyFilter = new FamilyFilter(
                CompareFilter.CompareOp.EQUAL,
                binaryPrefixComparator
        );
        Scan scan = new Scan();
        ResultScanner scanner = students.getScanner(scan);
        String id;
        for (Result result : scanner) {
            id = Bytes.toString(result.getRow());
            List<Cell> cells = result.listCells();
            System.out.print(id+"\t");
            for (Cell cell : cells) {
                String value = Bytes.toString(CellUtil.cloneValue(cell));
                System.out.print(value+"\t");
            }
            System.out.println();
        }

    }


    @After
    public void close() throws IOException{
        conn.close();
    }
}


感谢阅读,我是啊帅和和,一位大数据专业大四学生,祝你快乐。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

啊帅和和。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值