Apache Beam: KvSwap

Swap the Keys and Values of a Key-Value Pair


1 min read


What if you have a Key-Value pair, but want to group on the values, not the keys? Should you write a custom doFn to switch the keys and values?


You should use the KvSwap transform!

When You Should Use the KvSwap Transform

When you want to easily swap the keys and values of a PCollection of KV pairs.

How to Use the KvSwap Transform

Just apply the built-in Transform to a PCollection of KVs. The output PCollection will have the Keys and Values swapped from the input KV.

Example: Swap Keys and Values from Word Counts

    // Create key/value pairs
    PCollection<KV<String, Integer>> pairs =
            Create.of(KV.of("one", 1), KV.of("two", 2), KV.of("three", 3), KV.of("four", 4)));
    // Returns KV collection with keys and values swapped: PCollection<KV<K,V>> ->
    // PCollection<KV<V,K>>
    PCollection<KV<Integer, String>> swap = pairs.apply(KvSwap.create());


Check out other useful transforms from the official Apache Beam documentation.