public static void ValueFilterExam() throws Exception{
Configuration config = HBaseConfiguration.create();
config.set("hbase.zookeeper.quorum","node2");
config.set("hbase.zookeeper.property.clientPort","2181");
try(Connection connection = ConnectionFactory.createConnection(config)) {
Table table = connection.getTable(TableName.valueOf("test"));
Scan scan =new Scan(Bytes.toBytes("rk-001"));
Filter valueFilter = new ValueFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("wanghua"));
Filter single = new SingleColumnValueFilter(Bytes.toBytes("cf1"),Bytes.toBytes("001"),CompareFilter.CompareOp.EQUAL, new SubstringComparator("wanghua"));
scan.setFilter(valueFilter);
ResultScanner scannerResult = table.getScanner(scan);
for (Result rs:scannerResult) {
System.out.println(Bytes.toString(rs.getValue(Bytes.toBytes("cf1"),Bytes.toBytes("001"))));
}
}
}
public static void FilterListExam() throws Exception{
Configuration config = HBaseConfiguration.create();
config.set("hbase.zookeeper.quorum","node2");
config.set("hbase.zookeeper.property.clientPort","2181");
try(Connection connection = ConnectionFactory.createConnection(config)) {
Table table = connection.getTable(TableName.valueOf("test"));
Scan scan =new Scan(Bytes.toBytes("rk-001"));
FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
Filter familyFilter = new FamilyFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("wanghua")));
filterList.addFilter(familyFilter);
Filter qualiFilter = new QualifierFilter(CompareFilter.CompareOp.EQUAL,new BinaryComparator(Bytes.toBytes("00")));
filterList.addFilter(qualiFilter);
Filter valueFilter = new ValueFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("wanghua"));
filterList.addFilter(valueFilter);
scan.setFilter(filterList);
ResultScanner scannerResult = table.getScanner(scan);
for (Result rs:scannerResult) {
System.out.println(Bytes.toString(rs.getValue(Bytes.toBytes("cf1"),Bytes.toBytes("001"))));
}
}
}
public static void PageFilterExam() throws Exception{
Configuration config = HBaseConfiguration.create();
config.set("hbase.zookeeper.quorum","node2");
config.set("hbase.zookeeper.property.clientPort","2181");
try(Connection connection = ConnectionFactory.createConnection(config)) {
Table table = connection.getTable(TableName.valueOf("test"));
Scan scan =new Scan(Bytes.toBytes("rk-001"));
Filter pageFilter = new PageFilter(2L);
Filter rowFilter = new RowFilter(CompareFilter.CompareOp.GREATER,new BinaryComparator(Bytes.toBytes("rk-001")));
scan.setFilter(pageFilter);
ResultScanner scannerResult = table.getScanner(scan);
for (Result rs:scannerResult) {
System.out.println(Bytes.toString(rs.getValue(Bytes.toBytes("cf1"),Bytes.toBytes("001"))));
}
}
}
public static void RowFilterExam() throws Exception{
Configuration config = HBaseConfiguration.create();
config.set("hbase.zookeeper.quorum","node2");
config.set("hbase.zookeeper.property.clientPort","2181");
try(Connection connection = ConnectionFactory.createConnection(config)) {
List rowlist =new ArrayList<>();
PrefixFilter prefixFilter = new PrefixFilter(Bytes.toBytes("rk-0"));
FuzzyRowFilter fuzzyRowFilter = new FuzzyRowFilter(Arrays.asList(new Pair<byte[], byte[]>(
Bytes.toBytesBinary("2016_??_??_4567"),
new byte[]{0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0}
)));
Table table = connection.getTable(TableName.valueOf("test"));
MultiRowRangeFilter.RowRange rowRange1 = new MultiRowRangeFilter.RowRange("rk-001", true, "rk-003", true);
MultiRowRangeFilter.RowRange rowRange2 = new MultiRowRangeFilter.RowRange("rk-005", true, "rk-007", true);
rowlist.add(rowRange1);
MultiRowRangeFilter multiRowRangeFilter = new MultiRowRangeFilter(rowlist);
Scan scan =new Scan();
scan.setFilter(multiRowRangeFilter);
ResultScanner scannerResult = table.getScanner(scan);
for (Result rs:scannerResult) {
System.out.println(Bytes.toString(rs.getValue(Bytes.toBytes("cf1"),Bytes.toBytes("001"))));
}
}
}
public static void QualiFilterExam() throws Exception{
Configuration config = HBaseConfiguration.create();
config.set("hbase.zookeeper.quorum","node2");
config.set("hbase.zookeeper.property.clientPort","2181");
try(Connection connection = ConnectionFactory.createConnection(config)) {
Table table = connection.getTable(TableName.valueOf("test"));
Scan scan =new Scan(Bytes.toBytes("rk-001"));
Filter columnPrefixFilter = new ColumnPrefixFilter(Bytes.toBytes("cf"));
QualifierFilter cf1 = new QualifierFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("cf1")));
Filter depent = new DependentColumnFilter(Bytes.toBytes("cf1"),Bytes.toBytes("updatetime"));
Filter firstkey =new FirstKeyOnlyFilter();
Filter rangeFilter = new ColumnRangeFilter(Bytes.toBytes("active"),true,Bytes.toBytes("city"),true);
scan.setFilter(firstkey);
ResultScanner scannerResult = table.getScanner(scan);
int count = 0;
for (Result rs:scannerResult) {
count++;
}
}
}
public static void CellFilterExam() throws Exception{
Configuration config = HBaseConfiguration.create();
config.set("hbase.zookeeper.quorum","node2");
config.set("hbase.zookeeper.property.clientPort","2181");
try(Connection connection = ConnectionFactory.createConnection(config)) {
Table table = connection.getTable(TableName.valueOf("test"));
Scan scan =new Scan(Bytes.toBytes("rk-001"));
List time= new ArrayList<>();
time.add(5L);
time.add(7L);
Filter timrFilter = new TimestampsFilter(time);
Filter valueFilter = new ValueFilter(CompareFilter.CompareOp.NOT_EQUAL,new BinaryComparator(Bytes.toBytes("wang")));
Filter skipFilter = new SkipFilter(valueFilter);
WhileMatchFilter whileMatchFilter = new WhileMatchFilter(valueFilter);
scan.setFilter(skipFilter);
ResultScanner scannerResult = table.getScanner(scan);
for (Result rs:scannerResult) {
System.out.println(Bytes.toString(rs.getValue(Bytes.toBytes("cf1"),Bytes.toBytes("001"))));
}
}
}