大数据从入门到实战 - HBase高级特性:过滤器(一)
叮嘟!这里是小啊呜的学习课程资料整理。好记性不如烂笔头,今天也是努力进步的一天。一起加油进阶吧!
![在这里插入图片描述](https://img-blog.csdnimg.cn/202003012210578.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzNTQzNzg5,size_16,color_FFFFFF,t_70#pic_center)
一、关于此次实践
1、实战简介
HBase过滤器(filter)提供了非常强大的特性来帮助用户提高其处理表中数据的效率,用户不仅可以使用HBase中预定义好的过滤器,而且可以实现自定义的过滤器。
本次实训我们就来初步的了解过滤器的基本知识和开发流程。
2、全部任务
二、实践详解
1、第 1关:使用过滤器查询指定行的数据
package step1;
import java.io.IOException;
import org.apache.hadoop.cli.util.*;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.util.*;
public class Task {
public void query(String tName) throws Exception {
/********* Begin *********/
Configuration config = new Configuration();
Connection conn = ConnectionFactory.createConnection(config);
TableName tableName = TableName.valueOf(tName);
Table table = conn.getTable(tableName);
Scan scan1 = new Scan();
scan1.addColumn(Bytes.toBytes("basic_info"), Bytes.toBytes("gender"));
Filter filter1 = new RowFilter(CompareOperator.EQUAL,
new BinaryComparator(Bytes.toBytes("2018")));
scan1.setFilter(filter1);
ResultScanner scanner1 = table.getScanner(scan1);
System.out.println("row:2018");
for (Result result : scanner1) {
for(Cell cell : result.listCells()){
System.out.println("basic_info:gender " + new String(CellUtil.cloneValue(cell),"utf-8") );
}
}
scanner1.close();
Scan scan2 = new Scan();
scan2.addColumn(Bytes.toBytes("school_info"), Bytes.toBytes("college"));
Filter filter2 = new RowFilter(CompareOperator.GREATER,
new BinaryComparator(Bytes.toBytes("2018")));
scan2.setFilter(filter2);
ResultScanner scanner2 = table.getScanner(scan2);
for (Result result : scanner2) {
System.out.println("row:" + new String(result.getRow(),"utf-8"));
for(Cell cell : result.listCells()){
System.out.println("school_info:college " + new String(CellUtil.cloneValue(cell),"utf-8") );
}
}
scanner2.close();
Scan scan3 = new Scan();
scan3.addColumn(Bytes.toBytes("basic_info"), Bytes.toBytes("name"));
Filter filter3 = new RowFilter(CompareOperator.LESS_OR_EQUAL,
new BinaryComparator(Bytes.toBytes("2020")));
scan3.setFilter(filter3);
ResultScanner scanner3 = table.getScanner(scan3);
for (Result result : scanner3) {
System.out.println("row:" + new String(result.getRow(),"utf-8"));
for(Cell cell : result.listCells()){
System.out.println("basic_info:name " + new String(CellUtil.cloneValue(cell),"utf-8") );
}
}
scanner3.close();
conn.close();
/********* End *********/
}
}
评测
2、第 2关:使用正则表达式与子字符串匹配行键
package step2;
import java.io.IOException;
import org.apache.hadoop.cli.util.*;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.filter.SubstringComparator;
import org.apache.hadoop.hbase.util.*;
public class Task {
public void query() throws Exception {
/********* Begin *********/
Configuration config = new Configuration();
Connection conn = ConnectionFactory.createConnection(config);
TableName tableName = TableName.valueOf("t2_student_table");
Table table = conn.getTable(tableName);
Scan scan1 = new Scan();
Filter filter1 = new RowFilter(CompareOperator.EQUAL,new RegexStringComparator("1.*9$")); //匹配任意开头,以2结尾。
scan1.setFilter(filter1);
ResultScanner scanner1 = table.getScanner(scan1);
for (Result result : scanner1) {
System.out.println("row:" + new String(result.getRow(),"utf-8"));
for(Cell cell : result.listCells()){
String family = Bytes.toString(CellUtil.cloneFamily(cell));
String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
String value = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println(family + ":" + qualifier + " " + value);
}
}
scanner1.close();
Scan scan2 = new Scan();
Filter filter2 = new RowFilter(CompareOperator.EQUAL,new SubstringComparator("231"));
scan2.setFilter(filter2);
ResultScanner scanner2 = table.getScanner(scan2);
for (Result result : scanner2) {
System.out.println("row:" + new String(result.getRow(),"utf-8"));
for(Cell cell : result.listCells()){
String family = Bytes.toString(CellUtil.cloneFamily(cell));
String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
String value = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println(family + ":" + qualifier + " " + value);
}
}
scanner2.close();
conn.close();
/********* End *********/
}
}
评测
3、第 3关:列族过滤器、值过滤器、列名过滤器
package step3;
import java.io.IOException;
import org.apache.hadoop.cli.util.*;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.util.*;
import org.apache.hadoop.hbase.filter.SubstringComparator;
public class Task {
public void query() throws Exception {
/********* Begin *********/
Configuration config = new Configuration();
Connection conn = ConnectionFactory.createConnection(config);
TableName tableName = TableName.valueOf(Bytes.toBytes("t3_student_table"));
Table table = conn.getTable(tableName);
Filter filter1 = new FamilyFilter(CompareOperator.EQUAL,
new BinaryComparator(Bytes.toBytes("school_info")));
Get get1 = new Get(Bytes.toBytes("1019"));
get1.setFilter(filter1);
Result result1 = table.get(get1);
System.out.println("row:" + new String(result1.getRow(),"utf-8"));
for(Cell cell : result1.listCells()){
String family = Bytes.toString(CellUtil.cloneFamily(cell));
String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
String value = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println(family + ":" + qualifier + " " + value);
}
Filter filter2 = new QualifierFilter(CompareOperator.EQUAL,
new SubstringComparator("c"));
Get get2 = new Get(Bytes.toBytes("2020"));
get2.setFilter(filter2);
Result result2 = table.get(get2);
System.out.println("row:" + new String(result2.getRow(),"utf-8"));
for(Cell cell : result2.listCells()){
String family = Bytes.toString(CellUtil.cloneFamily(cell));
String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
String value = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println(family + ":" + qualifier + " " + value);
}
Scan scan3 = new Scan();
Filter filter3 = new ValueFilter(CompareOperator.EQUAL, new SubstringComparator("寮 "));
scan3.setFilter(filter3);
ResultScanner scanner3 = table.getScanner(scan3);
for (Result result : scanner3) {
System.out.println("row:" + new String(result.getRow(),"utf-8"));
for(Cell cell : result.listCells()){
String family = Bytes.toString(CellUtil.cloneFamily(cell));
String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
String value = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println(family + ":" + qualifier + " " + value);
}
}
scanner3.close();
conn.close();
/********* End *********/
}
}
评测
Ending!
更多课程知识学习记录随后再来吧!
就酱,嘎啦!
注:
人生在勤,不索何获。