Search in sources :

Example 21 with CoderRegistry

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

the class CopyOnAccessInMemoryStateInternalsTest method testAccumulatorCombiningStateWithUnderlying.

@Test
public void testAccumulatorCombiningStateWithUnderlying() throws CannotProvideCoderException {
    CopyOnAccessInMemoryStateInternals<String> underlying = CopyOnAccessInMemoryStateInternals.withUnderlying(key, null);
    CombineFn<Long, long[], Long> sumLongFn = Sum.ofLongs();
    StateNamespace namespace = new StateNamespaceForTest("foo");
    CoderRegistry reg = pipeline.getCoderRegistry();
    StateTag<CombiningState<Long, long[], Long>> stateTag = StateTags.combiningValue("summer", sumLongFn.getAccumulatorCoder(reg, reg.getCoder(Long.class)), sumLongFn);
    GroupingState<Long, Long> underlyingValue = underlying.state(namespace, stateTag);
    assertThat(underlyingValue.read(), equalTo(0L));
    underlyingValue.add(1L);
    assertThat(underlyingValue.read(), equalTo(1L));
    CopyOnAccessInMemoryStateInternals<String> internals = CopyOnAccessInMemoryStateInternals.withUnderlying(key, underlying);
    GroupingState<Long, Long> copyOnAccessState = internals.state(namespace, stateTag);
    assertThat(copyOnAccessState.read(), equalTo(1L));
    copyOnAccessState.add(4L);
    assertThat(copyOnAccessState.read(), equalTo(5L));
    assertThat(underlyingValue.read(), equalTo(1L));
    GroupingState<Long, Long> reReadUnderlyingValue = underlying.state(namespace, stateTag);
    assertThat(underlyingValue.read(), equalTo(reReadUnderlyingValue.read()));
}
Also used : CoderRegistry(org.apache.beam.sdk.coders.CoderRegistry) CombiningState(org.apache.beam.sdk.state.CombiningState) StateNamespaceForTest(org.apache.beam.runners.core.StateNamespaceForTest) StateNamespace(org.apache.beam.runners.core.StateNamespace) StateNamespaceForTest(org.apache.beam.runners.core.StateNamespaceForTest) Test(org.junit.Test)

Example 22 with CoderRegistry

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

the class CombineValuesFnFactoryTest method testCombineValuesFnCoders.

@Test
public void testCombineValuesFnCoders() throws Exception {
    CoderRegistry registry = CoderRegistry.createDefault();
    MeanInts meanInts = new MeanInts();
    CountSum countSum = new CountSum(6, 27);
    Coder<CountSum> coder = meanInts.getAccumulatorCoder(registry, registry.getCoder(TypeDescriptor.of(Integer.class)));
    assertEquals(countSum, CoderUtils.decodeFromByteArray(coder, CoderUtils.encodeToByteArray(coder, countSum)));
}
Also used : CoderRegistry(org.apache.beam.sdk.coders.CoderRegistry) Test(org.junit.Test)

Example 23 with CoderRegistry

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

the class StreamingKeyedWorkItemSideInputDoFnRunnerTest method createRunner.

@SuppressWarnings("unchecked")
private StreamingKeyedWorkItemSideInputDoFnRunner<String, Integer, KV<String, Integer>, IntervalWindow> createRunner(DoFnRunners.OutputManager outputManager) throws Exception {
    CoderRegistry registry = CoderRegistry.createDefault();
    Coder<String> keyCoder = StringUtf8Coder.of();
    Coder<Integer> inputCoder = BigEndianIntegerCoder.of();
    AppliedCombineFn<String, Integer, ?, Integer> combineFn = AppliedCombineFn.withInputCoder(Sum.ofIntegers(), registry, KvCoder.of(keyCoder, inputCoder));
    WindowingStrategy<Object, IntervalWindow> windowingStrategy = WindowingStrategy.of(WINDOW_FN);
    @SuppressWarnings("rawtypes") StreamingGroupAlsoByWindowViaWindowSetFn doFn = (StreamingGroupAlsoByWindowViaWindowSetFn) StreamingGroupAlsoByWindowsDoFns.create(windowingStrategy, key -> state, combineFn, keyCoder);
    DoFnRunner<KeyedWorkItem<String, Integer>, KV<String, Integer>> simpleDoFnRunner = new GroupAlsoByWindowFnRunner<>(PipelineOptionsFactory.create(), doFn.asDoFn(), mockSideInputReader, outputManager, mainOutputTag, stepContext);
    return new StreamingKeyedWorkItemSideInputDoFnRunner<String, Integer, KV<String, Integer>, IntervalWindow>(simpleDoFnRunner, keyCoder, sideInputFetcher, stepContext);
}
Also used : Arrays(java.util.Arrays) KV(org.apache.beam.sdk.values.KV) CoderRegistry(org.apache.beam.sdk.coders.CoderRegistry) KeyedWorkItems(org.apache.beam.runners.core.KeyedWorkItems) WindowedValue(org.apache.beam.sdk.util.WindowedValue) InMemoryStateInternals(org.apache.beam.runners.core.InMemoryStateInternals) Matchers(org.mockito.Matchers) Mock(org.mockito.Mock) DoFnRunner(org.apache.beam.runners.core.DoFnRunner) Duration(org.joda.time.Duration) RunWith(org.junit.runner.RunWith) Coder(org.apache.beam.sdk.coders.Coder) StateNamespaces(org.apache.beam.runners.core.StateNamespaces) TimerInternals(org.apache.beam.runners.core.TimerInternals) PipelineOptionsFactory(org.apache.beam.sdk.options.PipelineOptionsFactory) DoFnRunners(org.apache.beam.runners.core.DoFnRunners) StringUtf8Coder(org.apache.beam.sdk.coders.StringUtf8Coder) ImmutableSet(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableSet) MockitoAnnotations(org.mockito.MockitoAnnotations) ListOutputManager(org.apache.beam.runners.dataflow.worker.util.ListOutputManager) TupleTag(org.apache.beam.sdk.values.TupleTag) StateInternals(org.apache.beam.runners.core.StateInternals) TimerData(org.apache.beam.runners.core.TimerInternals.TimerData) KeyedWorkItem(org.apache.beam.runners.core.KeyedWorkItem) SideInputReader(org.apache.beam.runners.core.SideInputReader) Before(org.junit.Before) Windmill(org.apache.beam.runners.dataflow.worker.windmill.Windmill) KvCoder(org.apache.beam.sdk.coders.KvCoder) AppliedCombineFn(org.apache.beam.sdk.util.AppliedCombineFn) Timer(org.apache.beam.runners.dataflow.worker.windmill.Windmill.Timer) PaneInfo(org.apache.beam.sdk.transforms.windowing.PaneInfo) BigEndianIntegerCoder(org.apache.beam.sdk.coders.BigEndianIntegerCoder) Set(java.util.Set) Sum(org.apache.beam.sdk.transforms.Sum) FixedWindows(org.apache.beam.sdk.transforms.windowing.FixedWindows) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) JUnit4(org.junit.runners.JUnit4) List(java.util.List) BagState(org.apache.beam.sdk.state.BagState) Instant(org.joda.time.Instant) ImmutableList(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList) IntervalWindow(org.apache.beam.sdk.transforms.windowing.IntervalWindow) TimeDomain(org.apache.beam.sdk.state.TimeDomain) Assert.assertEquals(org.junit.Assert.assertEquals) ValueInEmptyWindows(org.apache.beam.runners.dataflow.worker.util.ValueInEmptyWindows) WindowingStrategy(org.apache.beam.sdk.values.WindowingStrategy) KV(org.apache.beam.sdk.values.KV) KeyedWorkItem(org.apache.beam.runners.core.KeyedWorkItem) CoderRegistry(org.apache.beam.sdk.coders.CoderRegistry) IntervalWindow(org.apache.beam.sdk.transforms.windowing.IntervalWindow)

Example 24 with CoderRegistry

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

the class DoFnInvokersTest method testSplittableDoFnWithHasDefaultMethods.

@Test
public void testSplittableDoFnWithHasDefaultMethods() throws Exception {
    class MockFn extends DoFn<String, String> {

        @ProcessElement
        public void processElement(ProcessContext c, RestrictionTracker<RestrictionWithBoundedDefaultTracker, Void> tracker, WatermarkEstimator<WatermarkEstimatorStateWithDefaultWatermarkEstimator> watermarkEstimator) {
        }

        @GetInitialRestriction
        public RestrictionWithBoundedDefaultTracker getInitialRestriction(@Element String element) {
            return null;
        }

        @GetInitialWatermarkEstimatorState
        public WatermarkEstimatorStateWithDefaultWatermarkEstimator getInitialWatermarkEstimatorState() {
            return null;
        }
    }
    MockFn fn = mock(MockFn.class);
    DoFnInvoker<String, String> invoker = DoFnInvokers.invokerFor(fn);
    CoderRegistry coderRegistry = CoderRegistry.createDefault();
    coderRegistry.registerCoderProvider(CoderProviders.fromStaticMethods(RestrictionWithBoundedDefaultTracker.class, CoderForDefaultTracker.class));
    coderRegistry.registerCoderForClass(WatermarkEstimatorStateWithDefaultWatermarkEstimator.class, new CoderForWatermarkEstimatorStateWithDefaultWatermarkEstimator());
    assertThat(invoker.<RestrictionWithBoundedDefaultTracker>invokeGetRestrictionCoder(coderRegistry), instanceOf(CoderForDefaultTracker.class));
    assertThat(invoker.invokeGetWatermarkEstimatorStateCoder(coderRegistry), instanceOf(CoderForWatermarkEstimatorStateWithDefaultWatermarkEstimator.class));
    invoker.invokeSplitRestriction(new FakeArgumentProvider<String, String>() {

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

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

        @Override
        public OutputReceiver<String> outputReceiver(DoFn<String, String> doFn) {
            return new DoFn.OutputReceiver<String>() {

                private boolean invoked;

                @Override
                public void output(String output) {
                    assertFalse(invoked);
                    invoked = true;
                    assertEquals("foo", output);
                }

                @Override
                public void outputWithTimestamp(String output, Instant instant) {
                    assertFalse(invoked);
                    invoked = true;
                    assertEquals("foo", output);
                }
            };
        }
    });
    assertEquals(stop(), invoker.invokeProcessElement(mockArgumentProvider));
    assertThat(invoker.invokeNewTracker(new FakeArgumentProvider<String, String>() {

        @Override
        public Object restriction() {
            return new RestrictionWithBoundedDefaultTracker();
        }
    }), instanceOf(BoundedDefaultTracker.class));
    assertThat(invoker.invokeNewWatermarkEstimator(new FakeArgumentProvider<String, String>() {

        @Override
        public Object watermarkEstimatorState() {
            return new WatermarkEstimatorStateWithDefaultWatermarkEstimator();
        }
    }), instanceOf(DefaultWatermarkEstimator.class));
}
Also used : RestrictionTracker(org.apache.beam.sdk.transforms.splittabledofn.RestrictionTracker) Instant(org.joda.time.Instant) OutputReceiver(org.apache.beam.sdk.transforms.DoFn.OutputReceiver) MultiOutputReceiver(org.apache.beam.sdk.transforms.DoFn.MultiOutputReceiver) CoderRegistry(org.apache.beam.sdk.coders.CoderRegistry) DoFn(org.apache.beam.sdk.transforms.DoFn) HasDefaultWatermarkEstimator(org.apache.beam.sdk.transforms.splittabledofn.HasDefaultWatermarkEstimator) WatermarkEstimator(org.apache.beam.sdk.transforms.splittabledofn.WatermarkEstimator) FakeArgumentProvider(org.apache.beam.sdk.transforms.reflect.DoFnInvoker.FakeArgumentProvider) HasDefaultWatermarkEstimator(org.apache.beam.sdk.transforms.splittabledofn.HasDefaultWatermarkEstimator) Test(org.junit.Test)

Aggregations

CoderRegistry (org.apache.beam.sdk.coders.CoderRegistry)24 Test (org.junit.Test)16 DoFn (org.apache.beam.sdk.transforms.DoFn)5 FakeArgumentProvider (org.apache.beam.sdk.transforms.reflect.DoFnInvoker.FakeArgumentProvider)4 StringUtf8Coder (org.apache.beam.sdk.coders.StringUtf8Coder)3 RestrictionTracker (org.apache.beam.sdk.transforms.splittabledofn.RestrictionTracker)3 IOException (java.io.IOException)2 TokenizationFileSystemIO (org.apache.beam.examples.complete.datatokenization.transforms.io.TokenizationFileSystemIO)2 SchemasUtils (org.apache.beam.examples.complete.datatokenization.utils.SchemasUtils)2 KeyedWorkItem (org.apache.beam.runners.core.KeyedWorkItem)2 ListOutputManager (org.apache.beam.runners.dataflow.worker.util.ListOutputManager)2 InstantCoder (org.apache.beam.sdk.coders.InstantCoder)2 VarLongCoder (org.apache.beam.sdk.coders.VarLongCoder)2 HasDefaultWatermarkEstimator (org.apache.beam.sdk.transforms.splittabledofn.HasDefaultWatermarkEstimator)2 WatermarkEstimator (org.apache.beam.sdk.transforms.splittabledofn.WatermarkEstimator)2 KV (org.apache.beam.sdk.values.KV)2 Instant (org.joda.time.Instant)2 SchemaRegistryClient (io.confluent.kafka.schemaregistry.client.SchemaRegistryClient)1 RestClientException (io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException)1 Arrays (java.util.Arrays)1