Java Code Examples for org.apache.beam.sdk.coders.Coder#decode()

The following examples show how to use org.apache.beam.sdk.coders.Coder#decode() . 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: CoderProperties.java    From beam with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
static <T> T decode(Coder<T> coder, Coder.Context context, byte[] bytes)
    throws CoderException, IOException {
  @SuppressWarnings("unchecked")
  Coder<T> deserializedCoder = SerializableUtils.clone(coder);

  byte[] buffer;
  if (Objects.equals(context, Coder.Context.NESTED)) {
    buffer = new byte[bytes.length + 1];
    System.arraycopy(bytes, 0, buffer, 0, bytes.length);
    buffer[bytes.length] = 1;
  } else {
    buffer = bytes;
  }

  CountingInputStream cis = new CountingInputStream(new ByteArrayInputStream(buffer));
  T value = deserializedCoder.decode(new UnownedInputStream(cis), context);
  assertThat(
      "consumed bytes equal to encoded bytes", cis.getCount(), equalTo((long) bytes.length));
  return value;
}
 
Example 2
Source File: WindmillStateReader.java    From beam with Apache License 2.0 6 votes vote down vote up
private <T> void consumeTagValue(TagValue tagValue, StateTag stateTag) {
  CoderAndFuture<T, T> coderAndFuture = getWaiting(stateTag, false);
  SettableFuture<T> future = coderAndFuture.getNonDoneFuture(stateTag);
  Coder<T> coder = coderAndFuture.getAndClearCoder();

  if (tagValue.hasValue()
      && tagValue.getValue().hasData()
      && !tagValue.getValue().getData().isEmpty()) {
    InputStream inputStream = tagValue.getValue().getData().newInput();
    try {
      T value = coder.decode(inputStream, Coder.Context.OUTER);
      future.set(value);
    } catch (IOException e) {
      throw new IllegalStateException("Unable to decode value using " + coder, e);
    }
  } else {
    future.set(null);
  }
}
 
Example 3
Source File: TranslationUtils.java    From twister2 with Apache License 2.0 5 votes vote down vote up
/**
 * Utility method for deserializing a byte array using the specified coder. (From spark code)
 *
 * @param serialized bytearray to be deserialized.
 * @param coder Coder to deserialize with.
 * @param <T> Type of object to be returned.
 * @return Deserialized object.
 */
public static <T> T fromByteArray(byte[] serialized, Coder<T> coder) {
  ByteArrayInputStream bais = new ByteArrayInputStream(serialized);
  try {
    return coder.decode(bais);
  } catch (IOException e) {
    throw new IllegalStateException("Error decoding bytes for coder: " + coder, e);
  }
}
 
Example 4
Source File: FnApiDoFnRunnerTest.java    From beam with Apache License 2.0 5 votes vote down vote up
private static <T> T decode(Coder<T> coder, ByteString value) {
  try {
    return coder.decode(value.newInput());
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example 5
Source File: CoderUtils.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Decodes a value from the given {@code stream}, which should be a stream that never throws
 * {@code IOException}, such as {@code ByteArrayInputStream} or {@link
 * ExposedByteArrayInputStream}.
 */
private static <T> T decodeFromSafeStream(
    Coder<T> coder, InputStream stream, Coder.Context context) throws CoderException {
  try {
    return coder.decode(new UnownedInputStream(stream), context);
  } catch (IOException exn) {
    Throwables.propagateIfPossible(exn, CoderException.class);
    throw new IllegalArgumentException(
        "Forbidden IOException when reading from InputStream", exn);
  }
}
 
Example 6
Source File: FakeJobService.java    From beam with Apache License 2.0 5 votes vote down vote up
private List<TableRow> readJsonTableRows(String filename) throws IOException {
  Coder<TableRow> coder = TableRowJsonCoder.of();
  List<TableRow> tableRows = Lists.newArrayList();
  try (BufferedReader reader =
      Files.newBufferedReader(Paths.get(filename), StandardCharsets.UTF_8)) {
    String line;
    while ((line = reader.readLine()) != null) {
      TableRow tableRow =
          coder.decode(
              new ByteArrayInputStream(line.getBytes(StandardCharsets.UTF_8)), Context.OUTER);
      tableRows.add(tableRow);
    }
  }
  return tableRows;
}
 
Example 7
Source File: IterableCombinerTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void testSerializing() throws IOException {

  IterableCombinerFn<String> tested = new IterableCombinerFn<>(STRING_TYPE_DESCRIPTOR);
  IterableCombinerFn.CollectionAccumulator<String> originalAccumulator =
      tested.createAccumulator();

  FIRST_ITEMS.forEach(originalAccumulator::addInput);

  Coder<IterableCombinerFn.CollectionAccumulator<String>> accumulatorCoder =
      tested.getAccumulatorCoder(null, StringUtf8Coder.of());

  byte[] bytes;

  try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
    accumulatorCoder.encode(originalAccumulator, byteArrayOutputStream);
    byteArrayOutputStream.flush();

    bytes = byteArrayOutputStream.toByteArray();
  }

  IterableCombinerFn.CollectionAccumulator<String> decodedAccumulator;

  try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes)) {
    decodedAccumulator = accumulatorCoder.decode(byteArrayInputStream);
  }

  String[] originalItems = FIRST_ITEMS.toArray(new String[0]);

  MatcherAssert.assertThat(
      originalAccumulator.extractOutput(), Matchers.containsInAnyOrder(originalItems));
  MatcherAssert.assertThat(
      decodedAccumulator.extractOutput(), Matchers.containsInAnyOrder(originalItems));
}
 
Example 8
Source File: CoderHelpers.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Utility method for deserializing a byte array using the specified coder.
 *
 * @param serialized bytearray to be deserialized.
 * @param coder Coder to deserialize with.
 * @param <T> Type of object to be returned.
 * @return Deserialized object.
 */
public static <T> T fromByteArray(byte[] serialized, Coder<T> coder) {
  ByteArrayInputStream bais = new ByteArrayInputStream(serialized);
  try {
    return coder.decode(bais);
  } catch (IOException e) {
    throw new IllegalStateException("Error decoding bytes for coder: " + coder, e);
  }
}
 
Example 9
Source File: ValueAndCoderLazySerializable.java    From beam with Apache License 2.0 5 votes vote down vote up
public T getOrDecode(Coder<T> coder) {
  if (!(coderOrBytes instanceof Coder)) {
    ByteArrayInputStream bais = new ByteArrayInputStream((byte[]) this.coderOrBytes);
    try {
      value = coder.decode(bais);
    } catch (IOException e) {
      throw new IllegalStateException("Error decoding bytes for coder: " + coder, e);
    }
    this.coderOrBytes = coder;
  }

  return value;
}
 
Example 10
Source File: CoderHelpers.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Utility method for deserializing a byte array using the specified coder.
 *
 * @param serialized bytearray to be deserialized.
 * @param coder Coder to deserialize with.
 * @param <T> Type of object to be returned.
 * @return Deserialized object.
 */
public static <T> T fromByteArray(byte[] serialized, Coder<T> coder) {
  ByteArrayInputStream bais = new ByteArrayInputStream(serialized);
  try {
    return coder.decode(bais);
  } catch (IOException e) {
    throw new IllegalStateException("Error decoding bytes for coder: " + coder, e);
  }
}
 
Example 11
Source File: DoFnOperator.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public KV<Integer, WindowedValue<?>> decode(InputStream in) throws IOException {
  Integer id = VarIntCoder.of().decode(in);
  Coder<WindowedValue<?>> coder = idsToCoders.get(id);
  WindowedValue<?> value = coder.decode(in);
  return KV.of(id, value);
}
 
Example 12
Source File: StreamingModeExecutionContext.java    From beam with Apache License 2.0 5 votes vote down vote up
public UnboundedSource.CheckpointMark getReaderCheckpoint(
    Coder<? extends UnboundedSource.CheckpointMark> coder) {
  try {
    ByteString state = work.getSourceState().getState();
    if (state.isEmpty()) {
      return null;
    }
    return coder.decode(state.newInput(), Coder.Context.OUTER);
  } catch (IOException e) {
    throw new RuntimeException("Exception while decoding checkpoint", e);
  }
}
 
Example 13
Source File: WindmillSink.java    From beam with Apache License 2.0 5 votes vote down vote up
public static Collection<? extends BoundedWindow> decodeMetadataWindows(
    Coder<Collection<? extends BoundedWindow>> windowsCoder, ByteString metadata)
    throws IOException {
  InputStream inStream = metadata.newInput();
  PaneInfoCoder.INSTANCE.decode(inStream);
  return windowsCoder.decode(inStream, Coder.Context.OUTER);
}
 
Example 14
Source File: BillingEvent.java    From nomulus with Apache License 2.0 5 votes vote down vote up
@Override
public InvoiceGroupingKey decode(InputStream inStream) throws IOException {
  Coder<String> stringCoder = StringUtf8Coder.of();
  return new AutoValue_BillingEvent_InvoiceGroupingKey(
      stringCoder.decode(inStream),
      stringCoder.decode(inStream),
      stringCoder.decode(inStream),
      stringCoder.decode(inStream),
      stringCoder.decode(inStream),
      Double.parseDouble(stringCoder.decode(inStream)),
      stringCoder.decode(inStream),
      stringCoder.decode(inStream));
}
 
Example 15
Source File: UngroupedWindmillReader.java    From beam with Apache License 2.0 4 votes vote down vote up
private <X> X decode(Coder<X> coder, InputStream input) throws IOException {
  return coder.decode(input, Coder.Context.OUTER);
}