简体   繁体   English

Spark - 如何在没有 RDD 的情况下进行字数统计

[英]Spark - How to word count without RDD

It looks RDD is to be removed from Spark.看起来 RDD 将从 Spark 中删除。

Announcement: DataFrame-based API is primary API 公告:基于 DataFrame 的 API 是主要的 API

The RDD-based API is expected to be removed in Spark 3.0基于RDD的API有望在Spark 3.0中移除

Then, how to implement programs like word count in Spark?那么,如何在Spark中实现字数统计这样的程序呢?

The data you manipulate as tuples using RDD api can be thought of and manipulated as columns/fields in a SQL like manner using DataFrame api.您使用 RDD api 作为元组操作的数据可以被认为和使用 SQL 类似方式使用 DataFrame api 作为列/字段进行操作。

df.withColumn("word", explode(split(col("lines"), " ")))
  .groupBy("word")
  .count()
  .orderBy(col("count").desc())
  .show()
+---------+-----+
|     word|count|
+---------+-----+
|      foo|    5|
|      bar|    2|
|     toto|    1|
...
+---------+-----+

Notes:笔记:

  • This code snippet requires necessary imports from org.apache.spark.sql.functions此代码片段需要从org.apache.spark.sql.functions进行必要的导入
  • Relevant examples can be found in this question 's answers.可以在这个问题的答案中找到相关示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM