【问题标题】:Spark - Convert RDD[Vector] to DataFrame with variable columnsSpark - 将 RDD[Vector] 转换为具有可变列的 DataFrame
【发布时间】:2017-08-02 14:03:51
【问题描述】:

使用 scala/spark 1.6 概括从 RDD[Vector] 到 DataFrame 的转换的最佳解决方案是什么。 输入是不同的 RDD[Vector]。 Vector 中的列数可以从 1 到 n 用于不同的 RDD。

我尝试使用 shapeless 库,它们需要声明的列号和类型。 ES:

val df = rddVector.map(_.toArray.toList)
  .collect  {
          case t: List[Double] if t.length == 3 => t.toHList[Double :: Double :: Double :: HNil].get.tupled.productArity
  }
  .toDF( "column_1", "column_2", "column_3" )

谢谢!

【问题讨论】:

标签: scala apache-spark apache-spark-mllib apache-spark-1.6


【解决方案1】:

这对我有用。

  // Create a vector rdd
  val vectorRDD = sc.parallelize(Seq(Seq(123L, 345L), Seq(567L, 789L), Seq(567L, 789L, 233334L))).
    map(s => Vectors.dense(s.toSeq.map(_.toString.toDouble).toArray))

  // Calculate the maximum length of the vector to create a schema 
  val vectorLength = vectorRDD.map(x => x.toArray.length).max()

  // create the dynamic schema
  var schema = new StructType()
  var i = 0
  while (i < vectorLength) {
    schema = schema.add(StructField(s"val${i}", DoubleType, true))
    i = i + 1
  }

  // create a rowRDD variable and make each row have the same arity 
  val rowRDD = vectorRDD.map { x =>
    var row = new Array[Double](vectorLength)
    val newRow = x.toArray

    System.arraycopy(newRow, 0, row, 0, newRow.length);

    println(row.length)

    Row.fromSeq(row)
  }

  // create your dataframe
  val dataFrame = sqlContext.createDataFrame(rowRDD, schema)

输出:

 root
 |-- val0: double (nullable = true)
 |-- val1: double (nullable = true)
 |-- val2: double (nullable = true)

+-----+-----+--------+
| val0| val1|    val2|
+-----+-----+--------+
|123.0|345.0|     0.0|
|567.0|789.0|     0.0|
|567.0|789.0|233334.0|
+-----+-----+--------+

【讨论】:

  • 谢谢,在这个解决方案中你必须创建一个固定的模式。我不知道架构。架构是可变的。我的 Spark 版本是 1.6,没有 2.0。
  • 我已经更新了答案以适应您提供的条件。它不是最简洁的解决方案,但可以工作:)
猜你喜欢
  • 2017-06-02
  • 2018-03-23
  • 1970-01-01
  • 2018-03-05
  • 1970-01-01
  • 1970-01-01
  • 2017-06-13
  • 2018-10-21
  • 1970-01-01
相关资源
最近更新 更多