当前位置:   article > 正文

java操作HBase

java操作hbase

创建一个学生信息表,用来存储学生的姓名(姓名作为行键,且假设姓名不会重复)以及考试成绩,其中考试成绩(score)是一个列族,存储了各个科目的考试成绩。然后向student中添加数据

1、HBase依赖

  1. <dependency>
  2. <groupId>org.apache.hbase</groupId>
  3. <artifactId>hbase-server</artifactId>
  4. <version>2.2.0</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.apache.hbase</groupId>
  8. <artifactId>hbase-client</artifactId>
  9. <version>2.2.0</version>
  10. </dependency>

2、HBase数据源

  1. package com.example.demo.config;
  2. import com.example.demo.service.ICodeService;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.boot.ApplicationArguments;
  5. import org.springframework.boot.ApplicationRunner;
  6. import org.springframework.core.annotation.Order;
  7. import org.springframework.stereotype.Component;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Map;
  11. @Component
  12. @Order(1)
  13. public class NmsHBaseSource implements ApplicationRunner {
  14. // 管理HBase的配置信息
  15. public static Configuration conf;
  16. // 管理HBase的连接
  17. public static Connection conn;
  18. // 管理HBase数据库的连接
  19. public static Admin admin;
  20. @Override
  21. public void run(ApplicationArguments args) throws Exception {
  22. conf = HBaseConfiguration.create();
  23. System.setProperty("HADOOP_USER_NAME", "hadoop");
  24. conf.set("HADOOP_USER_NAME", "hadoop");
  25. conf.set("hbase.root.dir", "hdfs://master:9000/hbase");
  26. conf.set("hbase.zookeeper.quorum", "master");//配置Zookeeper的ip地址
  27. conf.set("hbase.zookeeper.property.clientPort", "2181");//配置zookeeper的端口
  28. conn = ConnectionFactory.createConnection(conf);
  29. admin = conn.getAdmin();
  30. }
  31. /**
  32. * 关闭所有连接
  33. *
  34. * @throws IOException 可能出现的异常
  35. */
  36. public static void close() throws IOException {
  37. if (admin != null)
  38. admin.close();
  39. if (conn != null)
  40. conn.close();
  41. }
  42. /**
  43. * 创建表
  44. * @param myTableName 表名
  45. * @param colFamily 列族名的数组
  46. * @throws IOException 可能出现的异常
  47. */
  48. public static void createTable(String myTableName, String[] colFamily) throws IOException {
  49. TableName tableName = TableName.valueOf(myTableName);
  50. if (admin.tableExists(tableName)) {
  51. logger.info(myTableName + "表已经存在");
  52. } else {
  53. //HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
  54. //for (String str : colFamily) {
  55. // HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
  56. // hTableDescriptor.addFamily(hColumnDescriptor);
  57. //}
  58. //admin.createTable(hTableDescriptor);
  59. TableDescriptorBuilder tableDescriptor = TableDescriptorBuilder.newBuilder(tableName);
  60. for (String str : colFamily) {
  61. ColumnFamilyDescriptor columnFamily = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(str)).build();// 构建列族对象
  62. tableDescriptor.setColumnFamily(columnFamily); // 设置列族
  63. }
  64. admin.createTable(tableDescriptor.build()); // 创建表
  65. }
  66. }
  67. /**
  68. * 添加数据
  69. * @param tableName 表名
  70. * @param rowkey 行键
  71. * @param colFamily 列族
  72. * @param col 列
  73. * @param value 值
  74. * @throws IOException 可能出现的异常
  75. */
  76. public static void insertData(String tableName,String rowkey,String colFamily,String col,String value) throws IOException {
  77. Table table = conn.getTable(TableName.valueOf(tableName));
  78. Put put = new Put(rowkey.getBytes());
  79. put.addColumn(colFamily.getBytes(),col.getBytes(),value.getBytes());
  80. table.put(put);
  81. table.close();
  82. }
  83. /**
  84. * 根据行键删除数据
  85. * @param tableName 表名
  86. * @param rowkey 行键
  87. * @throws IOException 可能出现的异常
  88. */
  89. public static void deleteData(String tableName,String rowkey) throws IOException {
  90. Table table = conn.getTable(TableName.valueOf(tableName));
  91. Delete delete = new Delete(rowkey.getBytes());
  92. table.delete(delete);
  93. table.close();
  94. }
  95. /**
  96. * 获取数据
  97. * @param tableName 表名
  98. * @param rowkey 行键
  99. * @param colFamily 列族
  100. * @param col 列
  101. * @throws IOException 可能出现的异常
  102. */
  103. public static void getData(String tableName,String rowkey,String colFamily,String col) throws IOException {
  104. Table table = conn.getTable(TableName.valueOf(tableName));
  105. Get get = new Get(rowkey.getBytes());
  106. get.addColumn(colFamily.getBytes(),col.getBytes());
  107. Result result = table.get(get);
  108. System.out.println(new String(result.getValue(colFamily.getBytes(),col.getBytes())));
  109. table.close();
  110. }
  111. public static void main(String[] args) throws IOException {
  112. init();
  113. createTable("student",new String[]{"score"});
  114. insertData("student","zhangsan","score","English","69");
  115. insertData("student","zhangsan","score","Math","86");
  116. insertData("student","zhangsan","score","Computer","77");
  117. getData("student","zhangsan","score","Computer");
  118. close();
  119. }
  120. }

3、Hbase过滤器查询

过滤器可以分为两种:比较过滤器和专用过滤器

比较过滤器 

LESS —— 小于

LESS_OR_EQUAL —— 小于等于

EQUAL —— 等于

NOT_EQUAL —— 不等于

GREATER_OR_EQUAL —— 大于等于

GREATER —— 大于

NO_OP —— 排除所有

专用过滤器

BinaryComparator —— 匹配完整字节数组,Bytes.compareTo(byte[])

BinaryPrefixComparator —— 匹配字节数组前缀
NullComparator —— 判断给定的是否为空
BitComparator —— 按位比较
RegexStringComparator —— 提供一个正则的比较器,仅支持 EQUAL 和非EQUAL
SubstringComparator —— 判断提供的子串是否出现在value中

  • 3.1、ResultScanner结果处理handleResultScanner

  1. /**
  2. * ResultScanner结果解析
  3. */
  4. public void handleResultScanner(ResultScanner scanner) throws IOException {
  5. //因为ResultScanner类继承了迭代器
  6. //使用增强for循环遍历
  7. for (Result rs : scanner) {
  8. String id = Bytes.toString(rs.getRow());
  9. System.out.println("当前行的rowkey为:" + id);
  10. //继续增强for循环得到每一行中的每一个单元格(列)
  11. //获取一行中的所有单元格
  12. for (Cell cell : rs.listCells()) {
  13. //获取该单元格属于的列簇
  14. String family = Bytes.toString(CellUtil.cloneFamily(cell));
  15. //获取该单元格的列名
  16. String colName = Bytes.toString(CellUtil.cloneQualifier(cell));
  17. //获取该单元格的列值
  18. String value = Bytes.toString(CellUtil.cloneValue(cell));
  19. System.out.println(family + ":" + colName + "的值为:" + value);
  20. }
  21. String name = Bytes.toString(rs.getValue("info".getBytes(), "name".getBytes()));
  22. String age = Bytes.toString(rs.getValue("info".getBytes(), "age".getBytes()));
  23. String gender = Bytes.toString(rs.getValue("info".getBytes(), "gender".getBytes()));
  24. String clazz = Bytes.toString(rs.getValue("info".getBytes(), "clazz".getBytes()));
  25. System.out.println("学号:" + id + ",姓名:" + name + ",年龄:" + age + ",性别:" + gender + ",班级:" + clazz);
  26. }

3.2、rowKey过滤器RowFilter

  1. /**
  2. * 行键过滤器
  3. * 通过RowFilter与BinaryComparator过滤比rowKey 1500100010小的所有值出来
  4. */
  5. @Test
  6. public void RowFilter1(){
  7. try {
  8. //获取表的实例
  9. TableName students = TableName.valueOf("students");
  10. Table studentTable = conn.getTable(students);
  11. BinaryComparator binaryComparator = new BinaryComparator("1500100010".getBytes());
  12. //创建一个行键过滤器的对象
  13. RowFilter rowFilter = new RowFilter(CompareOperator.LESS, binaryComparator);
  14. Scan scan = new Scan();
  15. scan.setFilter(rowFilter);
  16. ResultScanner scanner = studentTable.getScanner(scan);
  17. handleResultScanner(scanner);
  18. } catch (IOException e) {
  19. e.printStackTrace();
  20. }
  21. }

3.3、列族过滤器FamilyFilter

  1. /**
  2. * 通过FamilyFilter与SubstringComparator查询列簇名包含in的所有列簇下面的数据
  3. */
  4. @Test
  5. public void FamilyFilter1(){
  6. try {
  7. //获取表的实例
  8. TableName students = TableName.valueOf("students");
  9. Table studentTable = conn.getTable(students);
  10. //创建一个比较器对象
  11. //只要列簇名中包含了in,就把该列簇下的所有列查询出来
  12. SubstringComparator substringComparator = new SubstringComparator("in");
  13. //创建列簇过滤器
  14. FamilyFilter familyFilter = new FamilyFilter(CompareOperator.EQUAL, substringComparator);
  15. Scan scan = new Scan();
  16. scan.setFilter(familyFilter);
  17. //获取数据
  18. ResultScanner scanner = studentTable.getScanner(scan);
  19. handleResultScanner(scanner);
  20. } catch (IOException e) {
  21. e.printStackTrace();
  22. }
  23. }
  24. /**
  25. * 通过FamilyFilter与 BinaryPrefixComparator 过滤出列簇以i开头的列簇下的所有数据
  26. *
  27. */
  28. @Test
  29. public void FamilyFilter2(){
  30. try {
  31. //获取表的实例
  32. TableName students = TableName.valueOf("students");
  33. Table studentTable = conn.getTable(students);
  34. //创建前缀比较器
  35. BinaryPrefixComparator binaryPrefixComparator = new BinaryPrefixComparator("i".getBytes());
  36. //创建列簇过滤器
  37. FamilyFilter familyFilter = new FamilyFilter(CompareOperator.EQUAL, binaryPrefixComparator);
  38. Scan scan = new Scan();
  39. scan.setFilter(familyFilter);
  40. ResultScanner scanner = studentTable.getScanner(scan);
  41. handleResultScanner(scanner);
  42. } catch (IOException e) {
  43. e.printStackTrace();
  44. }
  45. }

3.4、列过滤器QualifierFilter

  1. /**
  2. * 通过QualifierFilter与SubstringComparator查询列名包含ge的列的值
  3. *
  4. */
  5. @Test
  6. public void QualifierFilter1(){
  7. try {
  8. //获取表的实例
  9. TableName students = TableName.valueOf("students");
  10. Table studentTable = conn.getTable(students);
  11. //创建包含比较器
  12. //age
  13. //gender
  14. SubstringComparator substringComparator = new SubstringComparator("ge");
  15. //创建一个列过滤器
  16. QualifierFilter qualifierFilter = new QualifierFilter(CompareOperator.EQUAL, substringComparator);
  17. Scan scan = new Scan();
  18. scan.setFilter(qualifierFilter);
  19. ResultScanner scanner = studentTable.getScanner(scan);
  20. handleResultScanner(scanner);
  21. } catch (IOException e) {
  22. e.printStackTrace();
  23. }
  24. }
  25. /**
  26. *
  27. * 通过QualifierFilter与SubstringComparator查询列名包含ge的列的值
  28. */
  29. @Test
  30. public void QualifierFilter2(){
  31. try {
  32. //获取表的实例
  33. TableName students = TableName.valueOf("students");
  34. Table studentTable = conn.getTable(students);
  35. SubstringComparator substringComparator = new SubstringComparator("am");
  36. //创建列过滤器
  37. QualifierFilter qualifierFilter = new QualifierFilter(CompareOperator.EQUAL, substringComparator);
  38. Scan scan = new Scan();
  39. scan.setFilter(qualifierFilter);
  40. ResultScanner scanner = studentTable.getScanner(scan);
  41. handleResultScanner(scanner);
  42. } catch (IOException e) {
  43. e.printStackTrace();
  44. }
  45. }

3.5、列值过滤器ValueFilter

  1. /**
  2. * 通过ValueFilter与BinaryPrefixComparator过滤出所有的cell中值以 "张" 开头的学生
  3. */
  4. @Test
  5. public void ValueFilter1() {
  6. try {
  7. //获取表的实例
  8. TableName students = TableName.valueOf("students");
  9. Table studentTable = conn.getTable(students);
  10. //创建前缀比较器
  11. BinaryPrefixComparator binaryPrefixComparator = new BinaryPrefixComparator("张".getBytes());
  12. //创建列值过滤器的对象
  13. ValueFilter valueFilter = new ValueFilter(CompareOperator.EQUAL, binaryPrefixComparator);
  14. Scan scan = new Scan();
  15. scan.setFilter(valueFilter);
  16. ResultScanner scanner = studentTable.getScanner(scan);
  17. //因为ResultScanner类继承了迭代器
  18. //使用增强for循环遍历
  19. // for (Result rs : scanner) {
  20. // String id = Bytes.toString(rs.getRow());
  21. // System.out.println("当前行的rowkey为:" + id);
  22. // //继续增强for循环得到每一行中的每一个单元格(列)
  23. // //获取一行中的所有单元格
  24. // for (Cell cell : rs.listCells()) {
  25. // //获取该单元格属于的列簇
  26. // String family = Bytes.toString(CellUtil.cloneFamily(cell));
  27. // //获取该单元格的列名
  28. // String colName = Bytes.toString(CellUtil.cloneQualifier(cell));
  29. // //获取该单元格的列值
  30. // String value = Bytes.toString(CellUtil.cloneValue(cell));
  31. // System.out.println(family + ":" + colName + "的值为:" + value);
  32. // }
  33. // }
  34. handleResultScanner(scanner);
  35. } catch (IOException e) {
  36. e.printStackTrace();
  37. }
  38. }
  39. /**
  40. * 过滤出文科的学生,只会返回以文科开头的数据列,其他列的数据不符合条件,不会返回
  41. */
  42. @Test
  43. public void ValueFilter12(){
  44. try {
  45. //获取表的实例
  46. HTableInterface students = conn.getTable("students");
  47. //创建正则比较器
  48. RegexStringComparator regexStringComparator = new RegexStringComparator("^文科.*");
  49. //创建列值过滤器
  50. ValueFilter valueFilter = new ValueFilter(CompareFilter.CompareOp.EQUAL, regexStringComparator);
  51. Scan scan = new Scan();
  52. scan.setFilter(valueFilter);
  53. ResultScanner scanner = students.getScanner(scan);
  54. handleResultScanner(scanner);
  55. } catch (IOException e) {
  56. e.printStackTrace();
  57. }
  58. }

3.6、单列值过滤器 SingleColumnValueFilter

  1. /**
  2. * 单列值过滤器
  3. * SingleColumnValueFilter会返回满足条件的cell所在行的所有cell的值(即会返回一行数据)
  4. *
  5. * 通过SingleColumnValueFilter与查询文科班所有学生信息
  6. */
  7. @Test
  8. public void SingleColumnValueFilter(){
  9. try {
  10. //获取表的实例
  11. TableName students = TableName.valueOf("students");
  12. Table studentTable = conn.getTable(students);
  13. //创建一个正则比较器
  14. RegexStringComparator regexStringComparator = new RegexStringComparator("^文科.*");
  15. //创建单列值过滤器对象
  16. SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter(
  17. "info".getBytes(),
  18. "clazz".getBytes(),
  19. CompareOperator.EQUAL,
  20. regexStringComparator
  21. );
  22. Scan scan = new Scan();
  23. scan.setFilter(singleColumnValueFilter);
  24. ResultScanner scanner = studentTable.getScanner(scan);
  25. handleResultScanner(scanner);
  26. } catch (IOException e) {
  27. e.printStackTrace();
  28. }
  29. }

3.7、列值排除过滤器SingleColumnValueExcludeFilter

  1. /**
  2. * 列值排除过滤器
  3. * 与SingleColumnValueFilter相反,会排除掉指定的列,其他的列全部返回
  4. *
  5. * 通过SingleColumnValueExcludeFilter与BinaryComparator查询文科一班所有学生信息,最终不返回clazz列
  6. */
  7. @Test
  8. public void SingleColumnValueExcludeFilter(){
  9. try {
  10. //获取表的实例
  11. TableName students = TableName.valueOf("students");
  12. Table studentTable = conn.getTable(students);
  13. //创建一个二进制比较器
  14. BinaryComparator binaryComparator = new BinaryComparator("文科一班".getBytes());
  15. //创建一个列值排除过滤器
  16. SingleColumnValueExcludeFilter singleColumnValueExcludeFilter = new SingleColumnValueExcludeFilter(
  17. "info".getBytes(),
  18. "clazz".getBytes(),
  19. CompareOperator.EQUAL,
  20. binaryComparator
  21. );
  22. Scan scan = new Scan();
  23. scan.setFilter(singleColumnValueExcludeFilter);
  24. ResultScanner scanner = studentTable.getScanner(scan);
  25. handleResultScanner(scanner);
  26. } catch (IOException e) {
  27. e.printStackTrace();
  28. }
  29. }

3.8、rowKey前缀过滤器PrefixFilter

  1. /**
  2. * rowkey前缀过滤器
  3. *
  4. * 通过PrefixFilter查询以150010008开头的所有前缀的rowkey
  5. */
  6. @Test
  7. public void PrefixFilter(){
  8. try {
  9. //获取表的实例
  10. TableName students = TableName.valueOf("students");
  11. Table studentTable = conn.getTable(students);
  12. //创建rowkey前缀过滤器
  13. PrefixFilter prefixFilter = new PrefixFilter("150010008".getBytes());
  14. Scan scan = new Scan();
  15. scan.setFilter(prefixFilter);
  16. ResultScanner scanner = studentTable.getScanner(scan);
  17. handleResultScanner(scanner);
  18. } catch (IOException e) {
  19. e.printStackTrace();
  20. }
  21. }

3.9、分页过滤器PageFilter

  1. /**
  2. * 分页过滤器
  3. * 分页有两个条件
  4. * pageNum 第几页
  5. * pageSize 每页有几条
  6. */
  7. @Test
  8. public void pageFilter() throws IOException {
  9. int pageNum = 3;
  10. int pageSize = 2;
  11. /*
  12. 分为两种情况判断:
  13. 第一页
  14. 其他页
  15. */
  16. if (pageNum == 1){
  17. Scan scan = new Scan();
  18. //设置起始rowKey
  19. scan.setStartRow("".getBytes());
  20. //设置最大的返回结果,返回pageSize条
  21. scan.setMaxResultSize(pageSize);
  22. //分页过滤器
  23. PageFilter pageFilter = new PageFilter(pageSize);
  24. scan.setFilter(pageFilter);
  25. ResultScanner resultScanner = table.getScanner(scan);
  26. for (Result result : resultScanner) {
  27. byte[] row = result.getRow();
  28. System.out.println("数据的rowKey为" + Bytes.toString(row));
  29. List<Cell> cells = result.listCells();
  30. for (Cell cell : cells) {
  31. byte[] qualifier = cell.getQualifier();
  32. byte[] family = cell.getFamily();
  33. byte[] value = cell.getValue();
  34. //id列和age列是整型数据
  35. if ("f1".equals(Bytes.toString(family)) && "id".equals(Bytes.toString(qualifier)) || "age".equals(Bytes.toString(value))){
  36. System.out.println("列族为"+Bytes.toString(family)+"列名为"+Bytes.toString(qualifier)+"列值为"+Bytes.toInt(value));
  37. } else {
  38. System.out.println("列族为"+Bytes.toString(family)+"列名为"+Bytes.toString(qualifier)+"列值为"+Bytes.toString(value));
  39. }
  40. }
  41. }
  42. } else {
  43. String startRow = "";
  44. Scan scan = new Scan();
  45. /*
  46. 第二页的起始rowKey = 第一页的结束rowKey + 1
  47. 第三页的起始rowKey = 第二页的结束rowKey + 1
  48. */
  49. int resultSize = (pageNum - 1) * pageSize + 1;
  50. scan.setMaxResultSize(resultSize);
  51. //设置一次性往前扫描5条,最后一个rowKey是第三页起始rowKey
  52. PageFilter pageFilter = new PageFilter(resultSize);
  53. scan.setFilter(pageFilter);
  54. //resultScanner里面有5条数据
  55. ResultScanner scanner = table.getScanner(scan);
  56. for (Result result : scanner) {
  57. //获取rowKey
  58. byte[] row = result.getRow();
  59. //最后一次循环遍历 rowKey为0005
  60. startRow = Bytes.toString(row);
  61. }
  62. Scan scan1 = new Scan();
  63. scan1.setStartRow(startRow.getBytes());
  64. scan1.setMaxResultSize(pageSize);
  65. PageFilter pageFilter1 = new PageFilter(pageSize);
  66. scan1.setFilter(pageFilter1);
  67. ResultScanner scanner1 = table.getScanner(scan1);
  68. for (Result result : scanner1) {
  69. byte[] row = result.getRow();
  70. System.out.println("数据的rowKey为" + Bytes.toString(row));
  71. List<Cell> cells = result.listCells();
  72. for (Cell cell : cells) {
  73. // byte[] qualifier = cell.getQualifier();
  74. // byte[] family = cell.getFamily();
  75. // byte[] value = cell.getValue();
  76. String family = Bytes.toString(CellUtil.cloneFamily(cell));
  77. //获取该单元格的列名
  78. String colName = Bytes.toString(CellUtil.cloneQualifier(cell));
  79. //获取该单元格的列值
  80. String value = Bytes.toString(CellUtil.cloneValue(cell));
  81. //id列和age列是整型数据
  82. if ("f1".equals(Bytes.toString(family)) && "id".equals(Bytes.toString(qualifier)) || "age".equals(Bytes.toString(value))){
  83. System.out.println("列族为"+Bytes.toString(family)+"列名为"+Bytes.toString(qualifier)+"列值为"+Bytes.toInt(value));
  84. } else {
  85. System.out.println("列族为"+Bytes.toString(family)+"列名为"+Bytes.toString(qualifier)+"列值为"+Bytes.toString(value));
  86. }
  87. }
  88. }
  89. }
  90. }

3.10、多过滤器综合查询FilterList

  1. /**
  2. * 通过运用4种比较器过滤出姓于,年纪大于23岁,性别为女,且是理科的学生。
  3. *
  4. * 正则比较器 RegexStringComparator
  5. * 包含比较器 SubstringComparator
  6. * 二进制前缀比较器 BinaryPrefixComparator
  7. * 二进制比较器 BinaryComparator
  8. *
  9. */
  10. @Test
  11. public void FilterData1(){
  12. try {
  13. //获取表的实例
  14. TableName students = TableName.valueOf("students");
  15. Table studentTable = conn.getTable(students);
  16. /**
  17. * 第一个过滤器,过滤出是理科开头的班级
  18. */
  19. RegexStringComparator regexStringComparator = new RegexStringComparator("^理科.*");
  20. //单列值过滤器
  21. SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter("info".getBytes(), "clazz".getBytes(),
  22. CompareOperator.EQUAL, regexStringComparator);
  23. /**
  24. * 第二个过滤器,过滤出性别是女生的
  25. */
  26. SubstringComparator substringComparator = new SubstringComparator("女");
  27. SingleColumnValueFilter singleColumnValueFilter1 = new SingleColumnValueFilter("info".getBytes(), "gender".getBytes(),
  28. CompareOperator.EQUAL, substringComparator);
  29. /**
  30. * 第三个过滤器,过滤出年龄大于23岁的
  31. */
  32. BinaryComparator binaryComparator = new BinaryComparator("20".getBytes());
  33. SingleColumnValueFilter singleColumnValueFilter2 = new SingleColumnValueFilter("info".getBytes(), "age".getBytes(),
  34. CompareOperator.GREATER, binaryComparator);
  35. /**
  36. * 第四个过滤器,过滤出姓于的学生
  37. */
  38. BinaryPrefixComparator binaryPrefixComparator = new BinaryPrefixComparator("于".getBytes());
  39. SingleColumnValueFilter singleColumnValueFilter3 = new SingleColumnValueFilter("info".getBytes(), "name".getBytes(),
  40. CompareOperator.EQUAL, binaryPrefixComparator);
  41. Scan scan = new Scan();
  42. //要想实现多个需求同时过滤,就需要创建多个过滤器,添加到一个过滤器列表中
  43. //然后将过滤器列表传给扫描器scan
  44. FilterList filterList = new FilterList();
  45. filterList.addFilter(singleColumnValueFilter);
  46. filterList.addFilter(singleColumnValueFilter1);
  47. filterList.addFilter(singleColumnValueFilter2);
  48. filterList.addFilter(singleColumnValueFilter3);
  49. scan.setFilter(filterList);
  50. ResultScanner scanner = studentTable.getScanner(scan);
  51. handleResultScanner(scanner);
  52. } catch (IOException e) {
  53. e.printStackTrace();
  54. }
  55. }
  56. /**
  57. * 过滤出学号是以15001001开头的文科学生
  58. */
  59. @Test
  60. public void filterData2(){
  61. try {
  62. //获取表的实例
  63. TableName students = TableName.valueOf("students");
  64. Table studentTable = conn.getTable(students);
  65. /**
  66. * 创建第一个过滤器,过滤是以15001001开头的rowkey
  67. */
  68. BinaryPrefixComparator binaryPrefixComparator = new BinaryPrefixComparator("15001001".getBytes());
  69. //创建行键过滤器
  70. RowFilter rowFilter = new RowFilter(CompareOperator.EQUAL, binaryPrefixComparator);
  71. /**
  72. * 创建第二个过滤器,过滤出文科的学生
  73. */
  74. RegexStringComparator regexStringComparator = new RegexStringComparator("^文科.*");
  75. SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter("info".getBytes(), "clazz".getBytes(),
  76. CompareOperator.EQUAL,
  77. regexStringComparator);
  78. FilterList filterList = new FilterList();
  79. filterList.addFilter(rowFilter);
  80. filterList.addFilter(singleColumnValueFilter);
  81. Scan scan = new Scan();
  82. scan.setFilter(filterList);
  83. ResultScanner scanner = studentTable.getScanner(scan);
  84. handleResultScanner(scanner);
  85. } catch (IOException e) {
  86. e.printStackTrace();
  87. }
  88. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/419746
推荐阅读
相关标签
  

闽ICP备14008679号