我想写一个Spark Streamin App,它使用随机整数的流来计算它们。这是我写的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 App时,连接成功建立。但是我看到一个空输出:
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
-------------------------------------------
我不知道问题是什么,请帮我理解发生了什么?
答案 0 :(得分:0)
解决了,我用“\ 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)