Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
340 views
in Technique[技术] by (71.8m points)

Find cartesian product of 2 lists with Apache beam w java

I have 2 Pcollections

PCollection<List<String>> ListA = pipeline.apply("getListA", ParDo.of(new getListA()))
PCollection<List<String>> ListB = pipeline.apply("getListB", ParDo.of(new getListB()))

ListA contains

["1","2","3"]

ListB contains

["A","B","C"]

How do i end up with a PCollection that contains

[
 ["A","1"],["A","2"],["A","3"],
 ["B","1"],["B","2"],["B","3"],
 ["C","1"],["C","2"],["C","3"],
]

My search has pointed me to

How to do a cartesian product of two PCollections in Dataflow?

but this is dealing with KV using coGroupby with 2 outputs. It's possible that coGroupby can be used to create the cartesian product of 2 lists but I am not seeing it

question from:https://stackoverflow.com/questions/66065890/find-cartesian-product-of-2-lists-with-apache-beam-w-java

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It looks like you have a single element in each PCollection, so you just need to join those elements, and then you can do the cartesian product yourself in a DoFn

Something like

Flatten.pcollections(ListA, List)
.apply(WithKeys.of(null))
.apply(GroupByKey.create())

After that, you'll have a PCollection with a single element, which is a KV(null, Iterable(ListA, ListB)), and you can generate the cartesian product with some for loops.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...