【问题标题】:How to do group by key on custom logic in cloud data flow如何在云数据流中对自定义逻辑进行按键分组
【发布时间】:2017-08-30 15:59:47
【问题描述】:

我正在尝试基于云数据流管道中的自定义对象来实现 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 方法。 理想情况下,它应该将第一个学生密钥与第二个学生密钥进行比较。

我在这里做错了什么。

【问题讨论】:

    标签: google-cloud-dataflow google-cloud-dataproc


    【解决方案1】:

    为什么你认为你做错了什么?每个元素的键都是序列化的(使用您指定的 AvroCoder),并且 GroupByKey 可以将具有相同序列化表示的所有元素组合在一起。之后就不需要比较学生来确保具有相同键的值已经分组在一起了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多