Apache Beam,如何在对象列表中进行分组?

huangapple go评论76阅读模式
英文:

Apache Beam , how to GroupBy in a List of Objects

问题

我有一个Car对象的列表在一个PCollection中。
PCollection<List<Car>>

每辆车都有一个颜色。

我想按颜色对这个列表进行排序,其中颜色是键,具有该颜色的车辆是值,并最终得到一个KV<String, List<Car>>

{"red":[car1,car2],"green":[car3,car4]}

Car car1 = new Car();
Car car2 = a new Car();
Car car3 = new Car();
Car car4 = a new Car();

car1.setColor("red");
car2.setColor("red");
car3.setColor("green");
car4.setColor("green");

final List<Car> cars = Arrays.asList(car1,car2,car3,car4);
PCollection<Car> carsCollection = pipeline.apply(Create.of(cars));

PCollection<KV<String, List<Car>>> sortedCars = carsCollection.apply(...)

也许类似这样的代码可以工作

PCollection<KV<String, List<Car>>> sortedCars =
   cars.apply(WithKeys.of(new SimpleFunction<String, List<Car>>() {
       @Override
        public String apply(Car car) {
            return car.getColor();
        }
}));
英文:

I have a list of cars objects in a PCollection.
PCollection<List<Car>>

Each car has a color.

I want to sort this list where the color is the key and cars that have that color are the values and end up with a KV<String, List<Car>>

{"red":[car1,car2],"green":[car3,car4]}

Car car1 = new Car();
Car car2 = new Car();
Car car3 = new Car();
Car car4 = new Car();
    
car1.setColor("red");
car2.setColor("red");
car3.setColor("green");
car4.setColor("green");

final List<Cars> cars = Arrays.asList(car1,car2,car3,car4);
PCollection<Car> carsCollection = pipeline.apply(Create.of(cars));


PCollection<KV<String, List<Car>>> sortedCars = carsCollection.apply(...)
 

Maybe something like this wold work

PCollection<KV<String, List<Car>>> sortedCars =
   cars.apply(WithKeys.of(new SimpleFunction<String, List<Car>>() {
       @Override
        public String apply(Car car) {
            return cat.getColor();
        }
}));

答案1

得分: 2

你可以使用 Core 中的 GroupByKey 转换。

对于你的 WithKeys,你也可以使用 lambda 表达式

(WithKey.of(x -> x.getColor())).apply(GroupByKey.create())

这将产生一个 KV<key,Iterable<Car>>。

英文:

You can make use of the Core GroupByKey transform.

For your WithKeys you can also make use of the lambda

(WithKey.of(x -&gt; x.getColor())).apply(GroupByKey.create())

This will produce a KV<key,Iterable<Car>>

huangapple
  • 本文由 发表于 2020年10月22日 04:58:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/64471666.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定