【问题标题】:Spark context startup too slowSpark 上下文启动太慢
【发布时间】:2018-01-10 01:47:39
【问题描述】:

我正在使用以下代码以独立模式运行 Apache Spark:

from __future__ import division
from pyspark import SparkContext
import sys
sc = SparkContext()
sc.setLogLevel("ERROR")

data = sc.textFile(sys.argv[1])
words = data.flatMap(lambda x: x.split())
word_cant = words.map(lambda x: (x,1))
total = words.count()

cant_by_word = word_cant.reduceByKey(lambda x, y: x + y)
freq = cant_by_word.map(lambda x: (x[0],x[1]/total))
sortedFreq = freq.sortBy(lambda x: x[1], False)
out = freq.takeOrdered(5, lambda s: -1*s)
print('output', out)

无论输入多小,此示例的运行时间都大于 8 秒。我试过在创建 Spark 后停止程序 上下文和运行时间约为 5 秒。这是我使用 ptime 测量的一个运行示例:

ptime python freq_words.py sample.txt

ptime 1.0 for Win32, Freeware - http://www.pc-tools.net/
Copyright(C) 2002, Jem Berkes <jberkes@pc-tools.net>

===  python freq_words.py sample.txt ===
Setting default log level to "WARN".
To adjust logging level use sc.setLogLevel(newLevel). For SparkR, use setLogLevel(newLevel).

('output', [(u'tiene', 0.06666666666666667), (u'este', 0.06666666666666667), (u'veces', 0.13333333333333333), (u'la', 0.13333333333333333), (u'texto', 0.13333333333333333)])

Execution time: 9.046 s

C:\Users\user\Desktop\test>SUCCESS: The process with PID 5620 (child process of PID 6092) has been terminated.
SUCCESS: The process with PID 6092 (child process of PID 5776) has been terminated.
SUCCESS: The process with PID 5776 (child process of PID 1440) has been terminated.

有没有办法加快速度?或者在第二次运行时重用 spark 上下文?

【问题讨论】:

    标签: python windows apache-spark pyspark


    【解决方案1】:

    Spark 是重量级处理引擎,并非专为低延迟处理而设计。对于典型的 Spark 工作来说,恒定的 8s 开销确实不是问题。

    但要回答你的问题:

    一种加快速度的方法?

    不要使用 MS Windows。 Spark,尤其是 PySpark,必须使用非常低效的逻辑来解决操作系统的限制。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-20
      • 2019-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-24
      相关资源
      最近更新 更多