Search in sources :

Example 1 with ListCoder

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

the class KryoCoderTest method testCodingWithKvCoderKeyIsKryoCoder.

@Test
public void testCodingWithKvCoderKeyIsKryoCoder() throws IOException {
    final KryoRegistrar registrar = k -> k.register(TestClass.class);
    final ListCoder<Void> listCoder = ListCoder.of(VoidCoder.of());
    final KvCoder<TestClass, List<Void>> kvCoder = KvCoder.of(KryoCoder.of(OPTIONS, registrar), listCoder);
    final List<Void> inputValue = new ArrayList<>();
    inputValue.add(null);
    inputValue.add(null);
    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    final TestClass inputKey = new TestClass("something");
    kvCoder.encode(KV.of(inputKey, inputValue), byteArrayOutputStream);
    final KV<TestClass, List<Void>> decoded = kvCoder.decode(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
    assertNotNull(decoded);
    assertNotNull(decoded.getKey());
    assertEquals(inputKey, decoded.getKey());
    assertNotNull(decoded.getValue());
    assertEquals(inputValue, decoded.getValue());
}
Also used : KvCoder(org.apache.beam.sdk.coders.KvCoder) KV(org.apache.beam.sdk.values.KV) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Assert.assertNotNull(org.junit.Assert.assertNotNull) ObjectInputStream(java.io.ObjectInputStream) ListCoder(org.apache.beam.sdk.coders.ListCoder) IOException(java.io.IOException) Test(org.junit.Test) PipelineOptionsFactory(org.apache.beam.sdk.options.PipelineOptionsFactory) ArrayList(java.util.ArrayList) StringUtf8Coder(org.apache.beam.sdk.coders.StringUtf8Coder) Objects(java.util.Objects) CoderException(org.apache.beam.sdk.coders.CoderException) List(java.util.List) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectOutputStream(java.io.ObjectOutputStream) VoidCoder(org.apache.beam.sdk.coders.VoidCoder) PipelineOptions(org.apache.beam.sdk.options.PipelineOptions) Assert.assertEquals(org.junit.Assert.assertEquals) Nullable(org.checkerframework.checker.nullness.qual.Nullable) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 2 with ListCoder

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

the class KryoCoderTest method testCodingWithKvCoderClassToBeEncoded.

@Test
public void testCodingWithKvCoderClassToBeEncoded() throws IOException {
    final KryoRegistrar registrar = k -> {
        k.register(TestClass.class);
        k.register(ClassToBeEncoded.class);
    };
    final ListCoder<Void> listCoder = ListCoder.of(VoidCoder.of());
    final KvCoder<ClassToBeEncoded, List<Void>> kvCoder = KvCoder.of(KryoCoder.of(OPTIONS, registrar), listCoder);
    final List<Void> inputValue = new ArrayList<>();
    inputValue.add(null);
    inputValue.add(null);
    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    final ClassToBeEncoded inputKey = new ClassToBeEncoded("something", 1, 0.2);
    kvCoder.encode(KV.of(inputKey, inputValue), byteArrayOutputStream);
    final KV<ClassToBeEncoded, List<Void>> decoded = kvCoder.decode(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
    assertNotNull(decoded);
    assertNotNull(decoded.getKey());
    assertEquals(inputKey, decoded.getKey());
    assertNotNull(decoded.getValue());
    assertEquals(inputValue, decoded.getValue());
}
Also used : KvCoder(org.apache.beam.sdk.coders.KvCoder) KV(org.apache.beam.sdk.values.KV) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Assert.assertNotNull(org.junit.Assert.assertNotNull) ObjectInputStream(java.io.ObjectInputStream) ListCoder(org.apache.beam.sdk.coders.ListCoder) IOException(java.io.IOException) Test(org.junit.Test) PipelineOptionsFactory(org.apache.beam.sdk.options.PipelineOptionsFactory) ArrayList(java.util.ArrayList) StringUtf8Coder(org.apache.beam.sdk.coders.StringUtf8Coder) Objects(java.util.Objects) CoderException(org.apache.beam.sdk.coders.CoderException) List(java.util.List) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectOutputStream(java.io.ObjectOutputStream) VoidCoder(org.apache.beam.sdk.coders.VoidCoder) PipelineOptions(org.apache.beam.sdk.options.PipelineOptions) Assert.assertEquals(org.junit.Assert.assertEquals) Nullable(org.checkerframework.checker.nullness.qual.Nullable) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 ObjectInputStream (java.io.ObjectInputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Objects (java.util.Objects)2 CoderException (org.apache.beam.sdk.coders.CoderException)2 KvCoder (org.apache.beam.sdk.coders.KvCoder)2 ListCoder (org.apache.beam.sdk.coders.ListCoder)2 StringUtf8Coder (org.apache.beam.sdk.coders.StringUtf8Coder)2 VoidCoder (org.apache.beam.sdk.coders.VoidCoder)2 PipelineOptions (org.apache.beam.sdk.options.PipelineOptions)2 PipelineOptionsFactory (org.apache.beam.sdk.options.PipelineOptionsFactory)2 KV (org.apache.beam.sdk.values.KV)2 Nullable (org.checkerframework.checker.nullness.qual.Nullable)2 Assert.assertEquals (org.junit.Assert.assertEquals)2 Assert.assertNotNull (org.junit.Assert.assertNotNull)2 Test (org.junit.Test)2