Search in sources :

Example 1 with ValueProvider

use of org.apache.beam.sdk.options.ValueProvider in project beam by apache.

the class BigtableIOTest method testReadingWithRuntimeParameterizedFilter.

/**
 * Tests reading rows using a filter provided through ValueProvider.
 */
@Test
public void testReadingWithRuntimeParameterizedFilter() 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(StaticValueProvider.of(filter)), Lists.newArrayList(filteredRows));
}
Also used : ExpectedLogs(org.apache.beam.sdk.testing.ExpectedLogs) Mutation(com.google.bigtable.v2.Mutation) Arrays(java.util.Arrays) PipelineExecutionException(org.apache.beam.sdk.Pipeline.PipelineExecutionException) Matchers.hasItems(org.hamcrest.Matchers.hasItems) BoundedReader(org.apache.beam.sdk.io.BoundedSource.BoundedReader) Create(org.apache.beam.sdk.transforms.Create) Map(java.util.Map) Window(org.apache.beam.sdk.transforms.windowing.Window) GlobalWindow(org.apache.beam.sdk.transforms.windowing.GlobalWindow) Wait(org.apache.beam.sdk.transforms.Wait) Predicates(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Predicates) ValueProvider(org.apache.beam.sdk.options.ValueProvider) KvCoder(org.apache.beam.sdk.coders.KvCoder) GcpOptions(org.apache.beam.sdk.extensions.gcp.options.GcpOptions) Matchers.allOf(org.hamcrest.Matchers.allOf) Family(com.google.bigtable.v2.Family) StandardCharsets(java.nio.charset.StandardCharsets) Serializable(java.io.Serializable) CompletionStage(java.util.concurrent.CompletionStage) Verify.verifyNotNull(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Verify.verifyNotNull) MutateRowResponse(com.google.bigtable.v2.MutateRowResponse) TestStream(org.apache.beam.sdk.testing.TestStream) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) SourceTestUtils.assertSplitAtFractionSucceedsAndConsistent(org.apache.beam.sdk.testing.SourceTestUtils.assertSplitAtFractionSucceedsAndConsistent) DisplayDataMatchers.hasDisplayItem(org.apache.beam.sdk.transforms.display.DisplayDataMatchers.hasDisplayItem) DisplayDataMatchers.hasLabel(org.apache.beam.sdk.transforms.display.DisplayDataMatchers.hasLabel) Preconditions.checkNotNull(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkNotNull) KV(org.apache.beam.sdk.values.KV) TypeDescriptor(org.apache.beam.sdk.values.TypeDescriptor) Duration(org.joda.time.Duration) RunWith(org.junit.runner.RunWith) ArrayList(java.util.ArrayList) DisplayDataMatchers.hasValue(org.apache.beam.sdk.transforms.display.DisplayDataMatchers.hasValue) TestPipeline(org.apache.beam.sdk.testing.TestPipeline) Preconditions.checkArgument(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkArgument) Matchers.lessThan(org.hamcrest.Matchers.lessThan) SetCell(com.google.bigtable.v2.Mutation.SetCell) Matchers.hasSize(org.hamcrest.Matchers.hasSize) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Before(org.junit.Before) DoFn(org.apache.beam.sdk.transforms.DoFn) PAssert(org.apache.beam.sdk.testing.PAssert) IterableCoder(org.apache.beam.sdk.coders.IterableCoder) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) TreeMap(java.util.TreeMap) BoundedWindow(org.apache.beam.sdk.transforms.windowing.BoundedWindow) SourceTestUtils.assertSplitAtFractionExhaustive(org.apache.beam.sdk.testing.SourceTestUtils.assertSplitAtFractionExhaustive) IntervalWindow(org.apache.beam.sdk.transforms.windowing.IntervalWindow) Assert.assertEquals(org.junit.Assert.assertEquals) ByteKeyRange(org.apache.beam.sdk.io.range.ByteKeyRange) BulkOptions(com.google.cloud.bigtable.config.BulkOptions) SampleRowKeysResponse(com.google.bigtable.v2.SampleRowKeysResponse) SerializableFunction(org.apache.beam.sdk.transforms.SerializableFunction) Column(com.google.bigtable.v2.Column) Description(org.apache.beam.sdk.options.Description) RowFilter(com.google.bigtable.v2.RowFilter) PipelineRunMissingException(org.apache.beam.sdk.testing.TestPipeline.PipelineRunMissingException) SourceTestUtils.assertSourcesEqualReferenceSource(org.apache.beam.sdk.testing.SourceTestUtils.assertSourcesEqualReferenceSource) Cell(com.google.bigtable.v2.Cell) Row(com.google.bigtable.v2.Row) VarLongCoder(org.apache.beam.sdk.coders.VarLongCoder) Collectors(java.util.stream.Collectors) ByteString(com.google.protobuf.ByteString) BigtableSource(org.apache.beam.sdk.io.gcp.bigtable.BigtableIO.BigtableSource) List(java.util.List) ParDo(org.apache.beam.sdk.transforms.ParDo) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) ImmutableList(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList) SortedMap(java.util.SortedMap) RetryOptions(com.google.cloud.bigtable.config.RetryOptions) ByteKey(org.apache.beam.sdk.io.range.ByteKey) Coder(org.apache.beam.sdk.coders.Coder) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) PipelineOptionsFactory(org.apache.beam.sdk.options.PipelineOptionsFactory) DisplayDataMatchers.hasKey(org.apache.beam.sdk.transforms.display.DisplayDataMatchers.hasKey) StringUtf8Coder(org.apache.beam.sdk.coders.StringUtf8Coder) ByteStringCoder(org.apache.beam.sdk.extensions.protobuf.ByteStringCoder) SourceTestUtils.assertSplitAtFractionFails(org.apache.beam.sdk.testing.SourceTestUtils.assertSplitAtFractionFails) ProtoCoder(org.apache.beam.sdk.extensions.protobuf.ProtoCoder) CredentialType(com.google.cloud.bigtable.config.CredentialOptions.CredentialType) NoSuchElementException(java.util.NoSuchElementException) ExpectedException(org.junit.rules.ExpectedException) Predicate(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Predicate) Nullable(org.checkerframework.checker.nullness.qual.Nullable) BigtableOptions(com.google.cloud.bigtable.config.BigtableOptions) DisplayData(org.apache.beam.sdk.transforms.display.DisplayData) Iterator(java.util.Iterator) Assert.assertNotNull(org.junit.Assert.assertNotNull) CredentialOptions(com.google.cloud.bigtable.config.CredentialOptions) FixedWindows(org.apache.beam.sdk.transforms.windowing.FixedWindows) Lists(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Lists) Matchers(org.hamcrest.Matchers) JUnit4(org.junit.runners.JUnit4) PCollection(org.apache.beam.sdk.values.PCollection) TestCredential(org.apache.beam.sdk.extensions.gcp.auth.TestCredential) Rule(org.junit.Rule) Instant(org.joda.time.Instant) StaticValueProvider(org.apache.beam.sdk.options.ValueProvider.StaticValueProvider) Comparator(java.util.Comparator) Collections(java.util.Collections) IsIterableContainingInAnyOrder(org.hamcrest.collection.IsIterableContainingInAnyOrder) RowFilter(com.google.bigtable.v2.RowFilter) ByteString(com.google.protobuf.ByteString) Row(com.google.bigtable.v2.Row) Test(org.junit.Test)

Example 2 with ValueProvider

use of org.apache.beam.sdk.options.ValueProvider in project beam by apache.

the class PubsubIOExternalTest method testConstructPubsubWrite.

@Test
public void testConstructPubsubWrite() throws Exception {
    String topic = "projects/project-1234/topics/topic_name";
    String idAttribute = "id_foo";
    ExternalTransforms.ExternalConfigurationPayload payload = encodeRow(Row.withSchema(Schema.of(Field.of("topic", FieldType.STRING), Field.of("id_label", FieldType.STRING))).withFieldValue("topic", topic).withFieldValue("id_label", idAttribute).build());
    // Requirements are not passed as part of the expansion service so the validation
    // fails because of how we construct the pipeline to expand the transform since it now
    // has a transform with a requirement.
    Pipeline p = Pipeline.create();
    p.apply("unbounded", Impulse.create()).setIsBoundedInternal(PCollection.IsBounded.UNBOUNDED);
    RunnerApi.Pipeline pipelineProto = PipelineTranslation.toProto(p);
    String inputPCollection = Iterables.getOnlyElement(Iterables.getLast(pipelineProto.getComponents().getTransformsMap().values()).getOutputsMap().values());
    ExpansionApi.ExpansionRequest request = ExpansionApi.ExpansionRequest.newBuilder().setComponents(pipelineProto.getComponents()).setTransform(RunnerApi.PTransform.newBuilder().setUniqueName("test").putInputs("input", inputPCollection).setSpec(RunnerApi.FunctionSpec.newBuilder().setUrn(ExternalWrite.URN).setPayload(payload.toByteString()))).setNamespace("test_namespace").build();
    ExpansionService expansionService = new ExpansionService();
    TestStreamObserver<ExpansionApi.ExpansionResponse> observer = new TestStreamObserver<>();
    expansionService.expand(request, observer);
    ExpansionApi.ExpansionResponse result = observer.result;
    RunnerApi.PTransform transform = result.getTransform();
    assertThat(transform.getSubtransformsList(), Matchers.hasItem(MatchesPattern.matchesPattern(".*MapElements.*")));
    assertThat(transform.getSubtransformsList(), Matchers.hasItem(MatchesPattern.matchesPattern(".*PubsubUnboundedSink.*")));
    assertThat(transform.getInputsCount(), Matchers.is(1));
    assertThat(transform.getOutputsCount(), Matchers.is(0));
    // test_namespacetest/PubsubUnboundedSink
    RunnerApi.PTransform writeComposite = result.getComponents().getTransformsOrThrow(transform.getSubtransforms(1));
    // test_namespacetest/PubsubUnboundedSink/PubsubSink
    RunnerApi.PTransform writeComposite2 = result.getComponents().getTransformsOrThrow(writeComposite.getSubtransforms(1));
    // test_namespacetest/PubsubUnboundedSink/PubsubSink/PubsubUnboundedSink.Writer
    RunnerApi.PTransform writeComposite3 = result.getComponents().getTransformsOrThrow(writeComposite2.getSubtransforms(3));
    // test_namespacetest/PubsubUnboundedSink/PubsubSink/PubsubUnboundedSink.Writer/ParMultiDo(Writer)
    RunnerApi.PTransform writeParDo = result.getComponents().getTransformsOrThrow(writeComposite3.getSubtransforms(0));
    RunnerApi.ParDoPayload parDoPayload = RunnerApi.ParDoPayload.parseFrom(writeParDo.getSpec().getPayload());
    DoFn<?, ?> pubsubWriter = ParDoTranslation.getDoFn(parDoPayload);
    String idAttributeActual = (String) Whitebox.getInternalState(pubsubWriter, "idAttribute");
    ValueProvider<PubsubClient.TopicPath> topicActual = (ValueProvider<PubsubClient.TopicPath>) Whitebox.getInternalState(pubsubWriter, "topic");
    assertThat(topicActual == null ? null : String.valueOf(topicActual), Matchers.is(topic));
    assertThat(idAttributeActual, Matchers.is(idAttribute));
}
Also used : ExpansionService(org.apache.beam.sdk.expansion.service.ExpansionService) ExternalTransforms(org.apache.beam.model.pipeline.v1.ExternalTransforms) ByteString(org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.ByteString) Pipeline(org.apache.beam.sdk.Pipeline) RunnerApi(org.apache.beam.model.pipeline.v1.RunnerApi) ExpansionApi(org.apache.beam.model.expansion.v1.ExpansionApi) ValueProvider(org.apache.beam.sdk.options.ValueProvider) Test(org.junit.Test)

Aggregations

Cell (com.google.bigtable.v2.Cell)1 Column (com.google.bigtable.v2.Column)1 Family (com.google.bigtable.v2.Family)1 MutateRowResponse (com.google.bigtable.v2.MutateRowResponse)1 Mutation (com.google.bigtable.v2.Mutation)1 SetCell (com.google.bigtable.v2.Mutation.SetCell)1 Row (com.google.bigtable.v2.Row)1 RowFilter (com.google.bigtable.v2.RowFilter)1 SampleRowKeysResponse (com.google.bigtable.v2.SampleRowKeysResponse)1 BigtableOptions (com.google.cloud.bigtable.config.BigtableOptions)1 BulkOptions (com.google.cloud.bigtable.config.BulkOptions)1 CredentialOptions (com.google.cloud.bigtable.config.CredentialOptions)1 CredentialType (com.google.cloud.bigtable.config.CredentialOptions.CredentialType)1 RetryOptions (com.google.cloud.bigtable.config.RetryOptions)1 ByteString (com.google.protobuf.ByteString)1 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 StandardCharsets (java.nio.charset.StandardCharsets)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1