/** * Returns a {@code PTransform} that takes a {@code PCollection<V>} and returns a {@code * PCollection<KV<K, V>>}, where each of the values in the input {@code PCollection} has been * paired with the given key. */ @SuppressWarnings("unchecked") public static <K, V> WithKeys<K, V> of(@Nullable final K key) { return new WithKeys<>(value -> key, (Class<K>) (key == null ? Void.class : key.getClass())); }
/** * Return a {@link WithKeys} that is like this one with the specified key type descriptor. * * <p>For use with lambdas in Java 8, either this method must be called with an appropriate type * descriptor or {@link PCollection#setCoder(Coder)} must be called on the output {@link * PCollection}. */ public WithKeys<K, V> withKeyType(TypeDescriptor<K> keyType) { // Safe cast @SuppressWarnings("unchecked") Class<K> rawType = (Class<K>) keyType.getRawType(); return new WithKeys<>(fn, rawType); }
/** * Returns a {@code PTransform} that takes a {@code PCollection<V>} and returns a {@code * PCollection<KV<K, V>>}, where each of the values in the input {@code PCollection} has been * paired with a key computed from the value by invoking the given {@code SerializableFunction}. * * <p>If using a lambda in Java 8, {@link #withKeyType(TypeDescriptor)} must be called on the * result {@link PTransform}. */ public static <K, V> WithKeys<K, V> of(SerializableFunction<V, K> fn) { checkNotNull( fn, "WithKeys constructed with null function. Did you mean WithKeys.of((Void) null)?"); return new WithKeys<>(fn, null); }