【问题标题】:Transformed DStream in pyspark gives error when pprint called on it当调用 pprint 时,pyspark 中的转换后的 DStream 会出错
【发布时间】:2017-05-19 22:05:12
【问题描述】:

我正在通过 PySpark 探索 Spark Streaming,当我尝试将 transform 函数与 take 一起使用时遇到错误。

我可以通过transform 和pprint 结果成功地使用sortBy 对抗DStream。

author_counts_sorted_dstream = author_counts_dstream.transform\
  (lambda foo:foo\
   .sortBy(lambda x:x[0].lower())\
   .sortBy(lambda x:x[1],ascending=False))
author_counts_sorted_dstream.pprint()

但是如果我按照相同的模式使用take 并尝试pprint 它:

top_five = author_counts_sorted_dstream.transform\
  (lambda rdd:rdd.take(5))
top_five.pprint()

作业因

而失败
Py4JJavaError: An error occurred while calling o25.awaitTermination.
: org.apache.spark.SparkException: An exception was raised by Python:
Traceback (most recent call last):
  File "/usr/local/spark/python/pyspark/streaming/util.py", line 67, in call
    return r._jrdd
AttributeError: 'list' object has no attribute '_jrdd'

你可以在the notebook here看到完整的代码和输出。

我做错了什么?

【问题讨论】:

    标签: apache-spark pyspark spark-streaming dstream


    【解决方案1】:

    传递给transform 的函数应该从RDD 转换为RDD。如果你使用一个动作,比如take,你必须将结果转换回RDD:

    sc: SparkContext = ...
    
    author_counts_sorted_dstream.transform(
      lambda rdd: sc.parallelize(rdd.take(5))
    )
    

    相比之下,RDD.sortBy 使用的是转换(返回 RDD),因此不需要进一步并行化。

    附注以下功能:

    lambda foo: foo \
        .sortBy(lambda x:x[0].lower()) \
        .sortBy(lambda x:x[1], ascending=False)
    

    没有多大意义。请记住,Spark 按 shuffle 排序,因此它不稳定。如果要按多个字段排序,则应使用复合键,例如:

    lambda x: (x[0].lower(), -x[1])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-08
      • 1970-01-01
      • 1970-01-01
      • 2018-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多