@Override public RowFilter adapt( FilterAdapterContext context, MultipleColumnPrefixFilter filter) throws IOException { Interleave.Builder interleaveBuilder = Interleave.newBuilder(); ByteArrayOutputStream outputStream = null; for (byte[] prefix : filter.getPrefix()) { if (outputStream == null) { outputStream = new ByteArrayOutputStream(prefix.length * 2); } outputStream.reset(); readerExpressionHelper.writeQuotedExpression(prefix, outputStream); outputStream.write(ReaderExpressionHelper.ALL_QUALIFIERS_BYTES); RowFilter.Builder singlePrefixBuilder = RowFilter.newBuilder(); singlePrefixBuilder.setColumnQualifierRegexFilter( ByteString.copyFrom( outputStream.toByteArray())); interleaveBuilder.addFilters(singlePrefixBuilder); } return RowFilter.newBuilder() .setInterleave(interleaveBuilder) .build(); }
@Test public void multiplePrefixesAreAdapted() throws IOException { // Return all columns in all families that are prefixed by "prefix" or prefix2. MultipleColumnPrefixFilter filter = new MultipleColumnPrefixFilter( new byte[][]{Bytes.toBytes("prefix"), Bytes.toBytes("prefix2")}); RowFilter rowFilter = filterAdapter.adapt(emptyScanContext, filter); Interleave interleave = rowFilter.getInterleave(); Assert.assertEquals(2, interleave.getFiltersCount()); Assert.assertEquals( "prefix\\C*", interleave.getFilters(0).getColumnQualifierRegexFilter().toStringUtf8()); Assert.assertEquals( "prefix2\\C*", interleave.getFilters(1).getColumnQualifierRegexFilter().toStringUtf8()); }
/** * 获取这个列名过滤的column * * @param columns * @return */ private Filter getColumnFilter(String[] columns) { int length = columns.length; byte[][] filter = new byte[length][]; for (int i = 0; i < length; i++) { filter[i] = Bytes.toBytes(columns[i]); } return new MultipleColumnPrefixFilter(filter); }
@Test public void testMultipleColumnPrefixes() throws IOException { // Initialize String goodValue = "includeThisValue"; Table table = getConnection().getTable(TABLE_NAME); byte[] rowKey = dataHelper.randomData("testRow-"); Put put = new Put(rowKey); put.addColumn(COLUMN_FAMILY, dataHelper.randomData("a-"), Bytes.toBytes(goodValue)); put.addColumn(COLUMN_FAMILY, dataHelper.randomData("b-"), Bytes.toBytes(goodValue)); put.addColumn(COLUMN_FAMILY, dataHelper.randomData("c-"), Bytes.toBytes(goodValue)); put.addColumn(COLUMN_FAMILY, dataHelper.randomData("d-"), Bytes.toBytes(goodValue)); table.put(put); // Filter for results Filter filter = new MultipleColumnPrefixFilter(new byte[][]{ Bytes.toBytes("a-"), Bytes.toBytes("b-") }); Get get = new Get(rowKey).setFilter(filter); Result result = table.get(get); Cell[] cells = result.rawCells(); Assert.assertEquals("Should have two cells, prefixes a- and b-.", 2, cells.length); byte[] qualifier0 = CellUtil.cloneQualifier(cells[0]); Assert.assertTrue("qualifier0 should start with a-", qualifier0[0] == 'a' && qualifier0[1] == '-'); byte[] qualifier1 = CellUtil.cloneQualifier(cells[1]); Assert.assertTrue("qualifier1 should start with b-", qualifier1[0] == 'b' && qualifier1[1] == '-'); table.close(); }
private static Filter getColumnFilter(String[] columns) { int length = columns.length; byte[][] filter = new byte[length][]; for (int i = 0; i < length; i++) { filter[i] = Bytes.toBytes(columns[i]); } return new MultipleColumnPrefixFilter(filter); }
/** * Create a new FilterAdapter */ public static FilterAdapter buildAdapter() { FilterAdapter adapter = new FilterAdapter(); adapter.addFilterAdapter( ColumnPrefixFilter.class, new ColumnPrefixFilterAdapter()); adapter.addFilterAdapter( ColumnRangeFilter.class, new ColumnRangeFilterAdapter()); adapter.addFilterAdapter( KeyOnlyFilter.class, new KeyOnlyFilterAdapter()); adapter.addFilterAdapter( MultipleColumnPrefixFilter.class, new MultipleColumnPrefixFilterAdapter()); adapter.addFilterAdapter( TimestampsFilter.class, new TimestampsFilterAdapter()); ValueFilterAdapter valueFilterAdapter = new ValueFilterAdapter(); adapter.addFilterAdapter( ValueFilter.class, valueFilterAdapter); SingleColumnValueFilterAdapter scvfa = new SingleColumnValueFilterAdapter(valueFilterAdapter); adapter.addFilterAdapter( SingleColumnValueFilter.class, scvfa); adapter.addFilterAdapter( SingleColumnValueExcludeFilter.class, new SingleColumnValueExcludeFilterAdapter(scvfa)); adapter.addFilterAdapter( ColumnPaginationFilter.class, new ColumnPaginationFilterAdapter()); adapter.addFilterAdapter( FirstKeyOnlyFilter.class, new FirstKeyOnlyFilterAdapter()); adapter.addFilterAdapter( ColumnCountGetFilter.class, new ColumnCountGetFilterAdapter()); adapter.addFilterAdapter( RandomRowFilter.class, new RandomRowFilterAdapter()); adapter.addFilterAdapter( PrefixFilter.class, new PrefixFilterAdapter()); adapter.addFilterAdapter( QualifierFilter.class, new QualifierFilterAdapter()); // Passing the FilterAdapter in to the FilterListAdapter is a bit // unfortunate, but makes adapting the FilterList's subfilters simpler. FilterListAdapter filterListAdapter = new FilterListAdapter(adapter); // FilterList implements UnsupportedStatusCollector so it should // be used when possible (third parameter to addFilterAdapter()). adapter.addFilterAdapter( FilterList.class, filterListAdapter, filterListAdapter); return adapter; }
@Override public FilterSupportStatus isFilterSupported( FilterAdapterContext context, MultipleColumnPrefixFilter filter) { return FilterSupportStatus.SUPPORTED; }