Search in sources :

Example 1 with Splitter

use of org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Splitter in project beam by apache.

the class ResourceHints method fromOptions.

/**
 * Creates a {@link ResourceHints} instance with hints supplied in options.
 */
public static ResourceHints fromOptions(PipelineOptions options) {
    ResourceHintsOptions resourceHintsOptions = options.as(ResourceHintsOptions.class);
    ResourceHints result = create();
    List<String> hints = resourceHintsOptions.getResourceHints();
    Splitter splitter = Splitter.on('=').limit(2);
    for (String hint : hints) {
        List<String> parts = splitter.splitToList(hint);
        if (parts.size() != 2) {
            throw new IllegalArgumentException("Unparsable resource hint: " + hint);
        }
        String nameOrUrn = parts.get(0);
        String stringValue = parts.get(1);
        String urn;
        if (hintNameToUrn.containsKey(nameOrUrn)) {
            urn = hintNameToUrn.get(nameOrUrn);
        } else if (!nameOrUrn.startsWith("beam:resources:")) {
            // Allow unknown hints to be passed, but validate a little bit to prevent typos.
            throw new IllegalArgumentException("Unknown resource hint: " + hint);
        } else {
            urn = nameOrUrn;
        }
        ResourceHint value = parsers.getOrDefault(urn, s -> new StringHint(s)).apply(stringValue);
        result = result.withHint(urn, value);
    }
    return result;
}
Also used : RunnerApi(org.apache.beam.model.pipeline.v1.RunnerApi) StandardResourceHints(org.apache.beam.model.pipeline.v1.RunnerApi.StandardResourceHints) Charsets(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Charsets) ImmutableMap(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap) Splitter(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Splitter) Function(java.util.function.Function) List(java.util.List) Matcher(java.util.regex.Matcher) Preconditions.checkState(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkState) Map(java.util.Map) Pattern(java.util.regex.Pattern) ProtocolMessageEnum(org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.ProtocolMessageEnum) PipelineOptions(org.apache.beam.sdk.options.PipelineOptions) Nullable(org.checkerframework.checker.nullness.qual.Nullable) Splitter(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Splitter) StandardResourceHints(org.apache.beam.model.pipeline.v1.RunnerApi.StandardResourceHints)

Aggregations

List (java.util.List)1 Map (java.util.Map)1 Function (java.util.function.Function)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 RunnerApi (org.apache.beam.model.pipeline.v1.RunnerApi)1 StandardResourceHints (org.apache.beam.model.pipeline.v1.RunnerApi.StandardResourceHints)1 PipelineOptions (org.apache.beam.sdk.options.PipelineOptions)1 ProtocolMessageEnum (org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.ProtocolMessageEnum)1 Charsets (org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Charsets)1 Preconditions.checkState (org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkState)1 Splitter (org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Splitter)1 ImmutableMap (org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap)1 Nullable (org.checkerframework.checker.nullness.qual.Nullable)1