Search in sources :

Example 1 with CoderRegistry

use of org.apache.beam.sdk.coders.CoderRegistry 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 CoderRegistry

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

the class DoFnInvokersTest method testSplittableDoFnDefaultMethods.

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

        @ProcessElement
        public void processElement(ProcessContext c, DefaultTracker tracker) {
        }

        @GetInitialRestriction
        public RestrictionWithDefaultTracker getInitialRestriction(String element) {
            return null;
        }
    }
    MockFn fn = mock(MockFn.class);
    DoFnInvoker<String, String> invoker = DoFnInvokers.invokerFor(fn);
    CoderRegistry coderRegistry = CoderRegistry.createDefault();
    coderRegistry.registerCoderProvider(CoderProviders.fromStaticMethods(RestrictionWithDefaultTracker.class, CoderForDefaultTracker.class));
    assertThat(invoker.<RestrictionWithDefaultTracker>invokeGetRestrictionCoder(coderRegistry), instanceOf(CoderForDefaultTracker.class));
    invoker.invokeSplitRestriction("blah", "foo", new DoFn.OutputReceiver<String>() {

        private boolean invoked;

        @Override
        public void output(String output) {
            assertFalse(invoked);
            invoked = true;
            assertEquals("foo", output);
        }
    });
    invoker.invokeProcessElement(mockArgumentProvider);
    assertThat(invoker.invokeNewTracker(new RestrictionWithDefaultTracker()), instanceOf(DefaultTracker.class));
}
Also used : CoderRegistry(org.apache.beam.sdk.coders.CoderRegistry) DoFn(org.apache.beam.sdk.transforms.DoFn) HasDefaultTracker(org.apache.beam.sdk.transforms.splittabledofn.HasDefaultTracker) Test(org.junit.Test)

Example 3 with CoderRegistry

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

the class WithKeys method expand.

@Override
public PCollection<KV<K, V>> expand(PCollection<V> in) {
    PCollection<KV<K, V>> result = in.apply("AddKeys", MapElements.via(new SimpleFunction<V, KV<K, V>>() {

        @Override
        public KV<K, V> apply(V element) {
            return KV.of(fn.apply(element), element);
        }
    }));
    try {
        Coder<K> keyCoder;
        CoderRegistry coderRegistry = in.getPipeline().getCoderRegistry();
        if (keyType == null) {
            keyCoder = coderRegistry.getOutputCoder(fn, in.getCoder());
        } else {
            keyCoder = coderRegistry.getCoder(keyType);
        }
        // TODO: Remove when we can set the coder inference context.
        result.setCoder(KvCoder.of(keyCoder, in.getCoder()));
    } catch (CannotProvideCoderException exc) {
    // let lazy coder inference have a try
    }
    return result;
}
Also used : CoderRegistry(org.apache.beam.sdk.coders.CoderRegistry) CannotProvideCoderException(org.apache.beam.sdk.coders.CannotProvideCoderException) KV(org.apache.beam.sdk.values.KV) KV(org.apache.beam.sdk.values.KV)

Example 4 with CoderRegistry

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

the class ConfluentSchemaRegistryDeserializerProviderTest method testGetCoder.

@Test
public void testGetCoder() {
    String schemaRegistryUrl = "mock://my-scope-name";
    String subject = "mytopic";
    SchemaRegistryClient mockRegistryClient = mockSchemaRegistryClient(schemaRegistryUrl, subject);
    CoderRegistry coderRegistry = CoderRegistry.createDefault();
    AvroCoder coderV0 = (AvroCoder) mockDeserializerProvider(schemaRegistryUrl, subject, null).getCoder(coderRegistry);
    assertEquals(AVRO_SCHEMA, coderV0.getSchema());
    try {
        Integer version = mockRegistryClient.getVersion(subject, AVRO_SCHEMA_V1);
        AvroCoder coderV1 = (AvroCoder) mockDeserializerProvider(schemaRegistryUrl, subject, version).getCoder(coderRegistry);
        assertEquals(AVRO_SCHEMA_V1, coderV1.getSchema());
    } catch (IOException | RestClientException e) {
        throw new RuntimeException("Unable to register schema for subject: " + subject, e);
    }
}
Also used : CoderRegistry(org.apache.beam.sdk.coders.CoderRegistry) RestClientException(io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException) IOException(java.io.IOException) AvroCoder(org.apache.beam.sdk.coders.AvroCoder) SchemaRegistryClient(io.confluent.kafka.schemaregistry.client.SchemaRegistryClient) Test(org.junit.Test)

Example 5 with CoderRegistry

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

the class LocalDeserializerProviderTest method testInferKeyCoderFailure.

@Test
public void testInferKeyCoderFailure() throws Exception {
    cannotInferException.expect(RuntimeException.class);
    CoderRegistry registry = CoderRegistry.createDefault();
    LocalDeserializerProvider.of(NonInferableObjectDeserializer.class).getCoder(registry);
}
Also used : CoderRegistry(org.apache.beam.sdk.coders.CoderRegistry) 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