【问题标题】:Split struct type column in dataframe into multiple columns将数据框中的结构类型列拆分为多列
【发布时间】:2017-08-01 21:52:14
【问题描述】:

我使用 reducebyKey 从 rdd 创建了以下数据框。我想将第一列(最初是键)拆分为 2 个新列,用逗号分隔。

scala> result_winr_table.schema
res10: org.apache.spark.sql.types.StructType = StructType(StructField(_1,StructType(StructField(_1,IntegerType,false), StructField(_2,IntegerType,false)),true), StructField(_2,DoubleType,false))

scala> result_winr_table
    res5: org.apache.spark.sql.DataFrame = [_1: struct<_1:int,_2:int>, _2: double]

    scala> result_winr_table.show
    +--------+-------------------+
    |      _1|                 _2|
    +--------+-------------------+
    | [31,88]|              0.475|
    | [18,91]| 0.5833333333333334|
    | [56,95]|0.37142857142857144|
    | [70,61]| 0.6266666666666667|
    |[104,11]| 0.4527911784975879|
    | [42,58]| 0.6857142857142857|
    | [13,82]| 0.3333333333333333|
    | [30,18]|0.49310344827586206|
    | [99,18]|0.44285714285714284|
    | [53,31]| 0.2981366459627329|
    | [52,84]| 0.4444444444444444|
    | [60,38]|               0.38|
    |  [79,9]|0.36666666666666664|
    | [20,85]| 0.4389312977099237|
    | [61,87]| 0.4807692307692308|
    |  [3,67]| 0.4245810055865922|
    | [62,84]|0.47796610169491527|
    |  [9,32]| 0.4727272727272727|
    | [94,44]| 0.5698324022346368|
    | [50,67]|0.45083487940630795|
    +--------+-------------------+

我尝试直接在列上使用拆分方法,但由于类型不匹配,它不起作用。

实现这一目标的最佳方法是什么?

【问题讨论】:

    标签: apache-spark apache-spark-sql


    【解决方案1】:

    鉴于schema

    root
     |-- _1: struct (nullable = true)
     |    |-- _1: integer (nullable = false)
     |    |-- _2: integer (nullable = false)
     |-- _2: double (nullable = false)
    

    您可以使用withColumn api如下

    result_winr_table.withColumn("first", $"_1._1")
      .withColumn("second", $"_1._2")
    

    如果不想要原始列,可以使用.drop("_1")

    【讨论】:

    • 感谢@Adheip 接受 :) 如果答案有帮助,您也可以投票
    【解决方案2】:

    如果您有一个在编译时不知道属性名称的复杂结构,您可以这样做:

    case class Foo(a: Int, b: String, c: Boolean)
    val df = Seq( (1, Foo(2, "three", false)), (2, Foo(4, "five", true)) ).toDF("id", "foo")
    
    df.show
    +---+-----------------+
    | id|              foo|
    +---+-----------------+
    |  1|[2, three, false]|
    |  2|  [4, five, true]|
    +---+-----------------+
    
    df.select($"*", $"foo.*").show
    +---+-----------------+---+-----+-----+
    | id|              foo|  a|    b|    c|
    +---+-----------------+---+-----+-----+
    |  1|[2, three, false]|  2|three|false|
    |  2|  [4, five, true]|  4| five| true|
    +---+-----------------+---+-----+-----+
    

    【讨论】:

      【解决方案3】:

      人们希望使用 Spark sql 解决这个问题,就像我一样,以下 sql 查询将有助于在 spark 1.6+ 中展平您的数据框/表:

      sqlContext.sql(s""" select _1["_1"] as col1, _1["_2"] as col2, _2 as col3 from result_winr_table """)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-03-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-03
        • 2021-07-29
        • 1970-01-01
        • 2022-07-06
        • 2017-08-29
        相关资源
        最近更新 更多