【问题标题】:Spark 2.0.2 doesn't seem to think that "groupBy" is returning a DataFrameSpark 2.0.2 似乎不认为“groupBy”正在返回一个 DataFrame
【发布时间】:2017-06-06 05:31:06
【问题描述】:

这感觉有点傻,但我正在从 Spark 1.6.1 迁移到 Spark 2.0.2。我使用的是 Databrick CSV 库,现在正在尝试使用内置的 CSV DataFrameWriter

以下代码:

    // Get an SQLContext
    val sqlContext = new SQLContext(sc)
    import sqlContext.implicits._

    var sTS = lTimestampToSummarize.toString()
    val sS3InputPath = "s3://measurements/" + sTS + "/*"

    // Read all measurements - note that all subsequent ETLs will reuse dfRaw
    val dfRaw = sqlContext.read.json(sS3InputPath)

    // Filter just the user/segment timespent records
    val dfSegments = dfRaw.filter("segment_ts <> 0").withColumn("views", lit(1))

    // Aggregate views and timespent per user/segment tuples
    val dfUserSegments : DataFrame = dfSegments.groupBy("company_id", "division_id", "department_id", "course_id", "user_id", "segment_id")
        .agg(sum("segment_ts").alias("segment_ts_sum"), sum("segment_est").alias("segment_est_sum"), sum("views").alias("segment_views"))

    // The following will write CSV files to the S3 bucket
    val sS3Output = "s3://output/" + sTS + "/usersegment/"
    dfUserSegments.write.csv(sS3Output)

返回此错误:

[error] /home/Spark/src/main/scala/Example.scala:75: type mismatch;
[error]  found   : Unit
[error]  required: org.apache.spark.sql.DataFrame
[error]     (which expands to)  org.apache.spark.sql.Dataset[org.apache.spark.sql.Row]
[error]         dfUserSegments.write.csv(sS3Output)
[error]                                 ^
[error] one error found
[error] (compile:compile) Compilation failed
[error] Total time: 2 s, completed Jun 5, 2017 5:00:12 PM

我知道我一定是错误地解释了错误,因为我将 dfUserSegments 明确设置为 DataFrame,但编译器告诉我它是 Unit 类型(无类型)。

感谢任何帮助。

【问题讨论】:

    标签: scala apache-spark dataframe


    【解决方案1】:

    你没有展示整个方法。我猜是因为方法返回类型是DataFrame,但是这个方法中最后一条语句是dfUserSegments.write.csv(sS3Output),而csv的返回类型是Unit

    【讨论】:

      猜你喜欢
      • 2016-02-13
      • 1970-01-01
      • 1970-01-01
      • 2017-06-03
      • 2018-01-13
      • 1970-01-01
      • 2021-11-28
      • 2018-01-31
      • 1970-01-01
      相关资源
      最近更新 更多