pyspark无法找到KafkaUtils.createDirectStream

时间:2016-05-19 15:09:13

标签: python-2.7 apache-spark apache-kafka pyspark kafka-python

我有以下pyspark脚本,它假设连接到本地kafka集群:

from pyspark import SparkConf, SparkContext

from operator import add
import sys
from pyspark.streaming import StreamingContext
from pyspark.streaming.kafka import KafkaUtils
## Constants
APP_NAME = "PythonStreamingDirectKafkaWordCount"
##OTHER FUNCTIONS/CLASSES

def main():
    sc = SparkContext(appName="PythonStreamingDirectKafkaWordCount")
    ssc = StreamingContext(sc, 2)

    brokers, topic = sys.argv[1:]
    kvs = KafkaUtils.createDirectStream(ssc, [topic], {"metadata.broker.list": brokers})
    lines = kvs.map(lambda x: x[1])
    counts = lines.flatMap(lambda line: line.split(" ")) \
        .map(lambda word: (word, 1)) \
        .reduceByKey(lambda a, b: a+b)
    counts.pprint()

    ssc.start()
    ssc.awaitTermination()
if __name__ == "__main__":

   main()

当我运行它时,我收到以下错误:

File "/home/ubuntu/spark-1.3.0-bin-hadoop2.4/hello1.py", line 16, in main
    kvs = KafkaUtils.createDirectStream(ssc, [topic], {"metadata.broker.list": brokers})
AttributeError: type object 'KafkaUtils' has no attribute 'createDirectStream'

如何才能访问KafkaUtils.createDirectStream?

1 个答案:

答案 0 :(得分:1)

您使用Spark 1.3.0并在Spark 1.4.0中引入了createDirectStream的Python版本。 Spark 1.3仅提供Scala和Java实现。

如果您想使用直接流,则必须更新Spark安装。