Hbase多条件查询数据(FilterList)
利⽤Filter进⾏筛选:HBase的Scan可以通过setFilter⽅法添加过滤器(Filter),这也是分页、多条件查询的基础。HBase为筛选数据提供了⼀组过滤器,通过这个过滤器可以在HBase中的数据的多个维度(⾏,列,数据版本)上进⾏对数据的筛选操作。Filter是可以加多个的,HBase提供⼗多种Filter类型。filterList.addFilter(scvf) 就是可以添加多个查询条件,然后调⽤setFilter函数给Scanner。
直接附上源码:(多条件查询之后对该2⾏键⼀⾏数据删除)
package cn.edu.zucc.hbase;
import org.f.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.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import java.io.IOException;
import org.apache.hadoop.hbase.KeyValue;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;
public class test {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
public static void QueryByCondition3(String tableName) {
try {
init();
Table table = Table(TableName.valueOf(tableName));
List<Filter> filters = new ArrayList<Filter>();
Filter filter1 = new Bytes("Name"), null, CompareOp.EQUAL,
filters.add(filter1);
Filter filter2 = new Bytes("Score"), null, CompareOp.EQUAL,
filters.add(filter2);
/* Filter filter3 = new Bytes("column3"), null, CompareOp.EQUAL,
filters.add(filter3);*/
FilterList filterList1 = new FilterList(filters);
hbase为什么查询快Scan scan = new Scan();
scan.setFilter(filterList1);
ResultScanner rs = Scanner(scan);
System.out.println(rs);
String delrow="";
for (Result r : rs) {
System.out.println("获得到rowkey:" + new Row()));
delrow=new Row());
for (KeyValue keyValue : r.raw()) {
System.out.println(
"列:" + new Family())+new Qualifier()) + "====值:" + new Value()));
}
}
Delete delete = new Bytes());
//删除指定列族
//delete.Bytes(colFamily));
//删除指定列
//delete.Bytes(colFamily),Bytes(col));
table.delete(delete);
System.out.println("删除成功!");
table.close();
close();
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void init() {
configuration = ate();
configuration.set("dir", "hdfs://localhost:9000/hbase");
try {
connection = ateConnection(configuration);            admin = Admin();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void close() {
try {
if (admin != null) {
admin.close();
}
if (null != connection) {
connection.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
QueryByCondition3("person");
System.out.println("over111");
} catch (Exception e) {
e.printStackTrace();
}
}
}
结果:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。