【问题标题】:Modify Spark DataFrame structure修改 Spark DataFrame 结构
【发布时间】:2019-04-25 15:48:34
【问题描述】:

假设我有一个包含以下列的 Spark DataFrame:

| header1 | location | precision | header2 | velocity | data |

(这个df也包含一些数据)

现在我想将 df 转换为具有 2 列的新结构,每列都有复杂的字段 - 类似这样:

|          gps                   |         velocity          |
| header1 | location | precision | header2 | velocity | data |

如果我可以调用一个方法最好:

df1 = createStructure(df, "gps", ["header1", "gps", "precision"])
df2 = createStructure(df1, "velocity", ["header2", "velocity", "data"])

我正在尝试“withColumn”,但没有运气

【问题讨论】:

    标签: apache-spark dataframe data-structures


    【解决方案1】:

    试试这个。

    scala> import org.apache.spark.sql.functions._
    import org.apache.spark.sql.functions._
    
    scala> val df1 = Seq(("h1-4", "loc4", "prec4", "h2-4", "vel4", "d4"), ("h1-5", "loc5", "prec5", "h2-5", "vel5", "d5")).toDF("header1", "location", "precision", "header2", "velocity", "data")
    df1: org.apache.spark.sql.DataFrame = [header1: string, location: string ... 4 more fields]
    
    scala> df1.show(false)
    +-------+--------+---------+-------+--------+----+
    |header1|location|precision|header2|velocity|data|
    +-------+--------+---------+-------+--------+----+
    |h1-4   |loc4    |prec4    |h2-4   |vel4    |d4  |
    |h1-5   |loc5    |prec5    |h2-5   |vel5    |d5  |
    +-------+--------+---------+-------+--------+----+
    
    
    scala> val outputDF = df1.withColumn("gps", struct($"header1", $"location", $"precision")).withColumn("velocity", struct($"header2", $"velocity", $"data")).select("gps", "velocity")
    outputDF: org.apache.spark.sql.DataFrame = [gps: struct<header1: string, location: string ... 1 more field>, velocity: struct<header2: string, velocity: string ... 1 more field>]
    
    scala> outputDF.printSchema
    root
    |-- gps: struct (nullable = false)
    |    |-- header1: string (nullable = true)
    |    |-- location: string (nullable = true)
    |    |-- precision: string (nullable = true)
    |-- velocity: struct (nullable = false)
    |    |-- header2: string (nullable = true)
    |    |-- velocity: string (nullable = true)
    |    |-- data: string (nullable = true)
    
    
    scala> outputDF.show(false)
    +-------------------+----------------+
    |gps                |velocity        |
    +-------------------+----------------+
    |[h1-4, loc4, prec4]|[h2-4, vel4, d4]|
    |[h1-5, loc5, prec5]|[h2-5, vel5, d5]|
    +-------------------+----------------+
    

    【讨论】:

    • 谢谢!完成了这项工作
    猜你喜欢
    • 1970-01-01
    • 2017-02-04
    • 1970-01-01
    • 1970-01-01
    • 2021-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多