/*
* 扫描全表,查寻指定列族的记录
*/
@Test
public void testScanWithFamily() throws Exception {
Scan scan = new Scan();
scan.addFamily("base_info".getBytes());
ResultScanner scanner = table.getScanner(scan);
HBasePrintUtil.printResultScanner(scanner);
}
/*
* 扫描全表,查寻指定列族、指定列的记录
*/
@Test
public void testScanWithColumn() throws Exception {
Scan scan = new Scan();
scan.addColumn("base_info".getBytes(), "name".getBytes());
ResultScanner scanner = table.getScanner(scan);
HBasePrintUtil.printResultScanner(scanner);
}
/*
* 扫描全表,查寻指定时间戳或指定时间戳范围的记录
*/
@Test
public void testScanWithTimestamp() throws Exception {
Scan scan = new Scan();
// 指定时间戳,查出一条
// scan.setTimeStamp(1514443301587L);
// 指定时间戳范围,查出一条或多条
scan.setTimeRange(1514443301340L, 1514443301587L);
ResultScanner scanner = table.getScanner(scan);
HBasePrintUtil.printResultScanner(scanner);
}
/*
* 扫描全表,查寻指定rowkey或rowkey范围的记录
*/
@Test
public void testScanWithRowkey() throws Exception {
Scan scan = new Scan();
scan.setStartRow("baiyc_20150716_0003".getBytes());
scan.setStopRow("baiyc_20150716_0005".getBytes());
/*
* 如果只设置了 startRow,就查询从startRow到表末尾的记录(不包括表最后的rowkey所在的那一行记录)
* 如果只设置了 stopRow,就查询从表开头到stopRow的记录(不包括stopRow的那一行记录)
*/
ResultScanner scanner = table.getScanner(scan);
HBasePrintUtil.printResultScanner(scanner);
}
/*
* 测试RowFilter
* 扫描全表,查寻rowkey小于等于"baiyc_20150716_0003"的记录
*/
@Test
public void testRowFilter() throws Exception {
Scan scan = new Scan();
Filter filter = new RowFilter(CompareOp.LESS_OR_EQUAL, new BinaryComparator(Bytes.toBytes("baiyc_20150716_0003")));
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
HBasePrintUtil.printResultScanner(scanner);
}
/*
* 测试FamilyFilter
* 扫描全表,查寻列簇大于"base_info"的记录
*/
@Test
public void testFamilyFilter() throws Exception {
Scan scan = new Scan();
Filter filter = new FamilyFilter(CompareOp.GREATER, new BinaryComparator(Bytes.toBytes("base_info")));
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
HBasePrintUtil.printResultScanner(scanner);
}
/*
* 测试QualifierFilter
* 扫描全表,查寻列名等于"name"的记录
*/
@Test
public void testQualifierFilter() throws Exception {
Scan scan = new Scan();
Filter filter = new QualifierFilter(CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("name")));
/*
* BinaryComparator比较器:匹配完全等值的列名
* Filter filter = new QualifierFilter(CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("name")));
*
* BinaryPrefixComparator比较器:匹配列名的前缀为"na"的记录
* Filter filter = new QualifierFilter(CompareOp.EQUAL, new BinaryPrefixComparator(Bytes.toBytes("na")));
*
* RegexStringComparator比较器:匹配列名满足正则表达式"na."的记录
* Filter filter = new QualifierFilter(CompareOp.EQUAL, new RegexStringComparator("na."));
*/
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
HBasePrintUtil.printResultScanner(scanner);
}
/*
* 测试ValueFilter
* 扫描全表,查寻列的值中包含"mus"子串的记录
*/
@Test
public void testValueFilter() throws Exception {
Scan scan = new Scan();
Filter filter = new ValueFilter(CompareOp.EQUAL, new SubstringComparator("mus"));
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
HBasePrintUtil.printResultScanner(scanner);
}
/*
* 测试FilterList
* 同时添加多个过滤器
*/
@Test
public void testFilterList() throws Exception {
Scan scan = new Scan();
Filter filter1 = new FamilyFilter(CompareOp.GREATER, new BinaryComparator(Bytes.toBytes("base_info")));
Filter filter2 = new ValueFilter(CompareOp.NOT_EQUAL, new BinaryComparator(Bytes.toBytes("music")));
FilterList list = new FilterList(filter1, filter2);
scan.setFilter(list);
ResultScanner scanner = table.getScanner(scan);
HBasePrintUtil.printResultScanner(scanner);
}
/*
* 测试PageFilter
* 分页过滤器,从指定rowkey开始,显示指定的条数
*/
@Test
public void testPageFilter() throws Exception {
Scan scan = new Scan();
// 设置每页显示4页
Filter filter = new PageFilter(4);
// 设置起始的rowkey
scan.setStartRow("baiyc_20150716_0003".getBytes());
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
HBasePrintUtil.printResultScanner(scanner);
}
/*
* 测试SingleColumnValueFilter:单列值过滤器,会返回满足条件的整行
* 扫描全表,查询列族为"base_info",列名为"name",且列值包括"zhangsan"子串的所有行
*/
@Test
public void testSingleColumnValueFilter() throws Exception {
Scan scan = new Scan();
SingleColumnValueFilter filter = new SingleColumnValueFilter(
Bytes.toBytes("base_info"),
Bytes.toBytes("name"),
CompareOp.EQUAL,
new SubstringComparator("zhangsan"));
/*
* 如果不设置为 true,则那些不包含指定column的行也会返回
* 比如,现在有一行它没有"name"这个列,它的所有的列值中也不包括"shangsan"这个子串,那么这一行也会返回
* 设置为true,只会返回那些有"name"这个列,并且满足过滤条件的行
*/
filter.setFilterIfMissing(true);
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
HBasePrintUtil.printResultScanner(scanner);
}
/*
* 测试SingleColumnValueExcludeFilter:单列值排除器,返回排除了该列的结果
* 与上面的过滤器查询出来的行相同,但不打印"name"那一列
*/
@Test
public void testSingleColumnValueExcludeFilter() throws Exception {
Scan scan = new Scan();
SingleColumnValueExcludeFilter filter = new SingleColumnValueExcludeFilter(
Bytes.toBytes("base_info"),
Bytes.toBytes("name"),
CompareOp.EQUAL,
new SubstringComparator("zhangsan"));
filter.setFilterIfMissing(true);
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
HBasePrintUtil.printResultScanner(scanner);
}
/*
* 测试PrefixFilter:前缀过滤器,针对行键
* 扫描全表:查询rowkey的前缀为"baiyc"的全部行
*/
@Test
public void testPrefixFilter() throws Exception {
Scan scan = new Scan();
Filter filter = new PrefixFilter(Bytes.toBytes("baiyc"));
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
HBasePrintUtil.printResultScanner(scanner);
}
/*
* 测试ColumnPrefixFilter:列前缀过滤器
* 扫描全表:查询列名的前缀为"na"的全部记录
*/
@Test
public void testColumnPrefixFilter() throws Exception {
Scan scan = new Scan();
Filter filter = new ColumnPrefixFilter(Bytes.toBytes("na"));
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
HBasePrintUtil.printResultScanner(scanner);
}
/*
* 测试MultipleColumnPrefixFilter:基于列名设置多个前缀过滤数据
* 扫描全表:查询列名的前缀为"na"和列名的前缀为"ag"的全部记录
*/
@Test
public void testMultipleColumnPrefixFilter() throws Exception {
Scan scan = new Scan();
byte[][] prefixes = new byte[][] {Bytes.toBytes("na"), Bytes.toBytes("ag")};
Filter filter = new MultipleColumnPrefixFilter(prefixes);
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
HBasePrintUtil.printResultScanner(scanner);
}
}