Search in sources :

Example 1 with TranslationContext

use of org.apache.beam.runners.core.construction.CoderTranslation.TranslationContext 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)

Example 2 with TranslationContext

use of org.apache.beam.runners.core.construction.CoderTranslation.TranslationContext in project beam by apache.

the class CommonCoderTest method instantiateCoder.

private static Coder<?> instantiateCoder(CommonCoder coder) {
    List<Coder<?>> components = new ArrayList<>();
    for (CommonCoder innerCoder : coder.getComponents()) {
        components.add(instantiateCoder(innerCoder));
    }
    // translator since we need to interact with a fake state client.
    if (coder.getUrn().equals(getUrn(StandardCoders.Enum.STATE_BACKED_ITERABLE))) {
        BeamFnStateClient stateClient = new BeamFnStateClient() {

            @Override
            public CompletableFuture<StateResponse> handle(StateRequest.Builder requestBuilder) {
                checkState(requestBuilder.hasGet());
                checkState(requestBuilder.hasStateKey());
                checkState(requestBuilder.getStateKey().hasRunner());
                StateResponse.Builder rval = StateResponse.newBuilder();
                rval.setId(requestBuilder.getId());
                rval.setGet(StateGetResponse.newBuilder().setData(coder.getState().getOrDefault(requestBuilder.getStateKey().getRunner().getKey(), ByteString.EMPTY)));
                return CompletableFuture.completedFuture(rval.build());
            }
        };
        return new StateBackedIterable.Coder<>(() -> Caches.noop(), stateClient, () -> "instructionId", (Coder) Iterables.getOnlyElement(components));
    }
    Class<? extends Coder> coderType = ImmutableBiMap.copyOf(new ModelCoderRegistrar().getCoderURNs()).inverse().get(coder.getUrn());
    checkNotNull(coderType, "Unknown coder URN: " + coder.getUrn());
    CoderTranslator<?> translator = new ModelCoderRegistrar().getCoderTranslators().get(coderType);
    checkNotNull(translator, "No translator found for common coder class: " + coderType.getSimpleName());
    return translator.fromComponents(components, coder.getPayload(), new TranslationContext() {
    });
}
Also used : 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) ModelCoderRegistrar(org.apache.beam.runners.core.construction.ModelCoderRegistrar) BeamFnStateClient(org.apache.beam.fn.harness.state.BeamFnStateClient) ArrayList(java.util.ArrayList) StateResponse(org.apache.beam.model.fnexecution.v1.BeamFnApi.StateResponse) TranslationContext(org.apache.beam.runners.core.construction.CoderTranslation.TranslationContext)

Aggregations

TranslationContext (org.apache.beam.runners.core.construction.CoderTranslation.TranslationContext)2 RowCoder (org.apache.beam.sdk.coders.RowCoder)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 BeamFnStateClient (org.apache.beam.fn.harness.state.BeamFnStateClient)1 StateResponse (org.apache.beam.model.fnexecution.v1.BeamFnApi.StateResponse)1 ModelCoderRegistrar (org.apache.beam.runners.core.construction.ModelCoderRegistrar)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 StringUtf8Coder (org.apache.beam.sdk.coders.StringUtf8Coder)1 TimestampPrefixingWindowCoder (org.apache.beam.sdk.coders.TimestampPrefixingWindowCoder)1 VarLongCoder (org.apache.beam.sdk.coders.VarLongCoder)1 Schema (org.apache.beam.sdk.schemas.Schema)1 IntervalWindowCoder (org.apache.beam.sdk.transforms.windowing.IntervalWindow.IntervalWindowCoder)1 InvalidProtocolBufferException (org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.InvalidProtocolBufferException)1