运行sparkstreaming的NetworkWordCount不能出现

官网:https://spark.apache.org/docs/2.2.0/streaming-programming-guide.html#points-to-remember-1
代码:

from pyspark import SparkContext
from pyspark.streaming import StreamingContext
sc = SparkContext("local[2]","NetworkWordCount")
ssc = StreamingContext(sc, 1)
lines = ssc.socketTextStream("localhost", 9999)
words = lines.flatMap(lambda line: line.split(" "))
pairs = words.map(lambda word: (word, 1))
wordCounts = pairs.reduceByKey(lambda x, y: x + y)
wordCounts.pprint()
ssc.start()
ssc.awaitTermination()

在一个终端输入:

nc -lk 9999

另起一个终端输入:

spark-submit /home/hadoop01/test1.py localhost 9999

然后再nk终端任意输入文本
spark-submit终端出现能正常接收数据但是不能处理数据,终端反复出现下面的图片内容
在这里插入图片描述
官网解释:

Points to remember
When running a Spark Streaming program locally, do not use “local” or “local[1]” as the master URL.
Either of these means that only one thread will be used for running tasks locally. If you are using
an input DStream based on a receiver (e.g. sockets, Kafka, Flume, etc.), then the single thread will
be used to run the receiver, leaving no thread for processing the received data. Hence, when
running locally, always use “local[n]” as the master URL, where n > number of receivers to run
(see Spark Properties for information on how to set
the master).
Extending the logic to running on a cluster, the number of cores allocated to the Spark Streaming
application must be more than the number of receivers. Otherwise the system will receive data, but not be able to process it.
其意思是
在本地运行Spark Streaming程序时,请勿使用“local”或“local [1]”作为主URL。 这两种方法都意味着只有一个线程将用于本地运行任务。 如果您正在使用基于接收器的输入DStream(例如套接字,Kafka,Flume等),那么将使用单个线程来运行接收器,而不留下用于处理接收数据的线程。 因此,在本地运行时,始终使用“local [n]”作为主URL,其中n>要运行的接收器数量(有关如何设置主服务器的信息,请参阅Spark属性)。
将逻辑扩展到在集群上运行,分配给Spark Streaming应用程序的核心数必须大于接收器数。 否则系统将接收数据,但无法处理数据

但是我的程序使用的是local[2]按道理应该不是这个问题,然后又继续搜,看到一篇博客是修改了虚拟机处理器的设置,然后我也重新修改:
在这里插入图片描述
重新运行,在nk终端输入:

spark-submit终端出现结果

在这里插入图片描述
问题解决!
sparkstreaming需要注意的问题:

  1. 定义上下文后,您必须执行以下操作:
    (1)通过创建输入DStreams来定义输入源。
    (2)通过将转换和输出操作应用于DStream来定义流式计算。
    (3)开始接收数据并使用streamingContext.start()处理它。
    (4)等待使用streamingContext.awaitTermination()停止处理(手动或由于任何错误)。
    (5)可以使用streamingContext.stop()手动停止处理。
  2. Discretized Stream或DStream是Spark Streaming提供的基本抽象。 它表示连续的数据流,可以是从源接收的输入数据流,也可以是通过转换输入流生成的已处理数据流。 它表示连续的数据流,可以是从源接收的输入数据流,也可以是通过转换输入流生成的已处理数据流。
    在内部,DStream由一系列连续的RDD表示,这是Spark对不可变分布式数据集的抽象(有关更多详细信息,请参阅Spark编程指南)。 DStream中的每个RDD都包含来自特定时间间隔的数据
  3. Spark Streaming提供两类内置流媒体源。
    基本来源:StreamingContext API中直接提供的源。 示例:文件系统和套接字连接。
    高级资源:Kafka,Flume,Kinesis等资源可通过额外的实用程序类获得。 这些需要链接额外的依赖关系,如链接部分所述。
    注意:
    Spark Streaming应用程序需要分配足够的内核(或线程,如果在本地运行)来处理接收的数据,以及运行接收器。

猜你喜欢

转载自blog.csdn.net/weixin_37353303/article/details/84031405