AWS Glue - pySpark:将字符串列拆分为新的整数数组列

AWS Glue - pySpark: spliting a string column into a new integer array column

我正在尝试使用 Glue 和 pySpark 在 AWS 上执行 ETL 作业,但不幸的是,我对此很陌生。

在大多数情况下,我在使用胶水动态数据框执行应用映射和我必须执行的其他一些转换时没有任何问题。但是我遇到了一个特定列的问题,我必须将它从字符串转换为整数数组。在此列中,value,我们将数据类型设置为字符串,它实际上是一个转换为字符串并由 space 分隔的整数数组,例如 value 列中的数据条目看起来喜欢 '111 222 333 444 555 666'。我必须将此列转换为整数数组,以便我的数据转换为 '[111, 222, 333, 444, 555, 666]'

我如何在 AWS Glue 中使用 pySpark 实现此目的?非常感谢任何帮助。

使用 split 函数将 value 列拆分为 space 并转换为 array<int>.

  • (或)通过使用 transform (From Spark-2.4) 函数并将数组元素转换为 int.

Example:

df=spark.createDataFrame([('111 222 333 444 555 666',)],["value"])
df.printSchema()
#root
# |-- value: string (nullable = true)

#using split and cast as array<int>  
df.withColumn("array_int",split(col("value"),"\s+").cast("array<int>")).\
    show(10,False)

#using transform function
df.withColumn("array_int",expr("""transform(split(value,"\\s+"), x -> int(x))""")).\
show(10,False)
#+-----------------------+------------------------------+
#|value                  |array_int                     |
#+-----------------------+------------------------------+
#|111 222 333 444 555 666|[111, 222, 333, 444, 555, 666]|
#+-----------------------+------------------------------+

df.withColumn("array_int",split(col("value"),"\s+").cast("array<int>")).printSchema()
df.withColumn("array_int",expr("""transform(split(value,"\\s+"), x -> int(x))""")).printSchema()    
#root
# |-- value: string (nullable = true)
# |-- array_int: array (nullable = true)
# |    |-- element: integer (containsNull = true)