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 &#123; String tableName = "test_value_filter"; Configuration cOnfig= HBaseConfiguration.create&#40;&#41;; /** * 部分代码来自hbase权威指南 * * @throws IOException */ public void testFilter&#40;&#41; throws IOException &#123; HTable table = new HTable&#40;config, tableName&#41;; Scan scan = new Scan&#40;&#41;; System.out.println&#40;"只列出值包含data1的列"&#41;; Filter filter1 = new ValueFilter&#40;CompareFilter.CompareOp.EQUAL, new SubstringComparator&#40;"data1"&#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;"row003"&#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; TestHbaseValueFilter test = new TestHbaseValueFilter&#40;&#41;; test.init&#40;&#41;; test.testFilter&#40;&#41;; &#125; &#125;
原文地址:Hbase ValueFilter, 感谢原作者分享。