I think what you wrote is perfectly reasonable. Slightly more general,
maybe:

class KvToString<K,V> extends SimpleFunction<KV<K,V>, String> {
    public static String apply(KV<K, V> input) {
        return String.format("%s:%s", input.getKey(), input.getValue());
    }
}

Then this is reusable in MapElements across KVs of different types. But
it's effectively the same code you write.

We don't generally add a lot of utilities that replace 3-liners. I would,
however, be a fan of generic ToString, because that seems like it would be
used everywhere:

class ToStringFn<InputT> extends SimpleFunction<InputT, String> {
    public static String apply(InputT input) {
        return input.toString();
    }
}

or maybe a PTransform<PCollection<T>, PCollection<String>> that
internalized that SimpleFunction.

(caveat: I didn't actually compile or run any of that code, just typed it
in gmail :p).

Dan

On Mon, Nov 7, 2016 at 3:56 PM, Jesse Anderson <[email protected]>
wrote:

> Is there a way to directly take a PCollection<KV> and make it a
> PCollection<String>? I need to make the PCollection a PCollection<String>
> before writing it out with TextIO.Write.
>
> I tried using:
> withCoder(KvCoder.of(StringDelegateCoder.of(String.class),
> StringDelegateCoder.of(Long.class))
>
> but that causes binary data to be written out by the KV coder.
>
> The only way appears to be a manual transform with:
> PCollection<String> stringCounts = counts.apply(MapElements
>     .via((KV<String, Long> count) ->
>     count.getKey() + ":" + count.getValue())
>     .withOutputType(TypeDescriptors.strings()));
>
> If this is missing, that manual step should be baked into the API. That
> should be something either in StringDelegateCoder or a new String
> transform. The new StringDelegateCoder method would take in any KV (or list
> types) and put a specific String delimiter. The new transform would take in
> any type in a PCollection<T> and makes it a PCollection<String> using a
> specific String delimiter.
>
> Thanks,
>
> Jesse
>

Reply via email to