出5道关于Spark streaming相关的几道程序设计题并给出python答案
- 编写一个Spark streaming程序,从Kafka主题中读取数据,并计算每个单词的出现次数。
from pyspark.streaming.kafka import KafkaUtils
from pyspark.streaming import StreamingContext
ssc = StreamingContext(sparkContext, 10)
kafka_params = {"metadata.broker.list": "localhost:9092"}
kafka_stream = KafkaUtils.createDirectStream(ssc, ["topic"], kafka_params)
words = kafka_stream.flatMap(lambda line: line[1].split(" "))
word_counts = words.map(lambda word: (word, 1)).reduceByKey(lambda a, b: a + b)
word_counts.pprint()
ssc.start()
ssc.awaitTermination()
- 编写一个Spark streaming程序,从Twitter实时流中读取数据,并计算每个用户的推文数量。
from pyspark.streaming.twitter import TwitterUtils
from pyspark.streaming import StreamingContext
ssc = StreamingContext(sparkContext, 10)
twitter_stream = TwitterUtils.createStream(ssc)
tweets = twitter_stream.map(lambda tweet: (tweet["user"]["screen_name"], 1))
tweet_counts = tweets.reduceByKey(lambda a, b: a + b)
tweet_counts.pprint()
ssc.start()
ssc.awaitTermination()
- 编写一个Spark streaming程序,从HDFS中读取数据,并计算每个小时的访问量。
from pyspark.streaming import StreamingContext
ssc = StreamingContext(sparkContext, 3600)
logs = ssc.textFileStream("hdfs://localhost:9000/logs")
hourly_counts = logs.map(lambda line: (line.split(" ")[1][:13], 1)).reduceByKey(lambda a, b: a + b)
hourly_counts.pprint()
ssc.start()
ssc.awaitTermination()
- 编写一个Spark streaming程序,从TCP socket中读取数据,并计算每个IP地址的访问次数。
from pyspark.streaming import StreamingContext
ssc = StreamingContext(sparkContext, 10)
logs = ssc.socketTextStream("localhost", 9999)
ips = logs.map(lambda line: line.split(" ")[0])
ip_counts = ips.map(lambda ip: (ip, 1)).reduceByKey(lambda a, b: a + b)
ip_counts.pprint()
ssc.start()
ssc.awaitTermination()
- 编写一个Spark streaming程序,从Flume中读取数据,并计算每个URL的访问次数。
from pyspark.streaming import StreamingContext
ssc = StreamingContext(sparkContext, 10)
logs = ssc.flumeStream("localhost", 9999)
urls = logs.map(lambda event: event[1]["body"].split(" ")[6])
url_counts = urls.map(lambda url: (url, 1)).reduceByKey(lambda a, b: a + b)
url_counts.pprint()
ssc.start()
ssc.awaitTermination()
``
原文地址: http://www.cveoy.top/t/topic/fQXJ 著作权归作者所有。请勿转载和采集!