Heim >Datenbank >MySQL-Tutorial >Hbase ValueFilter

Hbase ValueFilter

WBOY
WBOYOriginal
2016-06-07 16:29:511418Durchsuche

Hbase ValueFilter用于过滤值 package com.fatkun.filter.comparison;import java.io.IOException;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.HColumnDescriptor;impo

Hbase ValueFilter用于过滤值

package com.fatkun.filter.comparison;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
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.Filter;
import org.apache.hadoop.hbase.filter.SubstringComparator;
import org.apache.hadoop.hbase.filter.ValueFilter;
import org.apache.hadoop.hbase.util.Bytes;
public class TestHbaseValueFilter {
	String tableName = "test_value_filter";
	Configuration config = HBaseConfiguration.create();
	/**
	 * 部分代码来自hbase权威指南
	 * 
	 * @throws IOException
	 */
	public void testFilter() throws IOException {
		HTable table = new HTable(config, tableName);
		Scan scan = new Scan();
		System.out.println("只列出值包含data1的列");
		Filter filter1 = new ValueFilter(CompareFilter.CompareOp.EQUAL,
				new SubstringComparator("data1"));
		scan.setFilter(filter1);
		ResultScanner scanner1 = table.getScanner(scan);
		for (Result res : scanner1) {
			System.out.println(res);
		}
		scanner1.close();
		System.out.println("get也可以设置filter");
		Get get1 = new Get(Bytes.toBytes("row003"));
		get1.setFilter(filter1);
		Result result1 = table.get(get1);
		System.out.println("Result of get(): " + result1);
	}
	/**
	 * 初始化数据
	 */
	public void init() {
		// 创建表和初始化数据
		try {
			HBaseAdmin admin = new HBaseAdmin(config);
			if (!admin.tableExists(tableName)) {
				HTableDescriptor htd = new HTableDescriptor(tableName);
				HColumnDescriptor hcd1 = new HColumnDescriptor("data1");
				htd.addFamily(hcd1);
				HColumnDescriptor hcd2 = new HColumnDescriptor("data2");
				htd.addFamily(hcd2);
				HColumnDescriptor hcd3 = new HColumnDescriptor("data3");
				htd.addFamily(hcd3);
				admin.createTable(htd);
			}
			HTable table = new HTable(config, tableName);
			table.setAutoFlush(false);
			int count = 50;
			for (int i = 1; i 
    <p class="copyright">
        原文地址:Hbase ValueFilter, 感谢原作者分享。
    </p>
    
    


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn