Search in sources :

Example 41 with TupleTag

use of org.apache.beam.sdk.values.TupleTag in project beam by apache.

the class BeamSqlDslUdfUdafTest method testSerializableFunctionUdf.

/**
 * Test UDF implementing {@link SerializableFunction}.
 */
@Test
public void testSerializableFunctionUdf() throws Exception {
    Schema resultType = Schema.builder().addInt32Field("f_int").addInt32Field("cubicvalue").build();
    Row row = Row.withSchema(resultType).addValues(2, 8).build();
    String sql = "SELECT f_int, cubic(f_int) as cubicvalue FROM PCOLLECTION WHERE f_int = 2";
    PCollection<Row> result = PCollectionTuple.of(new TupleTag<>("PCOLLECTION"), boundedInput1).apply("testUdf", SqlTransform.query(sql).registerUdf("cubic", new CubicIntegerFn()));
    PAssert.that(result).containsInAnyOrder(row);
    pipeline.run().waitUntilFinish();
}
Also used : Schema(org.apache.beam.sdk.schemas.Schema) TupleTag(org.apache.beam.sdk.values.TupleTag) Row(org.apache.beam.sdk.values.Row) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 42 with TupleTag

use of org.apache.beam.sdk.values.TupleTag in project beam by apache.

the class BeamSqlDslProjectTest method testProjectUnknownField.

@Test
public void testProjectUnknownField() throws Exception {
    exceptions.expect(ParseException.class);
    exceptions.expectCause(hasMessage(containsString("Column 'f_int_na' not found in any table")));
    pipeline.enableAbandonedNodeEnforcement(false);
    String sql = "SELECT f_int_na FROM TABLE_A";
    PCollectionTuple.of(new TupleTag<>("TABLE_A"), boundedInput1).apply("testProjectUnknownField", SqlTransform.query(sql));
    pipeline.run();
}
Also used : TupleTag(org.apache.beam.sdk.values.TupleTag) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 43 with TupleTag

use of org.apache.beam.sdk.values.TupleTag in project beam by apache.

the class BeamSqlDslProjectTest method runPartialFields.

private void runPartialFields(PCollection<Row> input) throws Exception {
    String sql = "SELECT f_int, f_long FROM TABLE_A";
    PCollection<Row> result = PCollectionTuple.of(new TupleTag<>("TABLE_A"), input).apply("testPartialFields", SqlTransform.query(sql));
    Schema resultType = Schema.builder().addInt32Field("f_int").addInt64Field("f_long").build();
    Row row = rowAtIndex(resultType, 0);
    PAssert.that(result).containsInAnyOrder(row);
    pipeline.run();
}
Also used : Schema(org.apache.beam.sdk.schemas.Schema) TupleTag(org.apache.beam.sdk.values.TupleTag) Matchers.containsString(org.hamcrest.Matchers.containsString) Row(org.apache.beam.sdk.values.Row)

Example 44 with TupleTag

use of org.apache.beam.sdk.values.TupleTag in project beam by apache.

the class BeamSqlDslUdfUdafTest method testBeamSqlUdfWithDefaultParameters.

/**
 * Test UDF implementing {@link BeamSqlUdf} with default parameters.
 */
@Test
public void testBeamSqlUdfWithDefaultParameters() throws Exception {
    String sql = "SELECT f_int, substr(f_string) as sub_string FROM PCOLLECTION WHERE f_int = 2";
    PCollection<Row> result = PCollectionTuple.of(new TupleTag<>("PCOLLECTION"), boundedInput1).apply("testUdf", SqlTransform.query(sql).registerUdf("substr", UdfFnWithDefault.class));
    Schema subStrSchema = Schema.builder().addInt32Field("f_int").addStringField("sub_string").build();
    Row subStrRow = Row.withSchema(subStrSchema).addValues(2, "s").build();
    PAssert.that(result).containsInAnyOrder(subStrRow);
    pipeline.run().waitUntilFinish();
}
Also used : Schema(org.apache.beam.sdk.schemas.Schema) TupleTag(org.apache.beam.sdk.values.TupleTag) Matchers.containsString(org.hamcrest.Matchers.containsString) Row(org.apache.beam.sdk.values.Row) Test(org.junit.Test)

Example 45 with TupleTag

use of org.apache.beam.sdk.values.TupleTag in project beam by apache.

the class PCollectionOutputTagVisitor method visitValue.

@Override
public void visitValue(PValue value, Node producer) {
    for (Entry<ProjectionProducer<PTransform<?, ?>>, Map<PCollection<?>, FieldAccessDescriptor>> entry : pCollFieldAccess.entrySet()) {
        FieldAccessDescriptor fieldAccess = entry.getValue().get(value);
        if (fieldAccess == null) {
            continue;
        }
        BiMap<PCollection<?>, TupleTag<?>> outputs = ImmutableBiMap.copyOf(producer.getOutputs()).inverse();
        TupleTag<?> tag = outputs.get(value);
        Preconditions.checkArgumentNotNull(tag, "PCollection %s not found in outputs of producer %s", value, producer);
        ImmutableMap.Builder<TupleTag<?>, FieldAccessDescriptor> tagEntryBuilder = tagFieldAccess.build().get(entry.getKey());
        if (tagEntryBuilder == null) {
            tagEntryBuilder = ImmutableMap.builder();
            tagFieldAccess.put(entry.getKey(), tagEntryBuilder);
        }
        tagEntryBuilder.put(tag, fieldAccess);
    }
}
Also used : FieldAccessDescriptor(org.apache.beam.sdk.schemas.FieldAccessDescriptor) PCollection(org.apache.beam.sdk.values.PCollection) ProjectionProducer(org.apache.beam.sdk.schemas.ProjectionProducer) TupleTag(org.apache.beam.sdk.values.TupleTag) ImmutableMap(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap) ImmutableBiMap(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableBiMap) Map(java.util.Map) BiMap(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.BiMap) ImmutableMap(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap)

Aggregations

TupleTag (org.apache.beam.sdk.values.TupleTag)185 Test (org.junit.Test)100 WindowedValue (org.apache.beam.sdk.util.WindowedValue)54 KV (org.apache.beam.sdk.values.KV)54 PCollectionTuple (org.apache.beam.sdk.values.PCollectionTuple)49 PCollection (org.apache.beam.sdk.values.PCollection)42 DoFn (org.apache.beam.sdk.transforms.DoFn)32 Instant (org.joda.time.Instant)32 SerializablePipelineOptions (org.apache.beam.runners.core.construction.SerializablePipelineOptions)30 Map (java.util.Map)29 Pipeline (org.apache.beam.sdk.Pipeline)29 PCollectionView (org.apache.beam.sdk.values.PCollectionView)29 HashMap (java.util.HashMap)27 Coder (org.apache.beam.sdk.coders.Coder)26 StreamRecordStripper.stripStreamRecordFromWindowedValue (org.apache.beam.runners.flink.translation.wrappers.streaming.StreamRecordStripper.stripStreamRecordFromWindowedValue)25 Matchers.containsString (org.hamcrest.Matchers.containsString)25 List (java.util.List)24 BoundedWindow (org.apache.beam.sdk.transforms.windowing.BoundedWindow)23 KvCoder (org.apache.beam.sdk.coders.KvCoder)22 KeyedOneInputStreamOperatorTestHarness (org.apache.flink.streaming.util.KeyedOneInputStreamOperatorTestHarness)22