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 &#123; String tableName = "test_family_filter"; Configuration cOnfig= HBaseConfiguration.create&#40;&#41;; /** * 部分代码来自hbase权威指南 * @throws IOException */ public void testRowFilter&#40;&#41; throws IOException &#123; HTable table = new HTable&#40;config, tableName&#41;; Scan scan = new Scan&#40;&#41;; System.out.println&#40;"只列出小于data2的列"&#41;; Filter filter1 = new FamilyFilter&#40;CompareFilter.CompareOp.LESS, new BinaryComparator&#40;Bytes.toBytes&#40;"data2"&#41;&#41;&#41;; scan.setFilter&#40;filter1&#41;; ResultScanner scanner1 = table.getScanner&#40;scan&#41;; for &#40;Result res : scanner1&#41; &#123; System.out.println&#40;res&#41;; &#125; scanner1.close&#40;&#41;; System.out.println&#40;"get也可以设置filter"&#41;; Get get1 = new Get&#40;Bytes.toBytes&#40;"row005"&#41;&#41;; get1.setFilter&#40;filter1&#41;; Result result1 = table.get&#40;get1&#41;; System.out.println&#40;"Result of get(): " + result1&#41;; &#125; /** * 初始化数据 */ public void init&#40;&#41; &#123; // 创建表和初始化数据 try &#123; HBaseAdmin admin = new HBaseAdmin&#40;config&#41;; if &#40;!admin.tableExists&#40;tableName&#41;&#41; &#123; HTableDescriptor htd = new HTableDescriptor&#40;tableName&#41;; HColumnDescriptor hcd1 = new HColumnDescriptor&#40;"data1"&#41;; htd.addFamily&#40;hcd1&#41;; HColumnDescriptor hcd2 = new HColumnDescriptor&#40;"data2"&#41;; htd.addFamily&#40;hcd2&#41;; HColumnDescriptor hcd3 = new HColumnDescriptor&#40;"data3"&#41;; htd.addFamily&#40;hcd3&#41;; admin.createTable&#40;htd&#41;; &#125; HTable table = new HTable&#40;config, tableName&#41;; table.setAutoFlush&#40;false&#41;; int count = 50; for &#40;int i = 1; i <= count; ++i&#41; &#123; Put p = new Put&#40;String.format&#40;"row%03d", i&#41;.getBytes&#40;&#41;&#41;; p.add&#40;"data1".getBytes&#40;&#41;, String.format&#40;"col%01d", i % 10&#41; .getBytes&#40;&#41;, String.format&#40;"data1%03d", i&#41;.getBytes&#40;&#41;&#41;; p.add&#40;"data2".getBytes&#40;&#41;, String.format&#40;"col%01d", i % 10&#41; .getBytes&#40;&#41;, String.format&#40;"data2%03d", i&#41;.getBytes&#40;&#41;&#41;; p.add&#40;"data3".getBytes&#40;&#41;, String.format&#40;"col%01d", i % 10&#41; .getBytes&#40;&#41;, String.format&#40;"data3%03d", i&#41;.getBytes&#40;&#41;&#41;; table.put&#40;p&#41;; &#125; table.close&#40;&#41;; &#125; catch &#40;IOException e&#41; &#123; e.printStackTrace&#40;&#41;; &#125; &#125; /** * @param args * @throws IOException */ public static void main&#40;String&#91;&#93; args&#41; throws IOException &#123; TestHbaseFamilyFilter test = new TestHbaseFamilyFilter&#40;&#41;; test.init&#40;&#41;; test.testRowFilter&#40;&#41;; &#125; &#125;
原文地址:Hbase FamilyFilter, 感谢原作者分享。