gpt4 book ai didi

python - Spark Streaming 应用程序无法在端口上接收字符串

转载 作者:太空宇宙 更新时间:2023-11-03 15:00:51 24 4
gpt4 key购买 nike

我想编写一个 Spark Streamin 应用程序,它采用带有随机整数的流并对它们进行计数。这是我编写的 Spark 应用程序:

from pyspark import SparkContext
from pyspark.streaming import StreamingContext

sc = SparkContext("local[2]", "IntegerCount") # 2 threads, app name
ssc = StreamingContext(sc, 1) # sc, time interval for batch update.

nums = ssc.socketTextStream("localhost", 8000) # stream data from TCP; source, port

# create key,value pairs
tests = nums.map(lambda num: (int(num), 1))

# Count each integer in each batch
intCounts = tests.reduceByKey(lambda x, y: x + y)

# Print
intCounts.pprint()

ssc.start() # Start the computation
ssc.awaitTermination() # Wait for the computation to terminate

我使用该 Server.py 向端口 8000 提供随机数:

import socket
from random import randint

host = 'localhost'
port = 8000
address = (host, port)

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(address)
server_socket.listen(5)


print "Listening for client . . ."
conn, address = server_socket.accept()
print "Connected to client at ", address
#pick a large output buffer size because i dont necessarily know how big the incoming packet is
while True:
output = str(randint(0, 10))
conn.send(output)

当我运行 Server.py 和 Spark 应用程序时,连接成功建立。但是我看到一个空输出:

Using Spark's default log4j profile: org/apache/spark/log4j-defaults.properties
Setting default log level to "WARN".
To adjust logging level use sc.setLogLevel(newLevel). For SparkR, use setLogLevel(newLevel).
-------------------------------------------
Time: 2017-07-16 22:36:11
-------------------------------------------

-------------------------------------------
Time: 2017-07-16 22:36:12
-------------------------------------------

我不知道问题出在哪里,请帮我理解发生了什么?

最佳答案

解决了,我发送了带有“\n”的字符串并且它起作用了。

import socket
from random import randint

host = 'localhost'
port = 8000
address = (host, port)

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(address)
server_socket.listen(5)


print "Listening for client . . ."
conn, address = server_socket.accept()
print "Connected to client at ", address
#pick a large output buffer size because i dont necessarily know how big the incoming packet is
while True:
output = str(randint(0, 10)) + "\n" ### THAT IS THE FIX.
conn.send(output)

关于python - Spark Streaming 应用程序无法在端口上接收字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45132636/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com