import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CompareOperator;
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.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class FilterScanTest {
Configuration conf = null;
ExecutorService pool = null;
Connection conn = null;
Admin admin = null;
Table table = null;
@Before
public void before() throws IOException {
Configuration conf = null;
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "192.168.93.101:2181");
ExecutorService pool = Executors.newFixedThreadPool(10);
System.out.println("");
conn = ConnectionFactory.createConnection(conf, pool);
admin = conn.getAdmin();
}
//scan 查询表操作,限定查询的行数
@Test
public void scan_basic3() throws IOException {
table = conn.getTable(TableName.valueOf("student"));
Scan scan = new Scan();
scan.setLimit(2);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
showResult(result);
}
}
//有关行健的过滤器
//scan 查询表操作,设置行健前缀过滤器
@Test
public void scan_basic_filter() throws IOException {
table = conn.getTable
(TableName.valueOf
("student"));
Scan scan = new Scan();
Filter f = new PrefixFilter(Bytes.toBytes("1001"));
scan.setFilter(f);
ResultScanner scanner =
table.getScanner(scan);
for (Result result : scanner) {
byte[] b = result.getRow();
System.out.println(new String(b));
showResult(result);
}
}
//scan 查询表操作,设置只扫描行键过滤器
@Test
public void scan_basic_filter1() throws IOException {
table = conn.getTable
(TableName.valueOf
("student"));
Scan scan = new Scan();
//只会保留所有的行键值,
Filter f = new KeyOnlyFilter();
scan.setFilter(f);
ResultScanner scanner =
table.getScanner(scan);
Iterator<Result> iter = scanner.iterator();
while (iter.hasNext()) {
Result rs = iter.next();
byte[] rowkey = rs.getRow();
System.out.println(new String(rowkey));
}
}
//scan 查询表操作,设置随机行过滤器,按比例获取随机行数
@Test
public void scan_basic_filter2() throws IOException {
table = conn.getTable
(TableName.valueOf
("student"));
Scan scan = new Scan();
Filter f = new RandomRowFilter(0.5f);
scan.setFilter(f);
ResultScanner scanner =table.getScanner(scan);
for (Result result : scanner) {
showResult(result);
}
}
//scan 查询表操作,设置行范围过滤器
@Test
public void scan_basic_filter3() throws IOException {
table = conn.getTable
(TableName.valueOf
("student"));
Scan scan = new Scan();
List<MultiRowRangeFilter.RowRange> list =
new ArrayList<>();
list.add(new MultiRowRangeFilter
.RowRange("1001", true, "1001", true));
Filter f = new MultiRowRangeFilter(list);
scan.setFilter(f);
ResultScanner scanner =
table.getScanner(scan);
for (Result result : scanner) {
System.out.println(new String(result.getRow()));
showResult(result);
}
}
// (1)比较运算符 CompareFilter.CompareOp
// 比较运算符用于定义比较关系,可以有以下几类值供选择:
// EQUAL 相等
// GREATER 大于
// GREATER_OR_EQUAL 大于等于
// LESS 小于
// LESS_OR_EQUAL 小于等于
// NOT_EQUAL 不等于
//
// (2)比较器 ByteArrayComparable
// 通过比较器可以实现多样化目标匹配效果,比较器有以下子类可以使用:
// BinaryComparator 匹配完整字节数组
// BinaryPrefixComparator 匹配字节数组前缀
// BitComparator
// NullComparator
// RegexStringComparator 正则表达式匹配
// SubstringComparator 子串匹配
//scan 查询表操作,设置行健过滤器,可以通过提供不同比较器实现不同功能,如匹配正则表达式
@Test
public void scan_basic_filter4() throws IOException {
table = conn.getTable
(TableName.valueOf
("student"));
Scan scan = new Scan();
// rowkey = .*5.*
Filter f = new RowFilter(
CompareOperator.EQUAL,
new RegexStringComparator(".*2.*"));
scan.setFilter(f);
ResultScanner scanner =table.getScanner(scan);
for (Result result : scanner) {
System.out.println(new String(result.getRow()));
showResult(result);
}
}
@Test
public void scan_basic_filter5() throws IOException {
//binary:1005
table = conn.getTable
(TableName.valueOf
("student"));
Scan scan = new Scan();
Filter f = new RowFilter(CompareOperator.EQUAL,
new BinaryComparator("1001".getBytes()));
scan.setFilter(f);
ResultScanner scanner =table.getScanner(scan);
for (Result result : scanner) {
showResult(result);
}
}
//scan 列族相关过滤器
@Test
public void scan_basic_filter6() throws IOException {
table = conn.getTable
(TableName.valueOf
("student"));
Scan scan = new Scan();
//family like %in%
Filter f = new FamilyFilter(
CompareOperator.EQUAL,
new RegexStringComparator(".*in.*"));
scan.setFilter(f);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
showResult(result);
}
}
// 列相关过滤器----------------------
// 获得前n列过滤器
@Test
public void scan_basic_filter7() throws IOException {
table = conn.getTable
(TableName.valueOf
("student"));
Scan scan = new Scan();
Filter f = new ColumnCountGetFilter(2);
scan.setFilter(f);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
System.out.println(new String(result.getRow()));
showResult(result);
}
}
//列分页过滤器 基于ColumnPaginationFilter,参数1 获得n列 参数2 从第n列开始获取
@Test
public void scan_basic_filter8() throws IOException {
table = conn.getTable(TableName.valueOf("student"));
Scan scan = new Scan();
Filter f = new ColumnPaginationFilter(3, 1);
scan.setFilter(f);
ResultScanner scanner =table.getScanner(scan);
for (Result result : scanner) {
System.out.println(new String(result.getRow()));
showResult(result);
}
}
// 列名前缀过滤器
// ColumnPrefixFilter
// 列名范围过滤器
// ColumnRangeFilter
// 匹配列族名列名过滤器
// ColumnValueFilter
// 有关值的过滤器
// 根据对于值的比较过滤行
@Test
public void scan_basic_filter9() throws IOException {
table = conn.getTable(TableName.valueOf("student"));
Scan scan = new Scan();
Filter f = new ValueFilter(CompareOperator.EQUAL,
new RegexStringComparator(".*2.*"));
scan.setFilter(f);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
showResult(result);
}
}
public static void showResult(Result result) {
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.println(cell);
}
}
@After
public void after() throws IOException {
admin.close();
conn.close();
}
}