RowFilter用于过滤row key Operator Description LESS 小于 LESS_OR_EQUAL 小于等于 EQUAL 等于 NOT_EQUAL 不等于 GREATER_OR_EQUAL 大于等于 GREATER 大于 NO_OP 排除所有 Comparator Description BinaryComparator 使用Bytes.compareTo()比较 BinaryPrefix
RowFilter用于过滤row key
Operator | Description |
---|---|
LESS |
小于 |
LESS_OR_EQUAL |
小于等于 |
EQUAL |
等于 |
NOT_EQUAL |
不等于 |
GREATER_OR_EQUAL |
大于等于 |
GREATER |
大于 |
NO_OP |
排除所有 |
Comparator | Description |
---|---|
BinaryComparator |
使用Bytes.compareTo()比较 |
BinaryPrefixComparator |
和BinaryComparator差不多,从前面开始比较 |
NullComparator |
Does?not compare against an actual value but whether a given one is?null , or not?null . |
BitComparator |
Performs?a bitwise comparison, providing a?BitwiseOp ?class with?AND ,?OR , and?XOR ?operators. |
RegexStringComparator |
正则表达式 |
SubstringComparator |
把数据当成字符串,用contains()来判断 |
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.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.BinaryPrefixComparator; import org.apache.hadoop.hbase.filter.CompareFilter; import org.apache.hadoop.hbase.filter.Filter; import org.apache.hadoop.hbase.filter.RegexStringComparator; import org.apache.hadoop.hbase.filter.RowFilter; import org.apache.hadoop.hbase.filter.SubstringComparator; public class TestHbaseRowFilter &#123; String tableName = "test_row_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;"小于等于row010的行"&#41;; Filter filter1 = new RowFilter&#40;CompareFilter.CompareOp.LESS_OR_EQUAL, new BinaryComparator&#40;"row010".getBytes&#40;&#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;"正则获取结尾为5的行"&#41;; Filter filter2 = new RowFilter&#40;CompareFilter.CompareOp.EQUAL, new RegexStringComparator&#40;".*5$"&#41;&#41;; scan.setFilter&#40;filter2&#41;; ResultScanner scanner2 = table.getScanner&#40;scan&#41;; for &#40;Result res : scanner2&#41; &#123; System.out.println&#40;res&#41;; &#125; scanner2.close&#40;&#41;; System.out.println&#40;"包行有5的行"&#41;; Filter filter3 = new RowFilter&#40;CompareFilter.CompareOp.EQUAL, new SubstringComparator&#40;"5"&#41;&#41;; scan.setFilter&#40;filter3&#41;; ResultScanner scanner3 = table.getScanner&#40;scan&#41;; for &#40;Result res : scanner3&#41; &#123; System.out.println&#40;res&#41;; &#125; scanner3.close&#40;&#41;; System.out.println&#40;"开头是row01的"&#41;; Filter filter4 = new RowFilter&#40;CompareFilter.CompareOp.EQUAL, new BinaryPrefixComparator&#40;"row01".getBytes&#40;&#41;&#41;&#41;; scan.setFilter&#40;filter4&#41;; ResultScanner scanner4 = table.getScanner&#40;scan&#41;; for &#40;Result res : scanner4&#41; &#123; System.out.println&#40;res&#41;; &#125; scanner3.close&#40;&#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;"data"&#41;; htd.addFamily&#40;hcd1&#41;; HColumnDescriptor hcd2 = new HColumnDescriptor&#40;"url"&#41;; htd.addFamily&#40;hcd2&#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;"data".getBytes&#40;&#41;, String.format&#40;"col%01d", i % 10&#41; .getBytes&#40;&#41;, String.format&#40;"data%03d", i&#41;.getBytes&#40;&#41;&#41;; p.add&#40;"url".getBytes&#40;&#41;, String.format&#40;"col%01d", i % 10&#41; .getBytes&#40;&#41;, String.format&#40;"url%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; TestHbaseRowFilter test = new TestHbaseRowFilter&#40;&#41;; test.init&#40;&#41;; test.testRowFilter&#40;&#41;; &#125; &#125;
小于等于row010的行 keyvalues={row001/data:col1/1364133382268/Put/vlen=7, row001/url:col1/1364133382268/Put/vlen=6} keyvalues={row002/data:col2/1364133382268/Put/vlen=7, row002/url:col2/1364133382268/Put/vlen=6} keyvalues={row003/data:col3/1364133382268/Put/vlen=7, row003/url:col3/1364133382268/Put/vlen=6} keyvalues={row004/data:col4/1364133382268/Put/vlen=7, row004/url:col4/1364133382268/Put/vlen=6} keyvalues={row005/data:col5/1364133382268/Put/vlen=7, row005/url:col5/1364133382268/Put/vlen=6} keyvalues={row006/data:col6/1364133382268/Put/vlen=7, row006/url:col6/1364133382268/Put/vlen=6} keyvalues={row007/data:col7/1364133382268/Put/vlen=7, row007/url:col7/1364133382268/Put/vlen=6} keyvalues={row008/data:col8/1364133382268/Put/vlen=7, row008/url:col8/1364133382268/Put/vlen=6} keyvalues={row009/data:col9/1364133382268/Put/vlen=7, row009/url:col9/1364133382268/Put/vlen=6} keyvalues={row010/data:col0/1364133382268/Put/vlen=7, row010/url:col0/1364133382268/Put/vlen=6} 正则获取结尾为5的行 keyvalues={row005/data:col5/1364133382268/Put/vlen=7, row005/url:col5/1364133382268/Put/vlen=6} keyvalues={row015/data:col5/1364133382268/Put/vlen=7, row015/url:col5/1364133382268/Put/vlen=6} keyvalues={row025/data:col5/1364133382268/Put/vlen=7, row025/url:col5/1364133382268/Put/vlen=6} keyvalues={row035/data:col5/1364133382268/Put/vlen=7, row035/url:col5/1364133382268/Put/vlen=6} keyvalues={row045/data:col5/1364133382268/Put/vlen=7, row045/url:col5/1364133382268/Put/vlen=6} 包行有5的行 keyvalues={row005/data:col5/1364133382268/Put/vlen=7, row005/url:col5/1364133382268/Put/vlen=6} keyvalues={row015/data:col5/1364133382268/Put/vlen=7, row015/url:col5/1364133382268/Put/vlen=6} keyvalues={row025/data:col5/1364133382268/Put/vlen=7, row025/url:col5/1364133382268/Put/vlen=6} keyvalues={row035/data:col5/1364133382268/Put/vlen=7, row035/url:col5/1364133382268/Put/vlen=6} keyvalues={row045/data:col5/1364133382268/Put/vlen=7, row045/url:col5/1364133382268/Put/vlen=6} keyvalues={row050/data:col0/1364133382268/Put/vlen=7, row050/url:col0/1364133382268/Put/vlen=6} 开头是row01的 keyvalues={row010/data:col0/1364133382268/Put/vlen=7, row010/url:col0/1364133382268/Put/vlen=6} keyvalues={row011/data:col1/1364133382268/Put/vlen=7, row011/url:col1/1364133382268/Put/vlen=6} keyvalues={row012/data:col2/1364133382268/Put/vlen=7, row012/url:col2/1364133382268/Put/vlen=6} keyvalues={row013/data:col3/1364133382268/Put/vlen=7, row013/url:col3/1364133382268/Put/vlen=6} keyvalues={row014/data:col4/1364133382268/Put/vlen=7, row014/url:col4/1364133382268/Put/vlen=6} keyvalues={row015/data:col5/1364133382268/Put/vlen=7, row015/url:col5/1364133382268/Put/vlen=6} keyvalues={row016/data:col6/1364133382268/Put/vlen=7, row016/url:col6/1364133382268/Put/vlen=6} keyvalues={row017/data:col7/1364133382268/Put/vlen=7, row017/url:col7/1364133382268/Put/vlen=6} keyvalues={row018/data:col8/1364133382268/Put/vlen=7, row018/url:col8/1364133382268/Put/vlen=6} keyvalues={row019/data:col9/1364133382268/Put/vlen=7, row019/url:col9/1364133382268/Put/vlen=6}
hbase权威指南
原文地址:hbase RowFilter, 感谢原作者分享。