• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Hbase FamilyFilter

mysql 搞代码 4年前 (2022-01-09) 17次浏览 已收录 0个评论

FamilyFilter 用于过滤Family package com.fatkun.filter;import java.io.IOException;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.HColumnDescriptor;import org.apac

FamilyFilter 用于过滤Family

package com.fatkun.filter;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.FamilyFilter;import org.apache.hadoop.hbase.filter.Filter;import org.apache.hadoop.hbase.util.Bytes;public class TestHbaseFamilyFilter {	String tableName = "test_family_filter";	Configuration config = HBase<a>本文来源gao($daima.com搞@代@#码8网^</a>Configuration.create();	/**	 * 部分代码来自hbase权威指南	 * @throws IOException	 */	public void testRowFilter() throws IOException {		HTable table = new HTable(config, tableName);		Scan scan = new Scan();		System.out.println("只列出小于data2的列");		Filter filter1 = new FamilyFilter(CompareFilter.CompareOp.LESS, 			      new BinaryComparator(Bytes.toBytes("data2")));		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("row005"));	    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 <= count; ++i) {				Put p = new Put(String.format("row%03d", i).getBytes());				p.add("data1".getBytes(), String.format("col%01d", i % 10)						.getBytes(), String.format("data1%03d", i).getBytes());				p.add("data2".getBytes(), String.format("col%01d", i % 10)						.getBytes(), String.format("data2%03d", i).getBytes());				p.add("data3".getBytes(), String.format("col%01d", i % 10)						.getBytes(), String.format("data3%03d", i).getBytes());				table.put(p);			}			table.close();		} catch (IOException e) {			e.printStackTrace();		}	}	/**	 * @param args	 * @throws IOException	 */	public static void main(String[] args) throws IOException {		TestHbaseFamilyFilter test = new TestHbaseFamilyFilter();		test.init();		test.testRowFilter();	}}

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Hbase FamilyFilter
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址