Search in sources :

Example 1 with ModelCoderRegistrar

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

ArrayList (java.util.ArrayList)1 BeamFnStateClient (org.apache.beam.fn.harness.state.BeamFnStateClient)1 StateResponse (org.apache.beam.model.fnexecution.v1.BeamFnApi.StateResponse)1 TranslationContext (org.apache.beam.runners.core.construction.CoderTranslation.TranslationContext)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 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 IntervalWindowCoder (org.apache.beam.sdk.transforms.windowing.IntervalWindow.IntervalWindowCoder)1