【发布时间】:2016-09-23 20:57:43
【问题描述】:
我在https://databricks.com/blog/2015/02/17/introducing-dataframes-in-spark-for-large-scale-data-science.html 看到了一个用 Python 编写的 Dataframes 教程。我正在尝试将其翻译成 Scala。
他们有以下代码:
df = context.load("/path/to/people.json")
# RDD-style methods such as map, flatMap are available on DataFrames
# Split the bio text into multiple words.
words = df.select("bio").flatMap(lambda row: row.bio.split(" "))
# Create a new DataFrame to count the number of words
words_df = words.map(lambda w: Row(word=w, cnt=1)).toDF()
word_counts = words_df.groupBy("word").sum()
所以,我首先将csv 中的数据读入数据框df,然后我有:
val title_words = df.select("title").flatMap { row =>
row.getAs[String("title").split(" ") }
val title_words_df = title_words.map( w => Row(w,1) ).toDF()
val word_counts = title_words_df.groupBy("word").sum()
但我不知道:
如何将字段名称分配给以 val title_words_df = ... 开头的行中的行
我遇到错误“toDF 的值不是 org.apache.spark.rdd.RDD[org.apache.spark.sql.Row] 的成员”
提前感谢您的帮助。
【问题讨论】:
标签: python scala apache-spark apache-spark-sql