Search in sources :

Example 1 with RowCoder

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

the class ExternalSchemaIOTransformRegistrar method translateRow.

private static Row translateRow(byte[] rowBytes, Schema configSchema) {
    RowCoder rowCoder = RowCoder.of(configSchema);
    InputStream stream = new ByteArrayInputStream(rowBytes);
    try {
        return rowCoder.decode(stream);
    } catch (IOException e) {
        throw new RuntimeException("Unable to infer configuration row from configuration proto and schema.", e);
    }
}
Also used : RowCoder(org.apache.beam.sdk.coders.RowCoder) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 2 with RowCoder

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

the class CoderTranslators method row.

static CoderTranslator<RowCoder> row() {
    return new CoderTranslator<RowCoder>() {

        @Override
        public List<? extends Coder<?>> getComponents(RowCoder from) {
            return ImmutableList.of();
        }

        @Override
        public byte[] getPayload(RowCoder from) {
            return SchemaTranslation.schemaToProto(from.getSchema(), true).toByteArray();
        }

        @Override
        public RowCoder fromComponents(List<Coder<?>> components, byte[] payload, TranslationContext context) {
            checkArgument(components.isEmpty(), "Expected empty component list, but received: " + components);
            Schema schema;
            try {
                schema = SchemaTranslation.schemaFromProto(SchemaApi.Schema.parseFrom(payload));
            } catch (InvalidProtocolBufferException e) {
                throw new RuntimeException("Unable to parse schema for RowCoder: ", e);
            }
            return RowCoder.of(schema);
        }
    };
}
Also used : RowCoder(org.apache.beam.sdk.coders.RowCoder) Schema(org.apache.beam.sdk.schemas.Schema) InvalidProtocolBufferException(org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.InvalidProtocolBufferException) List(java.util.List) ImmutableList(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList) TranslationContext(org.apache.beam.runners.core.construction.CoderTranslation.TranslationContext)

Aggregations

RowCoder (org.apache.beam.sdk.coders.RowCoder)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 List (java.util.List)1 TranslationContext (org.apache.beam.runners.core.construction.CoderTranslation.TranslationContext)1 Schema (org.apache.beam.sdk.schemas.Schema)1 InvalidProtocolBufferException (org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.InvalidProtocolBufferException)1 ImmutableList (org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList)1