簡體   English   中英

scala.collection.mutable.ArrayBuffer無法轉換為java.lang.Double(Spark)

[英]scala.collection.mutable.ArrayBuffer cannot be cast to java.lang.Double (Spark)

我有一個像這樣的DataFrame:

root
 |-- midx: double (nullable = true)
 |-- future: array (nullable = true)
 |    |-- element: struct (containsNull = true)
 |    |    |-- _1: long (nullable = false)
 |    |    |-- _2: long (nullable = false)

使用此代碼,我試圖將其轉換為以下內容:

val T = withFfutures.where($"midx" === 47.0).select("midx","future").collect().map((row: Row) =>
      Row {
        row.getAs[Seq[Row]]("future").map { case Row(e: Long, f: Long) =>
          (row.getAs[Double]("midx"), e, f)
        }
      }
    ).toList

root
 |-- id: double (nullable = true)
 |-- event: long (nullable = true)
 |-- future: long (nullable = true)

因此,計划是將(事件,未來)數組轉移到以這兩個字段為列的數據幀中。 我正在嘗試將T轉移到這樣的DataFrame中:

val schema = StructType(Seq(
  StructField("id", DoubleType, nullable = true)
  , StructField("event", LongType, nullable = true)
  , StructField("future", LongType, nullable = true)
))

val df = sqlContext.createDataFrame(context.parallelize(T), schema)

但是當我嘗試查看df此錯誤:

java.lang.ClassCastException: scala.collection.mutable.ArrayBuffer cannot be cast to java.lang.Double

一段時間后,我發現了問題所在:首先,列中的結構數組應該強制轉換為Row。 因此,用於構建最終數據框架的最終代碼應如下所示:

val T = withFfutures.select("midx","future").collect().flatMap( (row: Row) =>
    row.getAs[Seq[Row]]("future").map { case Row(e: Long, f: Long) =>
      (row.getAs[Double]("midx") , e, f)
    }.toList
).toList

val all = context.parallelize(T).toDF("id","event","future")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM