如何将数组的每一行分解为 Spark (Scala) 中的列?
How to explode each row that is an Array into columns in Spark (Scala)?
我有一个带有单列 'value' 的 Spark DataFrame,其中每一行都是一个等长的数组。如何将这个 'value' 列分解为多个列,这些列遵循这样的模式?
Single-column DataFrame
val bronzeDfSchema = new StructType()
.add("DATE", IntegerType)
.add("NUMARTS", IntegerType)
.add("COUNTS", StringType)
.add("THEMES", StringType)
.add("LOCATIONS", StringType)
.add("PERSONS", StringType)
.add("ORGANIZATIONS", StringType)
.add("TONE", StringType)
.add("CAMEOEVENTIDS", StringType)
.add("SOURCES", StringType)
.add("SOURCEURLS", StringType)
谢谢!
这应该可以正常工作
val schema=Seq(("DATE",0),("NUMARTS",1),("COUNTS",2),("THEMES",3),("LOCATIONS",4),("PERSONS",5),("ORGANIZATIONS",6),("TONE",7),("CAMEOEVENTIDS",8),("SOURCES",9),("SOURCEURLS",10))
val df2=schema.foldLeft(df)((df,x)=>df.withColumn(x._1,col("value").getItem(x._2)))
执行此操作后,只需将列转换为所需的数据类型即可。
我有一个带有单列 'value' 的 Spark DataFrame,其中每一行都是一个等长的数组。如何将这个 'value' 列分解为多个列,这些列遵循这样的模式?
Single-column DataFrame
val bronzeDfSchema = new StructType()
.add("DATE", IntegerType)
.add("NUMARTS", IntegerType)
.add("COUNTS", StringType)
.add("THEMES", StringType)
.add("LOCATIONS", StringType)
.add("PERSONS", StringType)
.add("ORGANIZATIONS", StringType)
.add("TONE", StringType)
.add("CAMEOEVENTIDS", StringType)
.add("SOURCES", StringType)
.add("SOURCEURLS", StringType)
谢谢!
这应该可以正常工作
val schema=Seq(("DATE",0),("NUMARTS",1),("COUNTS",2),("THEMES",3),("LOCATIONS",4),("PERSONS",5),("ORGANIZATIONS",6),("TONE",7),("CAMEOEVENTIDS",8),("SOURCES",9),("SOURCEURLS",10))
val df2=schema.foldLeft(df)((df,x)=>df.withColumn(x._1,col("value").getItem(x._2)))
执行此操作后,只需将列转换为所需的数据类型即可。