Java Code Examples for org.apache.beam.sdk.coders.KvCoder#encode()

The following examples show how to use org.apache.beam.sdk.coders.KvCoder#encode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KryoCoderTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void testCodingWithKvCoderValueIsKryoCoder() throws IOException {
  final KryoRegistrar registrar = k -> k.register(TestClass.class);

  final KvCoder<String, TestClass> kvCoder =
      KvCoder.of(StringUtf8Coder.of(), KryoCoder.of(OPTIONS, registrar));

  final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

  final String inputKey = "key";
  final TestClass inputValue = new TestClass("something");
  kvCoder.encode(KV.of(inputKey, inputValue), byteArrayOutputStream);

  final KV<String, TestClass> decoded =
      kvCoder.decode(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));

  assertNotNull(decoded);
  assertNotNull(decoded.getKey());
  assertEquals(inputKey, decoded.getKey());

  assertNotNull(decoded.getValue());
  assertEquals(inputValue, decoded.getValue());
}
 
Example 2
Source File: KryoCoderTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example 3
Source File: KryoCoderTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example 4
Source File: FakeBigQueryServices.java    From beam with Apache License 2.0 5 votes vote down vote up
public static String encodeQueryResult(Table table, List<TableRow> rows) throws IOException {
  KvCoder<String, List<TableRow>> coder =
      KvCoder.of(StringUtf8Coder.of(), ListCoder.of(TableRowJsonCoder.of()));
  KV<String, List<TableRow>> kv = KV.of(BigQueryHelpers.toJsonString(table), rows);
  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  coder.encode(kv, outputStream);
  return Base64.encodeBase64String(outputStream.toByteArray());
}