hbase 0.94的filter测试代码

1,RowFilter的使用

package hTableManagement;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.HTable;
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.filter.BinaryComparator;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.PageFilter;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.filter.SubstringComparator;
import org.apache.hadoop.hbase.util.Bytes;

public class MyRowFilter {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		Configuration conf = HBaseConfiguration.create();
		
		HTable table = new HTable(conf, "p9");
		
		Scan scan = new Scan();
//		scan.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("p1"));
		RowFilter filter1 = new RowFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("6jnvv2k"));
			
		scan.setFilter(filter1);
		ResultScanner rs = table.getScanner(scan);
		try
		{
			for(Result rr = rs.next(); rr != null; rr = rs.next()) {
				for(KeyValue kv : rr.raw()) {
					System.out.println("KV:" + kv + "value: " + Bytes.toString(kv.getValue()));
				}
			}
		}
		catch(Exception e) {
			e.printStackTrace();
			rs.close();
		}
	}

}


 

2,PageFilter返回指定数目的结果

package hTableManagement;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.HTable;
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.filter.PageFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.util.Bytes;

public class PageFilterTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		Configuration conf = HBaseConfiguration.create();
		
		HTable table = new HTable(conf, "p9");
		
		Scan scan = new Scan();
		scan.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("p1"));
		PageFilter pageFilter = new PageFilter(2);		


		scan.setFilter(pageFilter);
		ResultScanner rs = table.getScanner(scan);
		try
		{
			for(Result rr = rs.next(); rr != null; rr = rs.next()) {
				for(KeyValue kv : rr.raw()) {
					System.out.println("KV:" + kv + "value: " + Bytes.toString(kv.getValue()));
				}
			}
		}
		catch(Exception e) {
			e.printStackTrace();
			rs.close();
		}
	}

}


3,SingleColumnValueFilter测试

package hTableManagement;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.HTable;
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.filter.FilterList;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.util.Bytes;

public class SingleColumnValueFilterTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		Configuration conf = HBaseConfiguration.create();
		
		HTable table = new HTable(conf, "p9");
		
		Scan scan = new Scan();
					
		SingleColumnValueFilter filter1 = new SingleColumnValueFilter(
				Bytes.toBytes("cf"), 
				Bytes.toBytes("p1"),
				CompareOp.EQUAL,
				Bytes.toBytes("13826021287"));
		filter1.setFilterIfMissing(true);
		filter1.setLatestVersionOnly(false);

		scan.setFilter(filter1);
		ResultScanner rs = table.getScanner(scan);
		try
		{
			for(Result rr = rs.next(); rr != null; rr = rs.next()) {
				for(KeyValue kv : rr.raw()) {
					System.out.println("KV:" + kv + "value: " + Bytes.toString(kv.getValue()));
				}
			}
		}
		catch(Exception e) {
			e.printStackTrace();
			rs.close();
		}
	}

}


 

4,SingleColumnValueFilter正则表达式测试

package hTableManagement;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.HTable;
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.filter.FilterList;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.util.Bytes;

public class MyFilterList_Regex {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
Configuration conf = HBaseConfiguration.create();
		
		HTable table = new HTable(conf, "p9");
		
		Scan scan = new Scan();
		scan.setStartRow(Bytes.toBytes("lgofboem9q6jnvv2kfcpj3saq09h8u5ygy8"));
		scan.setStopRow(Bytes.toBytes("lgofboem9q6jnvv2kfcpj3saq09h8u5ygy85"));		
		FilterList filters = new FilterList(FilterList.Operator.MUST_PASS_ALL);
		RegexStringComparator com = new RegexStringComparator("138\\d{8}");
		SingleColumnValueFilter filter1 = new SingleColumnValueFilter(
				Bytes.toBytes("cf"), 
				Bytes.toBytes("p1"),
				CompareOp.EQUAL,
				com);
		filter1.setFilterIfMissing(true);
		filter1.setLatestVersionOnly(false);
		filters.addFilter(filter1);

		scan.setFilter(filters);
		ResultScanner rs = table.getScanner(scan);
		try
		{
			for(Result rr = rs.next(); rr != null; rr = rs.next()) {
				for(KeyValue kv : rr.raw()) {
					System.out.println("KV:" + kv + "value: " + Bytes.toString(kv.getValue()));
				}
			}
		}
		catch(Exception e) {
			e.printStackTrace();
			rs.close();
		}
	}

}


5,FilterList的MUST_PASS_ALL测试

package hTableManagement;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.HTable;
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.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;

public class MyFilterList_NotAll {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
Configuration conf = HBaseConfiguration.create();
		
		HTable table = new HTable(conf, "p9");
		
		Scan scan = new Scan();
		scan.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("p1"));
		scan.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("p2"));
		scan.setStartRow(Bytes.toBytes("lgofboem9q6jnvv2kfcpj3saq09h8u5ygy8"));
		scan.setStopRow(Bytes.toBytes("lgofboem9q6jnvv2kfcpj3saq09h8u5ygy89"));		
		FilterList filters = new FilterList(FilterList.Operator.MUST_PASS_ALL);
		SingleColumnValueFilter filter1 = new SingleColumnValueFilter(
				Bytes.toBytes("cf"), 
				Bytes.toBytes("p1"),
				CompareOp.EQUAL,
				Bytes.toBytes("13826021287"));
		filter1.setFilterIfMissing(true);
		filter1.setLatestVersionOnly(false);
		filters.addFilter(filter1);
		SingleColumnValueFilter filter2 = new SingleColumnValueFilter(
				Bytes.toBytes("cf"), 
				Bytes.toBytes("p2"),
				CompareOp.EQUAL,
				Bytes.toBytes("13828404306"));
		filter2.setFilterIfMissing(true);
		filter2.setLatestVersionOnly(false);
		filters.addFilter(filter2);
		scan.setFilter(filters);
		ResultScanner rs = table.getScanner(scan);
		try
		{
			for(Result rr = rs.next(); rr != null; rr = rs.next()) {
				for(KeyValue kv : rr.raw()) {
					System.out.println("KV:" + kv + "value: " + Bytes.toString(kv.getValue()));
				}
			}
		}
		catch(Exception e) {
			e.printStackTrace();
			rs.close();
		}
	}

}


6,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值