There are two versions of WithKeys
, depending on how the key should be determined:
WithKeys.of(SerializableFunction<V, K> fn)
takes a function to compute the key from each value.WithKeys.of(K key)
associates each value with the specified key.Example
PCollection<String> words = Create.of("Hello", "World", "Beam", "is", "fun"); PCollection<KV<Integer, String>> lengthAndWord = words.apply(WithKeys.of(new SerialiazableFunction<String, Integer>() { @Override public Integer apply(String s) { return s.length(); } });