第1关:常用的专用过滤器 package step1;
import java.io.IOException; import javax.ws.rs.POST;
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.conf.Configuration; import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; public class Task {
public void query(String tName) throws Exception { /********* Begin *********/
Configuration config = new Configuration();
Connection conn = ConnectionFactory.createConnection(config);
TableName tableName = TableName.valueOf(Bytes.toBytes(\ Table table = conn.getTable(tableName);
Filter filter = new PrefixFilter(Bytes.toBytes(\ Scan scan = new Scan(); scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan); for (Result result : scanner) {
System.out.println(Bytes.toString(result.getRow())); 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(\ } }
//分页
byte[] POSTFIX = new byte[] {0};
Filter filter1 = new PageFilter(10);//构建过滤器并设置每页数据量 int totalRows = 0; byte[] lastRow = null; int i = 4; while(i > 0 ){
Scan scan1 = new Scan(); //添加过滤器
scan1.setFilter(filter1); //设置查询的起始行 if(lastRow != null){
byte[] startRow = Bytes.add(lastRow, POSTFIX); String info = new String(startRow,\ System.out.println(\开始分页查询\ scan1.withStartRow(startRow); }
ResultScanner scanner1= table.getScanner(scan1); int localRows = 0; Result result;
while ((result = scanner1.next()) != null) {
System.out.println(Bytes.toString(result.getRow())); 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(\ }
localRows++; totalRows++;
lastRow = result.getRow(); }
scanner1.close();
if (localRows == 0) break; i--; }
conn.close();
/********* End *********/ } }
第2关:同时使用多种过滤器 package step2;
import java.io.IOException; import java.util.ArrayList; import java.util.List;
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(String tName) throws Exception { /********* Begin *********/
Configuration config = new Configuration();
Connection conn = ConnectionFactory.createConnection(config); TableName tableName = TableName.valueOf(\ Table table = conn.getTable(tableName);
Filter regFilter = new RowFilter(CompareOperator.EQUAL ,new RegexStringComparator(\
Filter moreThanFilter = new RowFilter(CompareOperator.GREATER , new BinaryComparator(Bytes.toBytes(\ List
list.add(moreThanFilter);
FilterList filterList1 = new FilterList(list); Scan scan1 = new Scan(); scan1.setFilter(filterList1);
ResultScanner scanner1 = table.getScanner(scan1); for (Result result : scanner1) {
System.out.println(Bytes.toString(result.getRow())); 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(\ } }
scanner1.close(); //第二次查询 Filter subFilter = new RowFilter(CompareOperator.EQUAL,new SubstringComparator(\
Filter valueFilter = new ValueFilter(CompareOperator.EQUAL, new BinaryComparator(Bytes.toBytes(\ List
FilterList filterList2 = new FilterList(FilterList.Operator.MUST_PASS_ONE,list2); Scan scan2 = new Scan(); scan2.setFilter(filterList2);
ResultScanner scanner2 = table.getScanner(scan2); for (Result result : scanner2) {
System.out.println(Bytes.toString(result.getRow())); 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(\ } }
scanner2.close(); conn.close();
/********* End *********/ } }
第3关:过滤器总结 第1题 AE 第2题 D
第3题 E
子串比较器的CompareOperator只能有EQUAL和NOT_EQUAL两种取值 第4题 BC