Skip to main content

Command Palette

Search for a command to run...

Apache Beam: KvSwap

Swap the Keys and Values of a Key-Value Pair

Updated
1 min read
N

Los Angeles

Overview

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?

NO!

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 =
        pipeline.apply(
            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());

Conclusion

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

Apache Beam and Google Cloud Dataflow

Part 9 of 12

Dive into the world of scalable data processing with our comprehensive series on Apache Beam and Google Cloud Dataflow.

Up next

Apache Beam: Keys

Get the Keys from a Key-Value Pair

More from this blog

Nikhil Rao's Blog

18 posts

For the love of Data