Skip to content
Advertisement

Apache Beam framework – sort in descending order

How do you sort in descending order using the Apache Beam framework?

I managed to create a word count pipeline which sorts alphabetically the output by word, but did not figure out how to invert the sorting order.

Here is the code:

JavaScript

This code produces a list of words sorted alphabetically with its respective counts:

JavaScript

Edit 1:

After some debugging I know that the code uses the class:

JavaScript

This class uses a static final Comparator during the execution of the sort method:

JavaScript

There is no way to inject a comparator in this class.

Advertisement

Answer

You can extract the Iterable<KV<String, Long>> into a List<KV<String, Long>> and reverse the list using Collections.reverse().

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement