import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.filter.ByteArrayComparable;
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.SubstringComparator;
import org.apache.hadoop.hbase.filter.BinaryComparator;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.FamilyFilter;
import org.apache.hadoop.hbase.filter.QualifierFilter;
import org.apache.hadoop.hbase.filter.TimestampsFilter;
import org.apache.hadoop.hbase.filter.ValueFilter;
import org.apache.hadoop.hbase.filter.DependentColumnFilter;
@SuppressWarnings("unused")
public class FilterOperationImp implements FilterOperation{
//=======================================================================
//=========================connecation and congriguration img============
/*
* 连接池配置
*/
public static Configuration configuration;
/*
* 连接池对象
*/
public static Connection connection;
private static final String ZK_QUORUM = "hbase.zookeeper.quorum";
private static final String ZK_CLIENT_PORT = "hbase.zookeeper.property.clientPort";
/*
* Admin
*/
public static Admin admin;
/**
* HBase位置
*/
@SuppressWarnings("unused")
private static final String HBASE_POS = "localhost";
/**
* ZooKeeper位置
*/
private static final String ZK_POS = "localhost";
/**
* zookeeper服务端口
*/
private final static String ZK_PORT_VALUE = "2181";
/*
* 建立连接
*/
//==================================================================
//=====================contry open and close========================
public static void init(){
configuration = HBaseConfiguration.create();
configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase");
configuration.set(ZK_QUORUM, ZK_POS);
configuration.set(ZK_CLIENT_PORT, ZK_PORT_VALUE);
try{
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
}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();
}
}
/*======================================================================================================
| start |
======================================================================================================*/
@Override
public void RowFilter() throws IOException {
init();
Table table= connection.getTable(TableName.valueOf("filter_test_table"));
Scan scan=new Scan();
scan.addColumn(Bytes.toBytes("colfam1"),Bytes.toBytes("col-0"));
Filter filter1=new RowFilter(CompareFilter.CompareOp.LESS_OR_EQUAL,
new BinaryComparator(Bytes.toBytes("row-22")));
scan.setFilter(filter1);
ResultScanner ss = table.getScanner(scan);
System.out.println("result1:");
for(Result r:ss){
System.out.println(r);
}
ss.close();
Filter filter2=new RowFilter(CompareFilter.CompareOp.EQUAL,
new RegexStringComparator(".*-.5"));
scan.setFilter(filter2);
ResultScanner ss2 = table.getScanner(scan);
System.out.println("result2:");
for(Result r:ss2){
System.out.println(r);
}
ss2.close();
Filter filter3=new RowFilter(CompareFilter.CompareOp.EQUAL,
new SubstringComparator("-5"));
scan.setFilter(filter3);
ResultScanner ss3 = table.getScanner(scan);
System.out.println("result3:");
for(Result r:ss3){
System.out.println(r);
}
ss3.close();
}
@Override
public void FamilyFilter() throws IOException {
init();
Table table= connection.getTable(TableName.valueOf("filter_test_table"));
Filter filter1=new FamilyFilter(CompareFilter.CompareOp.LESS,
new BinaryComparator(Bytes.toBytes("colfam3")));
Scan scan=new Scan();
scan.setFilter(filter1);
ResultScanner ss = table.getScanner(scan);
System.out.println("result1:");
for(Result r:ss){
System.out.println(r);
}
ss.close();
Get get1=new Get(Bytes.toBytes("row-5"));
get1.setFilter(filter1);
Result result1=table.get(get1);
System.out.println("result of get2:"+result1);
Filter filter2=new FamilyFilter(CompareFilter.CompareOp.EQUAL,
new BinaryComparator(Bytes.toBytes("colfam3")));
Get get2=new Get(Bytes.toBytes("row-5"));
//get2.addFamily(Bytes.toBytes("colfam1"));
get2.setFilter(filter2);
Result result2=table.get(get2);
System.out.println("result of get3:"+result2);
ss.close();
}
@Override
public void QualifierFilter() throws IOException {
init();
Table table= connection.getTable(TableName.valueOf("filter_test_table"));
Filter filter=new QualifierFilter(CompareFilter.CompareOp.LESS_OR_EQUAL,
new BinaryComparator(Bytes.toBytes("col-2")));
Scan scan=new Scan();
scan.setFilter(filter);
ResultScanner ss = table.getScanner(scan);
System.out.println("result1:");
for(Result r:ss){
System.out.println(r);
}
ss.close();
Get get=new Get(Bytes.toBytes("row-5"));
get.setFilter(filter);
Result result=table.get(get);
System.out.println("result of get:"+result);
}
@Override
public void ValueFilter() throws IOException {
init();
Table table= connection.getTable(TableName.valueOf("filter_test_table"));
Filter filter=new ValueFilter(CompareFilter.CompareOp.EQUAL,
new SubstringComparator("5"));
Scan scan=new Scan();
scan.setFilter(filter);
ResultScanner ss = table.getScanner(scan);
for(Result r:ss){
for(KeyValue kv : r.raw()){
System.out.println("kv"+kv+"==value"+new String(kv.getValue()));
}
}
ss.close();
Get get=new Get(Bytes.toBytes("row-5"));
get.setFilter(filter);
Result result=table.get(get);
for(KeyValue kv : result.raw()){
System.out.println("kv"+kv+"==value"+new String(kv.getValue()));
}
}
@Override
public void DependentColumnFilter(boolean drop, CompareOp operator,
ByteArrayComparable comparator) throws IOException {
System.out.println("开始加载hbase配置");
init();
System.out.println("实例化table对象");
Table table= connection.getTable(TableName.valueOf("filter_test_table"));
Filter filter;
if(comparator!=null){
System.out.println("加载filter对象");
filter=new DependentColumnFilter(Bytes.toBytes("colfam1"),Bytes.toBytes("col-1"),drop,operator,comparator);
}else{
filter=new DependentColumnFilter(Bytes.toBytes("colfam1"),Bytes.toBytes("col-1"),drop);
}
Scan scan=new Scan();
scan.setFilter(filter);
ResultScanner ss = table.getScanner(scan);
if(ss!=null){
System.out.println("ss不为空");
for(Result r:ss){
System.out.println("r不为空");
System.out.println(r);
for(KeyValue kv : r.raw()){
System.out.println("kv"+kv+"==value"+new String(kv.getValue()));
}
}
}
ss.close();
System.out.println("创建get对象");
Get get=new Get(Bytes.toBytes("row-5"));
get.setFilter(filter);
Result result=table.get(get);
for(KeyValue kv : result.raw()){
System.out.println("kv"+kv+"==value"+new String(kv.getValue()));
}
}
@Override
public void FilterLIst() throws IOException {
List<Filter>filters=new ArrayList<Filter>();
init();
Table table= connection.getTable(TableName.valueOf("filter_test_table"));
Filter filter1=new RowFilter(CompareFilter.CompareOp.LESS_OR_EQUAL,
new BinaryComparator(Bytes.toBytes("row-22")));
filters.add(filter1);
Filter filter2=new RowFilter(CompareFilter.CompareOp.EQUAL,
new SubstringComparator("5"));
filters.add(filter2);
Filter filter3=new QualifierFilter(CompareFilter.CompareOp.EQUAL,
new RegexStringComparator("col-1"));
filters.add(filter3);
FilterList filterlist1=new FilterList(filters);
Scan scan=new Scan();
scan.setFilter(filterlist1);
ResultScanner ss=table.getScanner(scan);
System.out.println("result1");
for(Result r:ss){
for(KeyValue kv : r.raw()){
System.out.println("kv"+kv+"==value"+new String(kv.getValue()));
}
}
ss.close();
FilterList filterlist2=new FilterList(FilterList.Operator.MUST_PASS_ONE,filters);
scan.setFilter(filterlist2);
ResultScanner ss1=table.getScanner(scan);
System.out.println("result2");
for(Result r:ss1){
for(KeyValue kv : r.raw()){
System.out.println("kv"+kv+"==value"+new String(kv.getValue()));
}
}
ss1.close();
}
@Override
public void TimStampFilter(long time1, long time2, long time3)
throws IOException {
init();
Table table= connection.getTable(TableName.valueOf("filter_test_table"));
List<Long> ts=new ArrayList<Long>();
ts.add(time1);
ts.add(time2);
ts.add(time3);
Filter filter=new TimestampsFilter(ts);
Scan scan=new Scan();
scan.setFilter(filter);
ResultScanner ss=table.getScanner(scan);
System.out.println("result1");
for(Result r:ss){
System.out.println(r);
}
ss.close();
Scan scan2=new Scan();
scan2.setFilter(filter);
scan2.setTimeRange(1508570000000L,1508570688438L);
ResultScanner ss1=table.getScanner(scan2);
System.out.println("result2");
for(Result r:ss1){
System.out.println(r);
}
ss1.close();
}
@Override
public void CustomerFilter() throws IOException {
init();
Table table= connection.getTable(TableName.valueOf("filter_test_table"));
List<Filter> filters=new ArrayList<Filter>();
Filter filter1=new CustomFilter(Bytes.toBytes("5"));
filters.add(filter1);
Scan scan=new Scan();
FilterList fiterlist=new FilterList(filters);
scan.setFilter(fiterlist);
ResultScanner ss=table.getScanner(scan);
System.out.println("result");
for(Result r:ss){
System.out.println(r);
}
ss.close();
}
}
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.filter.ByteArrayComparable;
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.SubstringComparator;
import org.apache.hadoop.hbase.filter.BinaryComparator;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.FamilyFilter;
import org.apache.hadoop.hbase.filter.QualifierFilter;
import org.apache.hadoop.hbase.filter.TimestampsFilter;
import org.apache.hadoop.hbase.filter.ValueFilter;
import org.apache.hadoop.hbase.filter.DependentColumnFilter;
@SuppressWarnings("unused")
public class FilterOperationImp implements FilterOperation{
//=======================================================================
//=========================connecation and congriguration img============
/*
* 连接池配置
*/
public static Configuration configuration;
/*
* 连接池对象
*/
public static Connection connection;
private static final String ZK_QUORUM = "hbase.zookeeper.quorum";
private static final String ZK_CLIENT_PORT = "hbase.zookeeper.property.clientPort";
/*
* Admin
*/
public static Admin admin;
/**
* HBase位置
*/
@SuppressWarnings("unused")
private static final String HBASE_POS = "localhost";
/**
* ZooKeeper位置
*/
private static final String ZK_POS = "localhost";
/**
* zookeeper服务端口
*/
private final static String ZK_PORT_VALUE = "2181";
/*
* 建立连接
*/
//==================================================================
//=====================contry open and close========================
public static void init(){
configuration = HBaseConfiguration.create();
configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase");
configuration.set(ZK_QUORUM, ZK_POS);
configuration.set(ZK_CLIENT_PORT, ZK_PORT_VALUE);
try{
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
}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();
}
}
/*======================================================================================================
| start |
======================================================================================================*/
@Override
public void RowFilter() throws IOException {
init();
Table table= connection.getTable(TableName.valueOf("filter_test_table"));
Scan scan=new Scan();
scan.addColumn(Bytes.toBytes("colfam1"),Bytes.toBytes("col-0"));
Filter filter1=new RowFilter(CompareFilter.CompareOp.LESS_OR_EQUAL,
new BinaryComparator(Bytes.toBytes("row-22")));
scan.setFilter(filter1);
ResultScanner ss = table.getScanner(scan);
System.out.println("result1:");
for(Result r:ss){
System.out.println(r);
}
ss.close();
Filter filter2=new RowFilter(CompareFilter.CompareOp.EQUAL,
new RegexStringComparator(".*-.5"));
scan.setFilter(filter2);
ResultScanner ss2 = table.getScanner(scan);
System.out.println("result2:");
for(Result r:ss2){
System.out.println(r);
}
ss2.close();
Filter filter3=new RowFilter(CompareFilter.CompareOp.EQUAL,
new SubstringComparator("-5"));
scan.setFilter(filter3);
ResultScanner ss3 = table.getScanner(scan);
System.out.println("result3:");
for(Result r:ss3){
System.out.println(r);
}
ss3.close();
}
@Override
public void FamilyFilter() throws IOException {
init();
Table table= connection.getTable(TableName.valueOf("filter_test_table"));
Filter filter1=new FamilyFilter(CompareFilter.CompareOp.LESS,
new BinaryComparator(Bytes.toBytes("colfam3")));
Scan scan=new Scan();
scan.setFilter(filter1);
ResultScanner ss = table.getScanner(scan);
System.out.println("result1:");
for(Result r:ss){
System.out.println(r);
}
ss.close();
Get get1=new Get(Bytes.toBytes("row-5"));
get1.setFilter(filter1);
Result result1=table.get(get1);
System.out.println("result of get2:"+result1);
Filter filter2=new FamilyFilter(CompareFilter.CompareOp.EQUAL,
new BinaryComparator(Bytes.toBytes("colfam3")));
Get get2=new Get(Bytes.toBytes("row-5"));
//get2.addFamily(Bytes.toBytes("colfam1"));
get2.setFilter(filter2);
Result result2=table.get(get2);
System.out.println("result of get3:"+result2);
ss.close();
}
@Override
public void QualifierFilter() throws IOException {
init();
Table table= connection.getTable(TableName.valueOf("filter_test_table"));
Filter filter=new QualifierFilter(CompareFilter.CompareOp.LESS_OR_EQUAL,
new BinaryComparator(Bytes.toBytes("col-2")));
Scan scan=new Scan();
scan.setFilter(filter);
ResultScanner ss = table.getScanner(scan);
System.out.println("result1:");
for(Result r:ss){
System.out.println(r);
}
ss.close();
Get get=new Get(Bytes.toBytes("row-5"));
get.setFilter(filter);
Result result=table.get(get);
System.out.println("result of get:"+result);
}
@Override
public void ValueFilter() throws IOException {
init();
Table table= connection.getTable(TableName.valueOf("filter_test_table"));
Filter filter=new ValueFilter(CompareFilter.CompareOp.EQUAL,
new SubstringComparator("5"));
Scan scan=new Scan();
scan.setFilter(filter);
ResultScanner ss = table.getScanner(scan);
for(Result r:ss){
for(KeyValue kv : r.raw()){
System.out.println("kv"+kv+"==value"+new String(kv.getValue()));
}
}
ss.close();
Get get=new Get(Bytes.toBytes("row-5"));
get.setFilter(filter);
Result result=table.get(get);
for(KeyValue kv : result.raw()){
System.out.println("kv"+kv+"==value"+new String(kv.getValue()));
}
}
@Override
public void DependentColumnFilter(boolean drop, CompareOp operator,
ByteArrayComparable comparator) throws IOException {
System.out.println("开始加载hbase配置");
init();
System.out.println("实例化table对象");
Table table= connection.getTable(TableName.valueOf("filter_test_table"));
Filter filter;
if(comparator!=null){
System.out.println("加载filter对象");
filter=new DependentColumnFilter(Bytes.toBytes("colfam1"),Bytes.toBytes("col-1"),drop,operator,comparator);
}else{
filter=new DependentColumnFilter(Bytes.toBytes("colfam1"),Bytes.toBytes("col-1"),drop);
}
Scan scan=new Scan();
scan.setFilter(filter);
ResultScanner ss = table.getScanner(scan);
if(ss!=null){
System.out.println("ss不为空");
for(Result r:ss){
System.out.println("r不为空");
System.out.println(r);
for(KeyValue kv : r.raw()){
System.out.println("kv"+kv+"==value"+new String(kv.getValue()));
}
}
}
ss.close();
System.out.println("创建get对象");
Get get=new Get(Bytes.toBytes("row-5"));
get.setFilter(filter);
Result result=table.get(get);
for(KeyValue kv : result.raw()){
System.out.println("kv"+kv+"==value"+new String(kv.getValue()));
}
}
@Override
public void FilterLIst() throws IOException {
List<Filter>filters=new ArrayList<Filter>();
init();
Table table= connection.getTable(TableName.valueOf("filter_test_table"));
Filter filter1=new RowFilter(CompareFilter.CompareOp.LESS_OR_EQUAL,
new BinaryComparator(Bytes.toBytes("row-22")));
filters.add(filter1);
Filter filter2=new RowFilter(CompareFilter.CompareOp.EQUAL,
new SubstringComparator("5"));
filters.add(filter2);
Filter filter3=new QualifierFilter(CompareFilter.CompareOp.EQUAL,
new RegexStringComparator("col-1"));
filters.add(filter3);
FilterList filterlist1=new FilterList(filters);
Scan scan=new Scan();
scan.setFilter(filterlist1);
ResultScanner ss=table.getScanner(scan);
System.out.println("result1");
for(Result r:ss){
for(KeyValue kv : r.raw()){
System.out.println("kv"+kv+"==value"+new String(kv.getValue()));
}
}
ss.close();
FilterList filterlist2=new FilterList(FilterList.Operator.MUST_PASS_ONE,filters);
scan.setFilter(filterlist2);
ResultScanner ss1=table.getScanner(scan);
System.out.println("result2");
for(Result r:ss1){
for(KeyValue kv : r.raw()){
System.out.println("kv"+kv+"==value"+new String(kv.getValue()));
}
}
ss1.close();
}
@Override
public void TimStampFilter(long time1, long time2, long time3)
throws IOException {
init();
Table table= connection.getTable(TableName.valueOf("filter_test_table"));
List<Long> ts=new ArrayList<Long>();
ts.add(time1);
ts.add(time2);
ts.add(time3);
Filter filter=new TimestampsFilter(ts);
Scan scan=new Scan();
scan.setFilter(filter);
ResultScanner ss=table.getScanner(scan);
System.out.println("result1");
for(Result r:ss){
System.out.println(r);
}
ss.close();
Scan scan2=new Scan();
scan2.setFilter(filter);
scan2.setTimeRange(1508570000000L,1508570688438L);
ResultScanner ss1=table.getScanner(scan2);
System.out.println("result2");
for(Result r:ss1){
System.out.println(r);
}
ss1.close();
}
@Override
public void CustomerFilter() throws IOException {
init();
Table table= connection.getTable(TableName.valueOf("filter_test_table"));
List<Filter> filters=new ArrayList<Filter>();
Filter filter1=new CustomFilter(Bytes.toBytes("5"));
filters.add(filter1);
Scan scan=new Scan();
FilterList fiterlist=new FilterList(filters);
scan.setFilter(fiterlist);
ResultScanner ss=table.getScanner(scan);
System.out.println("result");
for(Result r:ss){
System.out.println(r);
}
ss.close();
}
}