将行值转换为火花数据框中的列数组 [英] Converting row values into a column array in spark dataframe
本文介绍了将行值转换为火花数据框中的列数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在处理 spark 数据框,我需要对列进行分组,并将分组行的列值转换为元素数组作为新列.示例:
I am working on spark dataframes and I need to do a group by of a column and convert the column values of grouped rows into an array of elements as new column. Example :
Input:
employee | Address
------------------
Micheal | NY
Micheal | NJ
Output:
employee | Address
------------------
Micheal | (NY,NJ)
非常感谢任何帮助.
推荐答案
这里有一个替代解决方案我已将数据帧转换为用于转换的 rdd,并使用 sqlContext.createDataFrame()
Here is an alternate solution
Where I have converted the dataframe to an rdd for the transformations and converted it back a dataFrame using sqlContext.createDataFrame()
示例.json
{"employee":"Michale","Address":"NY"}
{"employee":"Michale","Address":"NJ"}
{"employee":"Sam","Address":"NY"}
{"employee":"Max","Address":"NJ"}
星火应用
val df = sqlContext.read.json("sample.json")
// Printing the original Df
df.show()
//Defining the Schema for the aggregated DataFrame
val dataSchema = new StructType(
Array(
StructField("employee", StringType, nullable = true),
StructField("Address", ArrayType(StringType, containsNull = true), nullable = true)
)
)
// Converting the df to rdd and performing the groupBy operation
val aggregatedRdd: RDD[Row] = df.rdd.groupBy(r =>
r.getAs[String]("employee")
).map(row =>
// Mapping the Grouped Values to a new Row Object
Row(row._1, row._2.map(_.getAs[String]("Address")).toArray)
)
// Creating a DataFrame from the aggregatedRdd with the defined Schema (dataSchema)
val aggregatedDf = sqlContext.createDataFrame(aggregatedRdd, dataSchema)
// Printing the aggregated Df
aggregatedDf.show()
输出:
+-------+--------+---+
|Address|employee|num|
+-------+--------+---+
| NY| Michale| 1|
| NJ| Michale| 2|
| NY| Sam| 3|
| NJ| Max| 4|
+-------+--------+---+
+--------+--------+
|employee| Address|
+--------+--------+
| Sam| [NY]|
| Michale|[NY, NJ]|
| Max| [NJ]|
+--------+--------+
这篇关于将行值转换为火花数据框中的列数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文