import com.yss.utils.BasicPropertites;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
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.apache.hadoop.hive.ql.exec.FilterOperator;
import java.io.IOException;
/**
-
@author
-
@version 2019-03-25 10:25
-
describe:
-
方法列表
-
根据startrow 和endrow 来进行scan
-
值过滤器 valueFilter
-
单列值过滤器 singleColumnValueFilter
-
分页过滤器 PageFilter
扫描二维码关注公众号,回复: 5681314 查看本文章 -
过滤器列表 FilterList
-
行过滤器 rowFilter
-
行键前缀过滤器 prefixFilter
-
列族过滤器 familyFilter
*/
public class HbaseScanFilter {
private static Configuration configuration;
private static Connection connection;
private static Admin admin;public static void main(String[] args) throws IOException {
HbaseScanFilter.scanData("t2", "rw1", "rw2");
}
/**
-
初始化连接
-
hbase初始化需要三个对象
-
Configuration
-
Connection
-
Admin
*/
public static void init(){
//用HBaseConfiguration类来建立一个Configuration类。Configuration类用于加载需要连接HBase的各项配置。
configuration = HBaseConfiguration.create();
configuration.set(“hbase.zookeeper.quorum”,BasicPropertites.zookeeperQuorum());
configuration.set(“hbase.zookeeper.property.clientPort”,BasicPropertites.zookeeperClientPort());
configuration.set(“zookeeper.znode.parent”,BasicPropertites.zookeeperParent());try {
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
- 关闭连接, 关闭的时候需要按照创建的顺序关闭
- Admin
- Connection
*/
public static void close(){
try {
if(null != admin)
admin.close();
if(null != connection)
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
- 格式化输出
- @param result
*/
public static void showCell(Result result){
Cell[] cells = result.rawCells();
for(Cell cell:cells){
System.out.println(“RowName:”+new String(CellUtil.cloneRow(cell))+" “);
System.out.println(“Timetamp:”+cell.getTimestamp()+” “);
System.out.println(“column Family:”+new String(CellUtil.cloneFamily(cell))+” “);
System.out.println(“row Name:”+new String(CellUtil.cloneQualifier(cell))+” “);
System.out.println(“value:”+new String(CellUtil.cloneValue(cell))+” ");
}
}
/**
- scan 通过startrowKey 和 endrowKey 来进行scan查询,也可以只指定 startrowKey
- @param tableName
- @param startRow
- @param stopRow
- @throws IOException
*/
public static void scanData(String tableName,String startRow,String stopRow)throws IOException{
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
scan.setStartRow(Bytes.toBytes(startRow));
scan.setStopRow(Bytes.toBytes(stopRow));
ResultScanner resultScanner = table.getScanner(scan);
for(Result result : resultScanner){
showCell(result);
}
table.close();
close();
}
/**
*值过滤器 valueFilter 任何一列满足条件,大于、等于或者小于某个值,都会被过滤选出来,不能指定具体哪一列
*CompareFilter中包含一个枚举类:CompareOp。CompareOp有以下值:-
LESS:小于。
-
LESS_OR_EQUAL:小于等于。
-
EQUAL:相等。
-
NOT_EQUAL:不相等。
-
GREATER_OR_EQUAL:大于等于。
-
GREATER:大于。
-
NO_OP:无操作。
-
SubstringComparator是一个比较器。这个比较器可以判断目标字符串是否包含所指定的字符串,也即是like ‘%Wang%’的操作,下面还有=的操作。
-
BinaryComparator 我们要完全匹配的“=”
-
@param tableName
-
@throws IOException
*/
public static void valueFilter(String tableName, String str)throws IOException{
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();Filter filter = new ValueFilter(CompareFilter.CompareOp.EQUAL,new SubstringComparator(str));
scan.setFilter(filter);ResultScanner resultScanner = table.getScanner(scan);
for(Result result : resultScanner){
showCell(result);
}
table.close();
close();
}
/**
*单列值过滤器 singleColumnValueFilter-
单列值过滤器在发现该行记录并没有你想要比较的列的时候,会把整行数据放入结果集
-
所以如果要安全地使用单列值过滤器,请务必保证你的每行记录都包含有将要比较的列。
-
可以针对在每行中都存在的列使用单列值过滤器,对于不确定是否存在的列使用过滤器列表。
-
@param tableName
-
@throws IOException
*/
public static void singleColumnValueFilter(String tableName, String columFamily, String columName, String columValue)throws IOException{
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
//mycef:name = value
Filter filter = new SingleColumnValueFilter(Bytes.toBytes(columFamily), Bytes.toBytes(columName), CompareFilter.CompareOp.EQUAL,new BinaryComparator(Bytes.toBytes(columValue)));
scan.setFilter(filter);ResultScanner resultScanner = table.getScanner(scan);
for(Result result : resultScanner){
showCell(result);
}
table.close();
close();
}
/**
*实际上PageFilter只能实现相当于limit n的功能(limit限制的只是一个region server,想要整体限制2条,还需要对所有结果进行排序,留下两条),下面的方法是整体限制2条-
@param tableName
-
@throws IOException
*/
public static void pageFilter(String tableName)throws IOException{
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
//代表每页几条数据
Filter filter = new PageFilter(2L);
scan.setFilter(filter);
//第一页的结果
ResultScanner resultScanner = table.getScanner(scan);
byte[] lastRowKey = printResult(resultScanner);
resultScanner.close();byte[] startRowKey = Bytes.add(lastRowKey, new byte[1]);
scan.setStartRow(startRowKey);
ResultScanner resultScanner2 = table.getScanner(scan);
//打印第二页结果
printResult(resultScanner2);
resultScanner2.close();table.close();
close();
}
/**
- 打印ResultScanner结果,并返回结果的最后一条rowKey
- @param rs
- @return
- @throws IOException
*/
public static byte[] printResult(ResultScanner rs)throws IOException{
byte[] lastRowKey = null;
for (Result r : rs) {
lastRowKey = r.getRow();
showCell®;
}
return lastRowKey;
}
/**
*顾虑器列表中的执行顺序是按照过滤器-
@param tableName
-
@throws IOException
*/
public static void filterList(String tableName, String columFamily, String columName, String columValue)throws IOException{
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
//过滤器列表
//MUST_PASS_ALL代表所有的过滤器都要满足要求,MUST_PASS_ONE 就是有一个过滤器满足就行,主要用来当有多个顾虑器时,只要其中1个过滤器满足过滤条件即可时使用
FilterList list = new FilterList(FilterList.Operator.MUST_PASS_ONE);
Filter filter = new SingleColumnValueFilter(Bytes.toBytes(columFamily), Bytes.toBytes(columName), CompareFilter.CompareOp.EQUAL,new BinaryComparator(Bytes.toBytes(columValue)));
list.addFilter(filter);
scan.setFilter(list);ResultScanner resultScanner = table.getScanner(scan);
for(Result result : resultScanner){
showCell(result);
}
table.close();
close();
}
/**
*行过滤器(RowFilter)的作用是针对rowkey进行过滤。-
@param tableName
-
@throws IOException
*/
public static void rowFilter(String tableName, String rowKey)throws IOException{
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();Filter filter = new RowFilter(CompareFilter.CompareOp.EQUAL,new BinaryComparator(Bytes.toBytes(rowKey)));
//通过正则匹配来过滤rowKey
//Filter filter2 = new RowFilter(CompareFilter.CompareOp.EQUAL,new RegexStringComparator(rowKey));
scan.setFilter(filter);ResultScanner resultScanner = table.getScanner(scan);
for(Result result : resultScanner){
showCell(result);
}
table.close();
close();
}
/**
*检索出所有行键以prefixRow起头的行-
@param tableName
-
@throws IOException
*/
public static void prefixFilter(String tableName, String prefixRow)throws IOException{
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();Filter filter = new PrefixFilter(Bytes.toBytes(prefixRow));
scan.setFilter(filter);ResultScanner resultScanner = table.getScanner(scan);
for(Result result : resultScanner){
showCell(result);
}
table.close();
close();
}
/**
*列族过滤器 familyFilter ,过滤获取列族为columFamily的单元格数据-
@param tableName
-
@throws IOException
*/
public static void familyFilter(String tableName, String columFamily)throws IOException{
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();//Filter filter = new QualifierFilter(CompareFilter.CompareOp.EQUAL,new RegexStringComparator(columFamily));
Filter filter = new FamilyFilter(CompareFilter.CompareOp.EQUAL,new BinaryComparator(Bytes.toBytes(columFamily)));
scan.setFilter(filter);ResultScanner resultScanner = table.getScanner(scan);
for(Result result : resultScanner){
showCell(result);
}
table.close();
close();
}
/**
*qualifierFilter 列名过滤器-
@param tableName
-
@throws IOException
*/
public static void qualifierFilter(String tableName, String columName)throws IOException{
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();//Filter filter = new QualifierFilter(CompareFilter.CompareOp.EQUAL,new RegexStringComparator(columName));
Filter filter = new QualifierFilter(CompareFilter.CompareOp.EQUAL,new BinaryComparator(Bytes.toBytes(columName)));
scan.setFilter(filter);ResultScanner resultScanner = table.getScanner(scan);
for(Result result : resultScanner){
showCell(result);
}
table.close();
close();
}
-
}