【问题标题】:How to count in Hadoop? [duplicate]如何在 Hadoop 中计数? [复制]
【发布时间】:2021-04-16 14:37:56
【问题描述】:

我有一个巨大的标题列表。我想计算整个数据集中的每个标题。例如:

`title`

A
b
A
c
c
c

输出:

title fre
    A   2
    b   1
    c   3

我正在寻找一种在 Hadoop 中使用 reduce 函数的快速方法。我知道以下方式:

import pyspark.sql.functions as f
df.groupBy('title').agg(f.count('*').alias('count')).show()

我还需要获得出现次数少于 10 次的标题。

【问题讨论】:

    标签: apache-spark hadoop pyspark count reduce


    【解决方案1】:

    如果你想使用 RDD,你可以试试这个代码:

    grouped_rdd = (df.rdd.map(lambda r: (r[0], 1))
                     .reduceByKey(lambda x, y: x + y)
                     .sortBy(lambda r: -r[1])    # descending order; remove - if ascending.
                     .take(10)
                  )
    
    grouped_df = spark.createDataFrame(grouped_rdd, ['title', 'count'])
    

    但我不确定这是否会比df.groupBy('title').count() 更快。

    【讨论】:

    • Py4JJavaError: An error occurred while calling z:org.apache.spark.api.python.PythonRDD.collectAndServe. : org.apache.spark.SparkException: Job aborted due to stage failure: Task 0 in stage 101.0 failed 1 times, most recent failure: Lost task 0.0 in stage 101.0 (TID 611, 192.168.1.102, executor driver): org.apache.hadoop.fs.FSError: java.io.IOException: Device not configured
    • @elham 非常奇怪的错误,应该与代码无关。尝试重新启动您的应用程序
    猜你喜欢
    • 1970-01-01
    • 2013-08-26
    • 2011-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多