【问题标题】:Create AVRO File AWS Glue Dynamic Frame One to Many Join创建 AVRO 文件 AWS Glue 动态帧一对多连接
【发布时间】:2021-05-12 23:07:09
【问题描述】:

在 AWS Glue 中是否可能出现以下行为? 我正在尝试通过以一对多方式加入两个 DynamicFrame 来创建单个 AVRO 文件。

例如,我有一个包含多种教师类型的 DyF: 教师编号 教师姓名

还有一个有许多学生类型的 Dyf: 学生卡 教师编号 学生姓名

我正在尝试将这些组合起来,以便老师可能有很多学生,例如:

[
  {
    teacher_id: 1,
    teacher_name: 'John',
    students: [
      {
        student_id: 100,
        teacher_id: 1
        student_name: 'Sally'
      },
      {
        student_id: 200,
        teacher_id: 1,
        student_name: 'Jack'
      }
    ]
  },
  ...
]

使用 Join.apply(teacher, student, 'teacher_id', 'teacher_id') 只会导致重复行:

[
  {
    teacher_id: 1,
    teacher_name: 'John',
    student_id: 100,
    teacher_id: 1
    student_name: 'Sally'
  },
  {
    teacher_id: 1,
    teacher_name: 'John',
    student_id: 200,
    teacher_id: 1
    student_name: 'Jack'
  }
  ...
]
]

【问题讨论】:

    标签: python amazon-web-services etl aws-glue spark-avro


    【解决方案1】:

    可能有比我在下面建议的更好的方法来做到这一点,但我希望以下方法可行:

    from pyspark.sql.functions import col,struct
    
    #first make your two tables into dataframes so we can use Spark
    students = students.toDF()
    teachers = teachers.toDF()
    
    #then convert your students DF to having a foreign key and a struct
    students = students.select(
      col("teacher_id").alias("student_teacher_id"),
      struct("student_id","teacher_id","student_name").alias("student_data"))#I'm not sure you want to keep the teacher_id here, but up to you :)
    
    #then perform your join
    result = teachers.join(students, teachers.teacher_id == students.student_teacher_id)
    

    在此之后,您应该得到包含所有教师数据的行,并且教师数据将具有包含与教师相关的学生的 struct 列。如果您要序列化或输出为分层格式(例如 JSON),它应该将每个学生显示为老师的孩子。

    【讨论】:

      猜你喜欢
      • 2020-09-26
      • 1970-01-01
      • 1970-01-01
      • 2021-04-19
      • 2020-01-22
      • 1970-01-01
      • 2021-11-26
      • 1970-01-01
      • 2021-10-06
      相关资源
      最近更新 更多