Search in sources :

Example 1 with InstantCoder

use of org.apache.beam.sdk.coders.InstantCoder in project beam by apache.

the class KafkaIOTest method testInferKeyCoder.

@Test
public void testInferKeyCoder() {
    CoderRegistry registry = CoderRegistry.createDefault();
    assertTrue(KafkaIO.inferCoder(registry, LongDeserializer.class).getValueCoder() instanceof VarLongCoder);
    assertTrue(KafkaIO.inferCoder(registry, StringDeserializer.class).getValueCoder() instanceof StringUtf8Coder);
    assertTrue(KafkaIO.inferCoder(registry, InstantDeserializer.class).getValueCoder() instanceof InstantCoder);
    assertTrue(KafkaIO.inferCoder(registry, DeserializerWithInterfaces.class).getValueCoder() instanceof VarLongCoder);
}
Also used : CoderRegistry(org.apache.beam.sdk.coders.CoderRegistry) VarLongCoder(org.apache.beam.sdk.coders.VarLongCoder) LongDeserializer(org.apache.kafka.common.serialization.LongDeserializer) InstantDeserializer(org.apache.beam.sdk.io.kafka.serialization.InstantDeserializer) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) InstantCoder(org.apache.beam.sdk.coders.InstantCoder) StringUtf8Coder(org.apache.beam.sdk.coders.StringUtf8Coder) Test(org.junit.Test)

Example 2 with InstantCoder

use of org.apache.beam.sdk.coders.InstantCoder in project beam by apache.

the class DoFnInvokersTest method testSplittableDoFnWithAllMethods.

@Test
public void testSplittableDoFnWithAllMethods() throws Exception {
    MockFn fn = mock(MockFn.class);
    DoFnInvoker<String, String> invoker = DoFnInvokers.invokerFor(fn);
    final SomeRestrictionTracker tracker = mock(SomeRestrictionTracker.class);
    final SomeRestrictionCoder coder = mock(SomeRestrictionCoder.class);
    final InstantCoder watermarkEstimatorStateCoder = InstantCoder.of();
    final Instant watermarkEstimatorState = Instant.now();
    final WatermarkEstimator<Instant> watermarkEstimator = new WatermarkEstimators.Manual(watermarkEstimatorState);
    SomeRestriction restriction = new SomeRestriction();
    final SomeRestriction part1 = new SomeRestriction();
    final SomeRestriction part2 = new SomeRestriction();
    final SomeRestriction part3 = new SomeRestriction();
    when(fn.getRestrictionCoder()).thenReturn(coder);
    when(fn.getWatermarkEstimatorStateCoder()).thenReturn(watermarkEstimatorStateCoder);
    when(fn.getInitialRestriction(mockElement)).thenReturn(restriction);
    doAnswer(AdditionalAnswers.delegatesTo(new MockFn() {

        @DoFn.SplitRestriction
        @Override
        public void splitRestriction(@Element String element, @Restriction SomeRestriction restriction, DoFn.OutputReceiver<SomeRestriction> receiver) {
            receiver.output(part1);
            receiver.output(part2);
            receiver.output(part3);
        }
    })).when(fn).splitRestriction(eq(mockElement), same(restriction), any());
    when(fn.getInitialWatermarkEstimatorState()).thenReturn(watermarkEstimatorState);
    when(fn.newTracker(restriction)).thenReturn(tracker);
    when(fn.newWatermarkEstimator(watermarkEstimatorState)).thenReturn(watermarkEstimator);
    when(fn.processElement(mockProcessContext, tracker, watermarkEstimator)).thenReturn(resume());
    when(fn.getSize()).thenReturn(2.0);
    assertEquals(coder, invoker.invokeGetRestrictionCoder(CoderRegistry.createDefault()));
    assertEquals(watermarkEstimatorStateCoder, invoker.invokeGetWatermarkEstimatorStateCoder(CoderRegistry.createDefault()));
    assertEquals(restriction, invoker.invokeGetInitialRestriction(new FakeArgumentProvider<String, String>() {

        @Override
        public String element(DoFn<String, String> doFn) {
            return mockElement;
        }
    }));
    List<SomeRestriction> outputs = new ArrayList<>();
    invoker.invokeSplitRestriction(new FakeArgumentProvider<String, String>() {

        @Override
        public String element(DoFn<String, String> doFn) {
            return mockElement;
        }

        @Override
        public Object restriction() {
            return restriction;
        }

        @Override
        public OutputReceiver outputReceiver(DoFn doFn) {
            return new OutputReceiver<SomeRestriction>() {

                @Override
                public void output(SomeRestriction output) {
                    outputs.add(output);
                }

                @Override
                public void outputWithTimestamp(SomeRestriction output, Instant timestamp) {
                    fail("Unexpected output with timestamp");
                }
            };
        }
    });
    assertEquals(Arrays.asList(part1, part2, part3), outputs);
    assertEquals(watermarkEstimatorState, invoker.invokeGetInitialWatermarkEstimatorState(new FakeArgumentProvider<>()));
    assertEquals(tracker, invoker.invokeNewTracker(new FakeArgumentProvider<String, String>() {

        @Override
        public String element(DoFn<String, String> doFn) {
            return mockElement;
        }

        @Override
        public Object restriction() {
            return restriction;
        }
    }));
    assertEquals(watermarkEstimator, invoker.invokeNewWatermarkEstimator(new FakeArgumentProvider<String, String>() {

        @Override
        public Object watermarkEstimatorState() {
            return watermarkEstimatorState;
        }
    }));
    assertEquals(resume(), invoker.invokeProcessElement(new FakeArgumentProvider<String, String>() {

        @Override
        public DoFn<String, String>.ProcessContext processContext(DoFn<String, String> fn) {
            return mockProcessContext;
        }

        @Override
        public RestrictionTracker<?, ?> restrictionTracker() {
            return tracker;
        }

        @Override
        public WatermarkEstimator<?> watermarkEstimator() {
            return watermarkEstimator;
        }
    }));
    assertEquals(2.0, invoker.invokeGetSize(mockArgumentProvider), 0.0001);
}
Also used : InstantCoder(org.apache.beam.sdk.coders.InstantCoder) Instant(org.joda.time.Instant) ArrayList(java.util.ArrayList) OutputReceiver(org.apache.beam.sdk.transforms.DoFn.OutputReceiver) MultiOutputReceiver(org.apache.beam.sdk.transforms.DoFn.MultiOutputReceiver) DoFn(org.apache.beam.sdk.transforms.DoFn) FakeArgumentProvider(org.apache.beam.sdk.transforms.reflect.DoFnInvoker.FakeArgumentProvider) Test(org.junit.Test)

Example 3 with InstantCoder

use of org.apache.beam.sdk.coders.InstantCoder in project beam by apache.

the class LocalDeserializerProviderTest method testInferKeyCoder.

@Test
public void testInferKeyCoder() {
    CoderRegistry registry = CoderRegistry.createDefault();
    assertTrue(LocalDeserializerProvider.of(LongDeserializer.class).getCoder(registry).getValueCoder() instanceof VarLongCoder);
    assertTrue(LocalDeserializerProvider.of(StringDeserializer.class).getCoder(registry).getValueCoder() instanceof StringUtf8Coder);
    assertTrue(LocalDeserializerProvider.of(InstantDeserializer.class).getCoder(registry).getValueCoder() instanceof InstantCoder);
    assertTrue(LocalDeserializerProvider.of(DeserializerWithInterfaces.class).getCoder(registry).getValueCoder() instanceof VarLongCoder);
}
Also used : CoderRegistry(org.apache.beam.sdk.coders.CoderRegistry) VarLongCoder(org.apache.beam.sdk.coders.VarLongCoder) InstantCoder(org.apache.beam.sdk.coders.InstantCoder) StringUtf8Coder(org.apache.beam.sdk.coders.StringUtf8Coder) Test(org.junit.Test)

Aggregations

InstantCoder (org.apache.beam.sdk.coders.InstantCoder)3 Test (org.junit.Test)3 CoderRegistry (org.apache.beam.sdk.coders.CoderRegistry)2 StringUtf8Coder (org.apache.beam.sdk.coders.StringUtf8Coder)2 VarLongCoder (org.apache.beam.sdk.coders.VarLongCoder)2 ArrayList (java.util.ArrayList)1 InstantDeserializer (org.apache.beam.sdk.io.kafka.serialization.InstantDeserializer)1 DoFn (org.apache.beam.sdk.transforms.DoFn)1 MultiOutputReceiver (org.apache.beam.sdk.transforms.DoFn.MultiOutputReceiver)1 OutputReceiver (org.apache.beam.sdk.transforms.DoFn.OutputReceiver)1 FakeArgumentProvider (org.apache.beam.sdk.transforms.reflect.DoFnInvoker.FakeArgumentProvider)1 LongDeserializer (org.apache.kafka.common.serialization.LongDeserializer)1 StringDeserializer (org.apache.kafka.common.serialization.StringDeserializer)1 Instant (org.joda.time.Instant)1