目錄
概述
過濾器可以分為兩種:比較過濾器和專用過濾器。過濾器的作用是在服務端判斷數據是否滿足條件,然后只將滿足條件的數據返回給客戶端。
-
比較過濾器
LESS —— 小於
LESS_OR_EQUAL —— 小於等於
EQUAL —— 等於
NOT_EQUAL —— 不等於
GREATER_OR_EQUAL —— 大於等於
GREATER —— 大於
NO_OP —— 排除所有 -
專用過濾器
BinaryComparator —— 按字節索引順序比較指定字節數組,采用Bytes.compareTo(byte[])
BinaryPrefixComparator —— 跟前面相同,只是比較左端的數據是否相同
NullComparator —— 判斷給定的是否為空
BitComparator —— 按位比較
RegexStringComparator —— 提供一個正則的比較器,僅支持 EQUAL 和非EQUAL
SubstringComparator —— 判斷提供的子串是否出現在value中
代碼實現
以下代碼均有一個BeforeTest和一個AfterTest
/** * 創建連接HBase服務器的方法 */
private Connection connection;
private Table table;
@BeforeTest
public void init() throws IOException {
//獲取連接
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "node01:2181,node02:2181,node03:2181");
connection = ConnectionFactory.createConnection(configuration);
//獲取表
table = connection.getTable(TableName.valueOf("myuser"));
}
/** * 關閉系統連接和表連接 */
@AfterTest
public void close() throws IOException {
//關閉表
table.close();
connection.close();
}
rowKey過濾器RowFilter
/** * 過濾rowKey比0003小的數據 */
@Test
public void rowFilter() throws IOException {
Scan scan = new Scan();
//因為RowFilter需要一個binaryComparator參數,所以需要創建一個對象
BinaryComparator binaryComparator = new BinaryComparator("0003".getBytes());
//通過rowFilter按照rowKet進行過濾
RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.LESS, binaryComparator);
scan.setFilter(rowFilter);
ResultScanner resultScanner = table.getScanner(scan);
for (Result result : resultScanner) {
byte[] row = result.getRow();
System.out.println("數據的rowKey為" + Bytes.toString(row));
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
byte[] family = cell.getFamily();
byte[] qualifier = cell.getQualifier();
byte[] value = cell.getValue();
//id列和age列是整型數據
if ("f1".equals(Bytes.toString(family)) && "id".equals(Bytes.toString(qualifier)) || "age".equals(Bytes.toString(value))){
System.out.println("列族為"+Bytes.toString(family)+"列名為"+Bytes.toString(qualifier)+"列值為"+Bytes.toInt(value));
} else {
System.out.println("列族為"+Bytes.toString(family)+"列名為"+Bytes.toString(qualifier)+"列值為"+Bytes.toString(value));
}
}
}
}
列族過濾器FamilyFilter
/** * 列族過濾器,只獲取f2列族的數據 */
@Test
public void familyFilter() throws IOException {
//獲取Scan對象
Scan scan = new Scan();
//FamilyFilter需要一個binaryComparator的參數,所以新建一個對象
BinaryComparator binaryComparator = new BinaryComparator("f2".getBytes());
//scan.setFilter需要的參數是FamilyFilter
FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.EQUAL, binaryComparator);
//數據裝在scan中
scan.setFilter(familyFilter);
//拿到需要的所有數據
ResultScanner resultScanner = table.getScanner(scan);
for (Result result : resultScanner) {
byte[] row = result.getRow();
System.out.println("數據的rowKey為" + Bytes.toString(row));
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
byte[] family = cell.getFamily();
byte[] qualifier = cell.getQualifier();
byte[] value = cell.getValue();
//id列和age列是整型數據
if ("f1".equals(Bytes.toString(family)) && "id".equals(Bytes.toString(qualifier)) || "age".equals(Bytes.toString(value))){
System.out.println("列族為"+Bytes.toString(family)+"列名為"+Bytes.toString(qualifier)+"列值為"+Bytes.toInt(value));
} else {
System.out.println("列族為"+Bytes.toString(family)+"列名為"+Bytes.toString(qualifier)+"列值為"+Bytes.toString(value));
}
}
}
}
列過濾器QualifierFilter
/** * 列過濾器,只查詢name這一列 */
@Test
public void qualifierFilter() throws IOException {
//獲取Scan對象
Scan scan = new Scan();
//列過濾器就是QualifierFilter,new一個
QualifierFilter qualifierFilter = new QualifierFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("name"));
//將列過濾器的值設置到scan中
scan.setFilter(qualifierFilter);
//獲取到所有的scan的數據
ResultScanner resultScanner = table.getScanner(scan);
for (Result result : resultScanner) {
byte[] row = result.getRow();
System.out.println("數據的rowKey為" + Bytes.toString(row));
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
byte[] family = cell.getFamily();
byte[] qualifier = cell.getQualifier();
byte[] value = cell.getValue();
//id列和age列是整型數據
if ("f1".equals(Bytes.toString(family)) && "id".equals(Bytes.toString(qualifier)) || "age".equals(Bytes.toString(value))){
System.out.println("列族為"+Bytes.toString(family)+"列名為"+Bytes.toString(qualifier)+"列值為"+Bytes.toInt(value));
} else {
System.out.println("列族為"+Bytes.toString(family)+"列名為"+Bytes.toString(qualifier)+"列值為"+Bytes.toString(value));
}
}
}
}
列值過濾器ValueFilter
/** * 列值過濾器,查詢列中含有8的值 */
@Test
public void valueFilter() throws IOException {
//獲取Scan對象
Scan scan = new Scan();
//列值過濾器ValueFilter,new一個對象
ValueFilter valueFilter = new ValueFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("8"));
//將ValueFilter過濾后的值放到Scan中
scan.setFilter(valueFilter);
//獲取所有數據
ResultScanner resultScanner = table.getScanner(scan);
//遍歷循環得到每一條數據
for (Result result : resultScanner) {
//獲取每一條數據的rowKey
byte[] row = result.getRow();
System.out.println("數據的rowKey為" + Bytes.toString(row));
//獲取到數據中的每一個cell
List<Cell> cells = result.listCells();
//遍歷循環得到每一個cell的值
for (Cell cell : cells) {
byte[] qualifier = cell.getQualifier();
byte[] family = cell.getFamily();
byte[] value = cell.getValue();
//id列和age列是整型數據
if ("f1".equals(Bytes.toString(family)) && "id".equals(Bytes.toString(qualifier)) || "age".equals(Bytes.toString(value))){
System.out.println("列族為"+Bytes.toString(family)+"列名為"+Bytes.toString(qualifier)+"列值為"+Bytes.toInt(value));
} else {
System.out.println("列族為"+Bytes.toString(family)+"列名為"+Bytes.toString(qualifier)+"列值為"+Bytes.toString(value));
}
}
}
}
專用過濾器
單列值過濾器 SingleColumnValueFilter
/** * 單列值過濾器,查詢name為劉備的人 */
@Test
public void singleColumnValueFilter() throws IOException {
//new一個Scan對象
Scan scan = new Scan();
//單列值過濾器,new一個對象,來限定條件
SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter("f1".getBytes(), "name".getBytes(), CompareFilter.CompareOp.EQUAL, "劉備".getBytes());
//獲取限定條件后拿到的數據
scan.setFilter(singleColumnValueFilter);
//所有數據封裝到resultScanner中
ResultScanner resultScanner = table.getScanner(scan);
//循環遍歷resultScanner中的每條數據
for (Result result : resultScanner) {
//獲取每條數據的rowKey值
byte[] row = result.getRow();
System.out.println("數據的rowKey為" + Bytes.toString(row));
//獲取每條數據中的cell值
List<Cell> cells = result.listCells();
//遍歷循環得到每一個cell
for (Cell cell : cells) {
byte[] qualifier = cell.getQualifier();
byte[] family = cell.getFamily();
byte[] value = cell.getValue();
//id列和age列是整型數據
if ("f1".equals(Bytes.toString(family)) && "id".equals(Bytes.toString(qualifier)) || "age".equals(Bytes.toString(value))){
System.out.println("列族為"+Bytes.toString(family)+"列名為"+Bytes.toString(qualifier)+"列值為"+Bytes.toInt(value));
} else {
System.out.println("列族為"+Bytes.toString(family)+"列名為"+Bytes.toString(qualifier)+"列值為"+Bytes.toString(value));
}
}
}
}
列值排除過濾器SingleColumnValueExcludeFilter
/** * 列值排除過濾器 */
@Test
public void singleColumnValueExcludeFilter() throws IOException {
Scan scan = new Scan();
scan.setFilter(new SingleColumnValueExcludeFilter("f1".getBytes(),"name".getBytes(), CompareFilter.CompareOp.EQUAL,"劉備".getBytes()));
ResultScanner resultScanner = table.getScanner(scan);
for (Result result : resultScanner) {
byte[] row = result.getRow();
System.out.println("數據的rowKey為" + Bytes.toString(row));
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
byte[] qualifier = cell.getQualifier();
byte[] family = cell.getFamily();
byte[] value = cell.getValue();
//id列和age列是整型數據
if ("f1".equals(Bytes.toString(family)) && "id".equals(Bytes.toString(qualifier)) || "age".equals(Bytes.toString(value))){
System.out.println("列族為"+Bytes.toString(family)+"列名為"+Bytes.toString(qualifier)+"列值為"+Bytes.toInt(value));
} else {
System.out.println("列族為"+Bytes.toString(family)+"列名為"+Bytes.toString(qualifier)+"列值為"+Bytes.toString(value));
}
}
}
}
rowKey前綴過濾器PrefixFilter
/** * row前綴過濾器,查詢以00開頭的所有前綴的rowkey */
@Test
public void prefixFilter() throws IOException {
Scan scan = new Scan();
PrefixFilter prefixFilter = new PrefixFilter("00".getBytes());
scan.setFilter(prefixFilter);
ResultScanner resultScanner = table.getScanner(scan);
for (Result result : resultScanner) {
byte[] row = result.getRow();
System.out.println("數據的rowKey為" + Bytes.toString(row));
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
byte[] qualifier = cell.getQualifier();
byte[] family = cell.getFamily();
byte[] value = cell.getValue();
//id列和age列是整型數據
if ("f1".equals(Bytes.toString(family)) && "id".equals(Bytes.toString(qualifier)) || "age".equals(Bytes.toString(value))){
System.out.println("列族為"+Bytes.toString(family)+"列名為"+Bytes.toString(qualifier)+"列值為"+Bytes.toInt(value));
} else {
System.out.println("列族為"+Bytes.toString(family)+"列名為"+Bytes.toString(qualifier)+"列值為"+Bytes.toString(value));
}
}
}
}
&160;
分頁過濾器PageFilter
/** * 分頁過濾器 * 分頁有兩個條件 * pageNum 第幾頁 * pageSize 每頁有幾條 */
@Test
public void pageFilter() throws IOException {
int pageNum = 3;
int pageSize = 2;
/* 分為兩種情況判斷: 第一頁 其他頁 */
if (pageNum == 1){
Scan scan = new Scan();
//設置起始rowKey
scan.setStartRow("".getBytes());
//設置最大的返回結果,返回pageSize條
scan.setMaxResultSize(pageSize);
//分頁過濾器
PageFilter pageFilter = new PageFilter(pageSize);
scan.setFilter(pageFilter);
ResultScanner resultScanner = table.getScanner(scan);
for (Result result : resultScanner) {
byte[] row = result.getRow();
System.out.println("數據的rowKey為" + Bytes.toString(row));
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
byte[] qualifier = cell.getQualifier();
byte[] family = cell.getFamily();
byte[] value = cell.getValue();
//id列和age列是整型數據
if ("f1".equals(Bytes.toString(family)) && "id".equals(Bytes.toString(qualifier)) || "age".equals(Bytes.toString(value))){
System.out.println("列族為"+Bytes.toString(family)+"列名為"+Bytes.toString(qualifier)+"列值為"+Bytes.toInt(value));
} else {
System.out.println("列族為"+Bytes.toString(family)+"列名為"+Bytes.toString(qualifier)+"列值為"+Bytes.toString(value));
}
}
}
} else {
String startRow = "";
Scan scan = new Scan();
/* 第二頁的起始rowKey = 第一頁的結束rowKey + 1 第三頁的起始rowKey = 第二頁的結束rowKey + 1 */
int resultSize = (pageNum - 1) * pageSize + 1;
scan.setMaxResultSize(resultSize);
//設置一次性往前掃描5條,最后一個rowKey是第三頁起始rowKey
PageFilter pageFilter = new PageFilter(resultSize);
scan.setFilter(pageFilter);
//resultScanner里面有5條數據
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
//獲取rowKey
byte[] row = result.getRow();
//最后一次循環遍歷 rowKey為0005
startRow = Bytes.toString(row);
}
Scan scan1 = new Scan();
scan1.setStartRow(startRow.getBytes());
scan1.setMaxResultSize(pageSize);
PageFilter pageFilter1 = new PageFilter(pageSize);
scan1.setFilter(pageFilter1);
ResultScanner scanner1 = table.getScanner(scan1);
for (Result result : scanner1) {
byte[] row = result.getRow();
System.out.println("數據的rowKey為" + Bytes.toString(row));
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
byte[] qualifier = cell.getQualifier();
byte[] family = cell.getFamily();
byte[] value = cell.getValue();
//id列和age列是整型數據
if ("f1".equals(Bytes.toString(family)) && "id".equals(Bytes.toString(qualifier)) || "age".equals(Bytes.toString(value))){
System.out.println("列族為"+Bytes.toString(family)+"列名為"+Bytes.toString(qualifier)+"列值為"+Bytes.toInt(value));
} else {
System.out.println("列族為"+Bytes.toString(family)+"列名為"+Bytes.toString(qualifier)+"列值為"+Bytes.toString(value));
}
}
}
}
}
多過濾器綜合查詢FilterList
需求: 使用 SingleColumnValueFilter 查詢f1列族,name為劉備的數據,並且同時滿足rowkey的前綴以00開頭的數據(PrefixFilter)
/** * 多過濾綜合查詢 * 需求: 使用 SingleColumnValueFilter 查詢f1列族,name為劉備的數據,並且同時滿足rowkey的前綴以00開頭的數據(PrefixFilter) */
@Test
public void filterList() throws IOException {
Scan scan = new Scan();
SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter("f1".getBytes(),"name".getBytes(), CompareFilter.CompareOp.EQUAL,"劉備".getBytes());
PrefixFilter prefixFilter = new PrefixFilter("00".getBytes());
FilterList filterList = new FilterList(singleColumnValueFilter, prefixFilter);
scan.setFilter(filterList);
ResultScanner resultScanner = table.getScanner(scan);
for (Result result : resultScanner) {
byte[] row = result.getRow();
System.out.println("數據的rowKey為" + Bytes.toString(row));
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
byte[] qualifier = cell.getQualifier();
byte[] value = cell.getValue();
byte[] family = cell.getFamily();
//id列和age列是整型數據
if ("f1".equals(Bytes.toString(family)) && "id".equals(Bytes.toString(qualifier)) || "age".equals(Bytes.toString(value))){
System.out.println("列族為"+Bytes.toString(family)+"列名為"+Bytes.toString(qualifier)+"列值為"+Bytes.toInt(value));
} else {
System.out.println("列族為"+Bytes.toString(family)+"列名為"+Bytes.toString(qualifier)+"列值為"+Bytes.toString(value));
}
}
}
}
代碼實現刪除數據
/** * 根據rowKey刪除數據 */
@Test
public void delete() throws IOException {
Delete delete = new Delete("0007".getBytes());
table.delete(delete);
}