educoder平台HBase高级特性:过滤器(二)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
第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("test_tb1"));
Table table = conn.getTable(tableName);
Filter filter = new PrefixFilter(Bytes.toBytes("row5"));
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("\t" + family + ":" + qualifier + " " + value);
}
}
//分页
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,"utf-8");
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("\t" + family + ":" + qualifier + " " + value);
}
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.*;