PySpark .groupBy()和.count()在相对较小的Dataframe

时间:2017-01-12 11:19:52

标签: python-3.x apache-spark pyspark spark-dataframe

好的,让我先描述一下我如何创建Dataframe及其中的内容。

我为这些HTML文档提供了一组gziped HTML文档和一组gziped元数据

对于两者,我提供了RDDs的路径列表,如下所示:

Wet_Paths_RDD = sc.parallelize(Wet_Paths)
Wet_RDD = Wet_Paths_RDD.map(open_wet_filelist).flatMap(split_wetfiles)

我准备两个RDD的方式如下:

(k,(some,other,values))

然后我将我的内容RDD加入我的元数据RDD,如下所示:

Wat_Wet_RDD = Wat_RDD.join(Wet_RDD)

然后我解压缩现在相对复杂的元组,并做其他事情的语言检测。我必须加入RDDs,因为到目前为止,我的所有字符串都表示为byte strings,无法在数据帧中表示。

Wat_Wet_RDD = Wat_Wet_RDD.map(unpack_wat_wet_tuple_and_decoding_and_langdetect)

然后我将加入的RDD转移到Dataframe

wat_wet_schema = StructType([
    StructField("URI", StringType(), True),
    StructField("Links", StringType(), True),
    StructField("N_Links", IntegerType(), True),
    StructField("Content_type", StringType(), True),
    StructField("Original_Encoding", StringType(), True),
    StructField("Content", StringType(), True),
    StructField("Language", StringType(), True),
    StructField("Language_confidence", IntegerType(), True),
])

WatWet_DF = sqlContext.createDataFrame(Wat_Wet_RDD, schema=wat_wet_schema)

并看一下:

print(WatWet_DF.show(20))

到目前为止,一切都需要24分钟,但下一步是:

print(WatWet_DF.groupBy(WatWet_DF.Language).count().orderBy(desc('count')).show(100))

我在24小时后中止,没有一个任务在这个阶段解决了。

目前我在一个测试linux VM上运行集群。 VM有4个核心,同时运行Master和Worker。工人有4名刽子手,每人有3.5G内存。 Dataframe应包含大约100万行。 Apache Spark版本是2.1.0并使用python 3.5。该虚拟机运行在带有24G内存的Xeon W3680 6(v12)内核之上。

1 个答案:

答案 0 :(得分:0)

好的,所以我发现为什么Array<T>.count()在此数据集上花费的时间比.groupBy()长得多。原因是,.show().count()提供所有结果,此处.groupBy()在地图阶段执行的函数需要应用于整个数据集。要使Wat_Wet_RDD.map(unpack_wat_wet_tuple_and_decoding_and_langdetect)提供结果,这些函数只需要应用于整个数据集的子集,从而更快地提供结果。现在,地图阶段.show()中包含一些非常昂贵的函数,导致计算时间非常长,尤其是在Wat_Wet_RDD.map(unpack_wat_wet_tuple_and_decoding_and_langdetect).count().groupBy()进行比较时。

相关问题