Search in sources :

Example 1 with Context

use of org.apache.beam.sdk.coders.Coder.Context in project beam by apache.

the class CommonCoderTest method executeSingleTest.

@Test
public void executeSingleTest() throws IOException {
    assertCoderIsKnown(testSpec.getCoder());
    Coder coder = instantiateCoder(testSpec.getCoder());
    Object testValue = convertValue(testSpec.getValue(), testSpec.getCoder(), coder);
    Context context = testSpec.getNested() ? Context.NESTED : Context.OUTER;
    byte[] encoded = CoderUtils.encodeToByteArray(coder, testValue, context);
    Object decodedValue = CoderUtils.decodeFromByteArray(coder, testSpec.getSerialized(), context);
    if (!testSpec.getCoder().getNonDeterministic()) {
        assertThat(testSpec.toString(), encoded, equalTo(testSpec.getSerialized()));
    }
    verifyDecodedValue(testSpec.getCoder(), decodedValue, testValue);
}
Also used : Context(org.apache.beam.sdk.coders.Coder.Context) IntervalWindowCoder(org.apache.beam.sdk.transforms.windowing.IntervalWindow.IntervalWindowCoder) Test(org.junit.Test)

Example 2 with Context

use of org.apache.beam.sdk.coders.Coder.Context in project beam by apache.

the class CommonCoderTest method executeSingleTest.

@Test
public void executeSingleTest() throws IOException {
    assertCoderIsKnown(testSpec.getCoder());
    Coder coder = instantiateCoder(testSpec.getCoder());
    Object testValue = convertValue(testSpec.getValue(), testSpec.getCoder(), coder);
    Context context = testSpec.getNested() ? Context.NESTED : Context.OUTER;
    byte[] encoded = CoderUtils.encodeToByteArray(coder, testValue, context);
    Object decodedValue = CoderUtils.decodeFromByteArray(coder, testSpec.getSerialized(), context);
    if (!testSpec.getCoder().getNonDeterministic()) {
        assertThat(testSpec.toString(), encoded, equalTo(testSpec.getSerialized()));
    }
    verifyDecodedValue(testSpec.getCoder(), decodedValue, testValue);
}
Also used : Context(org.apache.beam.sdk.coders.Coder.Context) TranslationContext(org.apache.beam.runners.core.construction.CoderTranslation.TranslationContext) DoubleCoder(org.apache.beam.sdk.coders.DoubleCoder) IntervalWindowCoder(org.apache.beam.sdk.transforms.windowing.IntervalWindow.IntervalWindowCoder) ByteCoder(org.apache.beam.sdk.coders.ByteCoder) KvCoder(org.apache.beam.sdk.coders.KvCoder) VarLongCoder(org.apache.beam.sdk.coders.VarLongCoder) BooleanCoder(org.apache.beam.sdk.coders.BooleanCoder) TimestampPrefixingWindowCoder(org.apache.beam.sdk.coders.TimestampPrefixingWindowCoder) Coder(org.apache.beam.sdk.coders.Coder) RowCoder(org.apache.beam.sdk.coders.RowCoder) StringUtf8Coder(org.apache.beam.sdk.coders.StringUtf8Coder) IterableCoder(org.apache.beam.sdk.coders.IterableCoder) IterableLikeCoder(org.apache.beam.sdk.coders.IterableLikeCoder) Test(org.junit.Test)

Example 3 with Context

use of org.apache.beam.sdk.coders.Coder.Context in project beam by apache.

the class CoderTest method testContextEqualsAndHashCode.

@Test
public void testContextEqualsAndHashCode() {
    assertEquals(Context.NESTED, new Context(false));
    assertEquals(Context.OUTER, new Context(true));
    assertNotEquals(Context.NESTED, Context.OUTER);
    assertEquals(Context.NESTED.hashCode(), new Context(false).hashCode());
    assertEquals(Context.OUTER.hashCode(), new Context(true).hashCode());
    // Even though this isn't strictly required by the hashCode contract,
    // we still want this to be true.
    assertNotEquals(Context.NESTED.hashCode(), Context.OUTER.hashCode());
}
Also used : Context(org.apache.beam.sdk.coders.Coder.Context) Test(org.junit.Test)

Example 4 with Context

use of org.apache.beam.sdk.coders.Coder.Context in project beam by apache.

the class AvroCoderTest method testAvroCoderTreeMapDeterminism.

@Test
public void testAvroCoderTreeMapDeterminism() throws Exception, NonDeterministicException {
    TreeMapField size1 = new TreeMapField();
    TreeMapField size2 = new TreeMapField();
    // Different order for entries
    size1.field.put("hello", "world");
    size1.field.put("another", "entry");
    size2.field.put("another", "entry");
    size2.field.put("hello", "world");
    AvroCoder<TreeMapField> coder = AvroCoder.of(TreeMapField.class);
    coder.verifyDeterministic();
    ByteArrayOutputStream outStream1 = new ByteArrayOutputStream();
    ByteArrayOutputStream outStream2 = new ByteArrayOutputStream();
    Context context = Context.NESTED;
    coder.encode(size1, outStream1, context);
    coder.encode(size2, outStream2, context);
    assertArrayEquals(outStream1.toByteArray(), outStream2.toByteArray());
}
Also used : Context(org.apache.beam.sdk.coders.Coder.Context) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 5 with Context

use of org.apache.beam.sdk.coders.Coder.Context in project beam by apache.

the class AvroCoderTest method testEncodingNotBuffered.

@Test
public void testEncodingNotBuffered() throws Exception {
    // This test ensures that the coder doesn't read ahead and buffer data.
    // Reading ahead causes a problem if the stream consists of records of different
    // types.
    Pojo before = new Pojo("Hello", 42, DATETIME_A);
    AvroCoder<Pojo> coder = AvroCoder.of(Pojo.class);
    SerializableCoder<Integer> intCoder = SerializableCoder.of(Integer.class);
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    Context context = Context.NESTED;
    coder.encode(before, outStream, context);
    intCoder.encode(10, outStream, context);
    ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray());
    Pojo after = coder.decode(inStream, context);
    assertEquals(before, after);
    Integer intAfter = intCoder.decode(inStream, context);
    assertEquals(Integer.valueOf(10), intAfter);
}
Also used : Context(org.apache.beam.sdk.coders.Coder.Context) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Aggregations

Context (org.apache.beam.sdk.coders.Coder.Context)5 Test (org.junit.Test)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IntervalWindowCoder (org.apache.beam.sdk.transforms.windowing.IntervalWindow.IntervalWindowCoder)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 TranslationContext (org.apache.beam.runners.core.construction.CoderTranslation.TranslationContext)1 BooleanCoder (org.apache.beam.sdk.coders.BooleanCoder)1 ByteCoder (org.apache.beam.sdk.coders.ByteCoder)1 Coder (org.apache.beam.sdk.coders.Coder)1 DoubleCoder (org.apache.beam.sdk.coders.DoubleCoder)1 IterableCoder (org.apache.beam.sdk.coders.IterableCoder)1 IterableLikeCoder (org.apache.beam.sdk.coders.IterableLikeCoder)1 KvCoder (org.apache.beam.sdk.coders.KvCoder)1 RowCoder (org.apache.beam.sdk.coders.RowCoder)1 StringUtf8Coder (org.apache.beam.sdk.coders.StringUtf8Coder)1 TimestampPrefixingWindowCoder (org.apache.beam.sdk.coders.TimestampPrefixingWindowCoder)1 VarLongCoder (org.apache.beam.sdk.coders.VarLongCoder)1