我正在尝试基于云数据流管道中的自定义对象来实现Groupby密钥。
public static void main(String[] args) {
Pipeline pipeline = Pipeline.create(PipelineOptionsFactory.create());
List<KV<Student,StudentValues>> studentList = new ArrayList<>();
studentList.add(KV.of(new Student("pawan", 10,"govt"),
new StudentValues("V1", 123,"govt")));
studentList.add(KV.of(new Student("pawan", 13223,"word"),
new StudentValues("V2", 456,"govt")));
PCollection<KV<Student,StudentValues>> pc =
pipeline.apply(Create.of(studentList));
PCollection<KV<Student, Iterable<StudentValues>>> groupedWords =
pc.apply(GroupByKey.<Student,StudentValues>create());
}
我只想将基于Student对象的PCollection记录分组。
@DefaultCoder(AvroCoder.class)
static class Student /*implements Serializable*/{
public Student(){}
public Student(String n, Integer i, String sc){
name = n;
id = i;
school = sc;
}
public String name;
public Integer id;
public String school;
@Override
public boolean equals(Object obj) {
System.out.println("obj = "+obj);
System.out.println("this = "+this);
Student stObj= (Student)obj;
if (stObj.Name== this.Name){
return true;
} else{
return false;
}
}
}
我已经覆盖了我的自定义类的equals方法,但是每次我得到相同的Student对象实例来比较equals方法。 理想情况下,它会将第一个学生密钥与第二个学生密钥进行比较。
我在这里做错了。
答案 0 :(得分:0)
为什么你认为你做错了什么?每个元素的键都是序列化的(使用您指定的AvroCoder),GroupByKey可以将具有相同序列化表示的所有元素组合在一起。之后,它不需要比较学生,以确保具有相同键的值已组合在一起。