/** * Matches only cells from rows whose keys satisfy the given <a * href="https://github.com/google/re2/wiki/Syntax">RE2 regex</a>. In other words, passes * through the entire row when the key matches, and otherwise produces an empty row. Note that, * since row keys can contain arbitrary bytes, the `\C` escape sequence must be used if a true * wildcard is desired. The `.` character will not match the new line character `\n`, which may * be present in a binary key. */ public Filter regex(@Nonnull ByteString regex) { Preconditions.checkNotNull(regex); return new SimpleFilter(RowFilter.newBuilder().setRowKeyRegexFilter(regex).build()); }
@Test public void keyExactMatchTest() { RowFilter actualFilter = FILTERS.key().exactMatch(ByteString.copyFromUtf8(".*")).toProto(); RowFilter expectedFilter = RowFilter.newBuilder().setRowKeyRegexFilter(ByteString.copyFromUtf8("\\.\\*")).build(); assertThat(actualFilter).isEqualTo(expectedFilter); }
@Test public void keyRegexTest() { RowFilter actualFilter = FILTERS.key().regex(ByteString.copyFromUtf8(".*")).toProto(); RowFilter expectedFilter = RowFilter.newBuilder().setRowKeyRegexFilter(ByteString.copyFromUtf8(".*")).build(); assertThat(actualFilter).isEqualTo(expectedFilter); }
/** Tests reading all rows from a sub-split table. */ @Test public void testReadingWithFilterAndSubSplits() throws Exception { final String table = "TEST-FILTER-SUB-SPLITS"; final int numRows = 1700; final int numSamples = 10; final int numSplits = 20; final long bytesPerRow = 100L; // Set up test table data and sample row keys for size estimation and splitting. makeTableData(table, numRows); service.setupSampleRowKeys(table, numSamples, bytesPerRow); // Generate source and split it. RowFilter filter = RowFilter.newBuilder().setRowKeyRegexFilter(ByteString.copyFromUtf8(".*17.*")).build(); BigtableSource source = new BigtableSource( config.withTableId(ValueProvider.StaticValueProvider.of(table)), filter, Arrays.asList(ByteKeyRange.ALL_KEYS), null /*size*/); List<BigtableSource> splits = source.split(numRows * bytesPerRow / numSplits, null); // Test num splits and split equality. assertThat(splits, hasSize(numSplits)); assertSourcesEqualReferenceSource(source, splits, null /* options */); }
/** Tests reading all rows using a filter. */ @Test public void testReadingWithFilter() throws Exception { final String table = "TEST-FILTER-TABLE"; final int numRows = 1001; List<Row> testRows = makeTableData(table, numRows); String regex = ".*17.*"; final KeyMatchesRegex keyPredicate = new KeyMatchesRegex(regex); Iterable<Row> filteredRows = testRows .stream() .filter( input -> { verifyNotNull(input, "input"); return keyPredicate.apply(input.getKey()); }) .collect(Collectors.toList()); RowFilter filter = RowFilter.newBuilder().setRowKeyRegexFilter(ByteString.copyFromUtf8(regex)).build(); service.setupSampleRowKeys(table, 5, 10L); runReadTest( defaultRead.withTableId(table).withRowFilter(filter), Lists.newArrayList(filteredRows)); }
@Test public void testReadingPrimitiveDisplayData() throws IOException, InterruptedException { final String table = "fooTable"; service.createTable(table); RowFilter rowFilter = RowFilter.newBuilder().setRowKeyRegexFilter(ByteString.copyFromUtf8("foo.*")).build(); DisplayDataEvaluator evaluator = DisplayDataEvaluator.create(); BigtableIO.Read read = BigtableIO.read() .withBigtableOptions(BIGTABLE_OPTIONS) .withTableId(table) .withRowFilter(rowFilter) .withBigtableService(service); Set<DisplayData> displayData = evaluator.displayDataForPrimitiveSourceTransforms(read); assertThat( "BigtableIO.Read should include the table id in its primitive display data", displayData, Matchers.hasItem(hasDisplayItem("tableId"))); assertThat( "BigtableIO.Read should include the row filter, if it exists, in its primitive " + "display data", displayData, Matchers.hasItem(hasDisplayItem("rowFilter"))); }
@Test public void testReadingDisplayData() { RowFilter rowFilter = RowFilter.newBuilder().setRowKeyRegexFilter(ByteString.copyFromUtf8("foo.*")).build(); ByteKeyRange keyRange = ByteKeyRange.ALL_KEYS.withEndKey(ByteKey.of(0xab, 0xcd)); BigtableIO.Read read = BigtableIO.read() .withBigtableOptions(BIGTABLE_OPTIONS) .withTableId("fooTable") .withRowFilter(rowFilter) .withKeyRange(keyRange); DisplayData displayData = DisplayData.from(read); assertThat( displayData, hasDisplayItem( allOf(hasKey("tableId"), hasLabel("Bigtable Table Id"), hasValue("fooTable")))); assertThat(displayData, hasDisplayItem("rowFilter", rowFilter.toString())); assertThat(displayData, hasDisplayItem("keyRange 0", keyRange.toString())); // BigtableIO adds user-agent to options; assert only on key and not value. assertThat(displayData, hasDisplayItem("bigtableOptions")); }
/** * Matches only cells from rows whose keys satisfy the given <a * href="https://github.com/google/re2/wiki/Syntax">RE2 regex</a>. In other words, passes * through the entire row when the key matches, and otherwise produces an empty row. Note that, * since row keys can contain arbitrary bytes, the `\C` escape sequence must be used if a true * wildcard is desired. The `.` character will not match the new line character `\n`, which may * be present in a binary key. */ public Filter regex(@Nonnull ByteString regex) { Preconditions.checkNotNull(regex); return new SimpleFilter(RowFilter.newBuilder().setRowKeyRegexFilter(regex).build()); }
/** * Matches only cells from rows whose keys satisfy the given <a * href="https://github.com/google/re2/wiki/Syntax">RE2 regex</a>. In other words, passes * through the entire row when the key matches, and otherwise produces an empty row. Note that, * since row keys can contain arbitrary bytes, the `\C` escape sequence must be used if a true * wildcard is desired. The `.` character will not match the new line character `\n`, which may * be present in a binary key. */ public Filter regex(@Nonnull ByteString regex) { Preconditions.checkNotNull(regex); return new SimpleFilter(RowFilter.newBuilder().setRowKeyRegexFilter(regex).build()); }
setRowKeyRegexFilter(other.getRowKeyRegexFilter()); break;
@Test public void chainTest() { RowFilter actualProto = FILTERS .chain() .filter(FILTERS.key().regex(".*")) .filter(FILTERS.key().sample(0.5)) .filter(FILTERS.chain().filter(FILTERS.family().regex("hi$")).filter(FILTERS.pass())) .toProto(); RowFilter expectedFilter = RowFilter.newBuilder() .setChain( Chain.newBuilder() .addFilters( RowFilter.newBuilder().setRowKeyRegexFilter(ByteString.copyFromUtf8(".*"))) .addFilters(RowFilter.newBuilder().setRowSampleFilter(0.5)) .addFilters( RowFilter.newBuilder() .setChain( Chain.newBuilder() .addFilters( RowFilter.newBuilder().setFamilyNameRegexFilter("hi$")) .addFilters(RowFilter.newBuilder().setPassAllFilter(true))))) .build(); assertThat(actualProto).isEqualTo(expectedFilter); }
@Test public void interleaveTest() { RowFilter actualProto = FILTERS .interleave() .filter(FILTERS.key().regex(".*")) .filter(FILTERS.key().sample(0.5)) .filter( FILTERS.interleave().filter(FILTERS.family().regex("hi$")).filter(FILTERS.pass())) .toProto(); RowFilter expectedFilter = RowFilter.newBuilder() .setInterleave( Interleave.newBuilder() .addFilters( RowFilter.newBuilder().setRowKeyRegexFilter(ByteString.copyFromUtf8(".*"))) .addFilters(RowFilter.newBuilder().setRowSampleFilter(0.5)) .addFilters( RowFilter.newBuilder() .setInterleave( Interleave.newBuilder() .addFilters( RowFilter.newBuilder().setFamilyNameRegexFilter("hi$")) .addFilters( RowFilter.newBuilder().setPassAllFilter(true).build())))) .build(); assertThat(actualProto).isEqualTo(expectedFilter); }
@Test public void conditionTest() { RowFilter actualFilter = FILTERS .condition(FILTERS.key().regex(".*")) .then(FILTERS.label("true")) .otherwise(FILTERS.label("false")) .toProto(); RowFilter expectedFilter = RowFilter.newBuilder() .setCondition( Condition.newBuilder() .setPredicateFilter( RowFilter.newBuilder().setRowKeyRegexFilter(ByteString.copyFromUtf8(".*"))) .setTrueFilter(RowFilter.newBuilder().setApplyLabelTransformer("true")) .setFalseFilter(RowFilter.newBuilder().setApplyLabelTransformer("false"))) .build(); assertThat(actualFilter).isEqualTo(expectedFilter); }
@Test public void testFromProto() { ReadRowsRequest request = ReadRowsRequest.newBuilder() .setTableName(NameUtil.formatTableName(PROJECT_ID, INSTANCE_ID, TABLE_ID)) .setAppProfileId(APP_PROFILE_ID) .setFilter(RowFilter.newBuilder().setRowKeyRegexFilter(ByteString.copyFromUtf8(".*"))) .setRows( RowSet.newBuilder() .addRowKeys(ByteString.copyFromUtf8("row-key")) .addRowRanges( RowRange.newBuilder() .setStartKeyClosed(ByteString.copyFromUtf8("j")) .setEndKeyClosed(ByteString.copyFromUtf8("z")))) .build(); Query query = Query.fromProto(request); assertThat(query.toProto(requestContext)).isEqualTo(request); }
@Test public void filterTest() { Query query = Query.create(TABLE_ID).filter(FILTERS.key().regex(".*")); Builder expectedProto = expectedProtoBuilder() .setFilter(RowFilter.newBuilder().setRowKeyRegexFilter(ByteString.copyFromUtf8(".*"))); ReadRowsRequest actualProto = query.toProto(requestContext); assertThat(actualProto).isEqualTo(expectedProto.build()); }
@Test public void chainSingleTest() { RowFilter actualProto = FILTERS.chain().filter(FILTERS.key().regex(".*")).toProto(); RowFilter expectedFilter = RowFilter.newBuilder().setRowKeyRegexFilter(ByteString.copyFromUtf8(".*")).build(); assertThat(actualProto).isEqualTo(expectedFilter); }
@Test public void conditionTest() { ConditionalRowMutation mutation = ConditionalRowMutation.create(TABLE_ID, TEST_KEY) .condition(Filters.FILTERS.key().regex("a.*")) .then(Mutation.create().deleteRow()); CheckAndMutateRowRequest actualProto = mutation.toProto(REQUEST_CONTEXT); assertThat(actualProto.getPredicateFilter()) .isEqualTo( RowFilter.newBuilder().setRowKeyRegexFilter(ByteString.copyFromUtf8("a.*")).build()); }
@Test public void keyExactMatchStringTest() { RowFilter actualFilter = FILTERS.key().exactMatch(".*").toProto(); RowFilter expectedFilter = RowFilter.newBuilder().setRowKeyRegexFilter(ByteString.copyFromUtf8("\\.\\*")).build(); assertThat(actualFilter).isEqualTo(expectedFilter); }
@Test public void interleaveSingleTest() { RowFilter actualProto = FILTERS.interleave().filter(FILTERS.key().regex(".*")).toProto(); RowFilter expectedFilter = RowFilter.newBuilder().setRowKeyRegexFilter(ByteString.copyFromUtf8(".*")).build(); assertThat(actualProto).isEqualTo(expectedFilter); }
@Test public void keyRegexStringTest() { RowFilter actualFilter = FILTERS.key().regex(".*").toProto(); RowFilter expectedFilter = RowFilter.newBuilder().setRowKeyRegexFilter(ByteString.copyFromUtf8(".*")).build(); assertThat(actualFilter).isEqualTo(expectedFilter); }