Search in sources :

Example 1 with Materializations

use of org.apache.beam.sdk.transforms.Materializations in project beam by apache.

the class FnApiStateAccessor method get.

@Override
@Nullable
public <T> T get(PCollectionView<T> view, BoundedWindow window) {
    TupleTag<?> tag = view.getTagInternal();
    SideInputSpec sideInputSpec = sideInputSpecMap.get(tag);
    checkArgument(sideInputSpec != null, "Attempting to access unknown side input %s.", view);
    ByteString.Output encodedWindowOut = ByteString.newOutput();
    try {
        sideInputSpec.getWindowCoder().encode(sideInputSpec.getWindowMappingFn().getSideInputWindow(window), encodedWindowOut);
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
    ByteString encodedWindow = encodedWindowOut.toByteString();
    StateKey.Builder cacheKeyBuilder = StateKey.newBuilder();
    switch(sideInputSpec.getAccessPattern()) {
        case Materializations.ITERABLE_MATERIALIZATION_URN:
            cacheKeyBuilder.getIterableSideInputBuilder().setTransformId(ptransformId).setSideInputId(tag.getId()).setWindow(encodedWindow);
            break;
        case Materializations.MULTIMAP_MATERIALIZATION_URN:
            checkState(sideInputSpec.getCoder() instanceof KvCoder, "Expected %s but received %s.", KvCoder.class, sideInputSpec.getCoder().getClass());
            cacheKeyBuilder.getMultimapKeysSideInputBuilder().setTransformId(ptransformId).setSideInputId(tag.getId()).setWindow(encodedWindow);
            break;
        default:
            throw new IllegalStateException(String.format("This SDK is only capable of dealing with %s materializations " + "but was asked to handle %s for PCollectionView with tag %s.", ImmutableList.of(Materializations.ITERABLE_MATERIALIZATION_URN, Materializations.MULTIMAP_MATERIALIZATION_URN), sideInputSpec.getAccessPattern(), tag));
    }
    return (T) stateKeyObjectCache.computeIfAbsent(cacheKeyBuilder.build(), key -> {
        switch(sideInputSpec.getAccessPattern()) {
            case Materializations.ITERABLE_MATERIALIZATION_URN:
                return sideInputSpec.getViewFn().apply(new IterableSideInput<>(getCacheFor(key), beamFnStateClient, processBundleInstructionId.get(), key, sideInputSpec.getCoder()));
            case Materializations.MULTIMAP_MATERIALIZATION_URN:
                return sideInputSpec.getViewFn().apply(new MultimapSideInput<>(getCacheFor(key), beamFnStateClient, processBundleInstructionId.get(), key, ((KvCoder) sideInputSpec.getCoder()).getKeyCoder(), ((KvCoder) sideInputSpec.getCoder()).getValueCoder()));
            default:
                throw new IllegalStateException(String.format("This SDK is only capable of dealing with %s materializations " + "but was asked to handle %s for PCollectionView with tag %s.", ImmutableList.of(Materializations.ITERABLE_MATERIALIZATION_URN, Materializations.MULTIMAP_MATERIALIZATION_URN), sideInputSpec.getAccessPattern(), tag));
        }
    });
}
Also used : StateSpec(org.apache.beam.sdk.state.StateSpec) CombineFn(org.apache.beam.sdk.transforms.Combine.CombineFn) CombineFnWithContext(org.apache.beam.sdk.transforms.CombineWithContext.CombineFnWithContext) SetState(org.apache.beam.sdk.state.SetState) TimestampCombiner(org.apache.beam.sdk.transforms.windowing.TimestampCombiner) Coder(org.apache.beam.sdk.coders.Coder) ValueState(org.apache.beam.sdk.state.ValueState) StateContext(org.apache.beam.sdk.state.StateContext) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) CacheToken(org.apache.beam.model.fnexecution.v1.BeamFnApi.ProcessBundleRequest.CacheToken) MapState(org.apache.beam.sdk.state.MapState) TupleTag(org.apache.beam.sdk.values.TupleTag) Map(java.util.Map) Cache(org.apache.beam.fn.harness.Cache) Iterables(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Iterables) Preconditions.checkArgument(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkArgument) Maps(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Maps) ByteString(org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.ByteString) PipelineOptions(org.apache.beam.sdk.options.PipelineOptions) Materializations(org.apache.beam.sdk.transforms.Materializations) CombineFnUtil(org.apache.beam.sdk.util.CombineFnUtil) Nullable(org.checkerframework.checker.nullness.qual.Nullable) SideInputReader(org.apache.beam.runners.core.SideInputReader) KvCoder(org.apache.beam.sdk.coders.KvCoder) OrderedListState(org.apache.beam.sdk.state.OrderedListState) Iterator(java.util.Iterator) Collection(java.util.Collection) ReadableStates(org.apache.beam.sdk.state.ReadableStates) IOException(java.io.IOException) BeamFnApi(org.apache.beam.model.fnexecution.v1.BeamFnApi) List(java.util.List) BagState(org.apache.beam.sdk.state.BagState) CombiningState(org.apache.beam.sdk.state.CombiningState) WatermarkHoldState(org.apache.beam.sdk.state.WatermarkHoldState) Preconditions.checkState(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkState) PCollectionView(org.apache.beam.sdk.values.PCollectionView) BoundedWindow(org.apache.beam.sdk.transforms.windowing.BoundedWindow) ImmutableList(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList) Caches(org.apache.beam.fn.harness.Caches) VoidCoder(org.apache.beam.sdk.coders.VoidCoder) StateBinder(org.apache.beam.sdk.state.StateBinder) StateKey(org.apache.beam.model.fnexecution.v1.BeamFnApi.StateKey) ThrowingRunnable(org.apache.beam.sdk.function.ThrowingRunnable) ReadableState(org.apache.beam.sdk.state.ReadableState) StateKey(org.apache.beam.model.fnexecution.v1.BeamFnApi.StateKey) ByteString(org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.ByteString) KvCoder(org.apache.beam.sdk.coders.KvCoder) IOException(java.io.IOException) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Aggregations

IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Function (java.util.function.Function)1 Supplier (java.util.function.Supplier)1 Cache (org.apache.beam.fn.harness.Cache)1 Caches (org.apache.beam.fn.harness.Caches)1 BeamFnApi (org.apache.beam.model.fnexecution.v1.BeamFnApi)1 CacheToken (org.apache.beam.model.fnexecution.v1.BeamFnApi.ProcessBundleRequest.CacheToken)1 StateKey (org.apache.beam.model.fnexecution.v1.BeamFnApi.StateKey)1 SideInputReader (org.apache.beam.runners.core.SideInputReader)1 Coder (org.apache.beam.sdk.coders.Coder)1 KvCoder (org.apache.beam.sdk.coders.KvCoder)1 VoidCoder (org.apache.beam.sdk.coders.VoidCoder)1 ThrowingRunnable (org.apache.beam.sdk.function.ThrowingRunnable)1 PipelineOptions (org.apache.beam.sdk.options.PipelineOptions)1 BagState (org.apache.beam.sdk.state.BagState)1