Java 在TwitterStorm中使用非JVM语言将真实数据传递到Storms喷口
我很难理解如何将真实数据传递到喷口, 例如: 我有两个文件,它们工作正常:Java 在TwitterStorm中使用非JVM语言将真实数据传递到Storms喷口,java,python,subprocess,apache-storm,Java,Python,Subprocess,Apache Storm,我很难理解如何将真实数据传递到喷口, 例如: 我有两个文件,它们工作正常: #! /usr/bin/env python import os, random, sys, time for i in xrange(50): print("%s\t%s"%(os.getpid(), i)) sys.stdout.flush() time.sleep(random.randint(0,5)) 及 现在想象一下,我想将这些随机线传递到喷口,以便将来处理,我正在尝试以下方法:
#! /usr/bin/env python
import os, random, sys, time
for i in xrange(50):
print("%s\t%s"%(os.getpid(), i))
sys.stdout.flush()
time.sleep(random.randint(0,5))
及
现在想象一下,我想将这些随机线传递到喷口,以便将来处理,我正在尝试以下方法:
从uuid导入uuid4
从选择导入选择
从子流程导入Popen、PIPE
进口风暴
class TwitterSpout(storm.Spout):
def initialize(self, conf, context):
self.pid = os.getpid()
try:
self.p= Popen(['./rand_lines.py'], stdout=PIPE, bufsize=1, close_fds=True, universal_newlines=True)
except OSError, e:
self.log('%s'%e)
sys.exit(1)
而不是下一个倍数:
def nextTuple(self):
timeout = 0.1 # seconds
while self.p:
# remove finished processes from the list
if self.p.poll() is not None: # process ended
self.log ("%s"%self.p.stdout.read()) # read the rest
self.p.stdout.close()
processes.remove(self.p)
# wait until there is something to read
rlist = select([self.p.stdout], [],[], timeout)[0]
# read a line from each process that has output ready
for f in rlist:
self.log ("%s%s"%f.readline()) #NOTE: it can block
msgId = random.randint(0,500)
self.log('MSG IN SPOUT %s\n'%msgId)
storm.emit([f.readline()], id=msgId)
但是这个结构不起作用,我总是出错,皮皮好像坏了。。。或者,如果我尝试这段代码的不同变体,我会阻止这一过程,而Storm永远不会丰富下一个代码。请帮助我解决我的问题,或者如果有人能给我一些例子如何做类似的事情,或者只是一些建议。
谢谢可能有多个问题 while循环-无限循环中没有中断 你给f.readline打了两次电话。您可能打算在每次选择后只调用它一次 为避免阻塞,请在选择后使用data=os.readf.fileno,1024 我不知道在子进程退出之前阻止nextTuple是否可以接受 如果只需从子流程中读取行,则不需要选择: 例如:
# ...
self.lines = iter_lines(sys.executable, '-u', 'rand_lines.py')
#...
def nextTuple(self):
try:
line = next(self.lines).decode('ascii', 'ignore')
except StopIteration as e:
self.exit_status = e.args[0]
else:
storm.emit([line.strip()])
@我能收到你的电子邮件吗?
def iter_lines(*args, DEVNULL=open(os.devnull, 'r+')):
p = Popen(args, stdin=DEVNULL, stdout=PIPE, stderr=DEVNULL,
bufsize=1, close_fds=True)
for line in iter(p.stdout.readline, b''): # expect b'\n' newline
yield line
p.stdout.close()
raise StopIteration(p.wait())
# ...
self.lines = iter_lines(sys.executable, '-u', 'rand_lines.py')
#...
def nextTuple(self):
try:
line = next(self.lines).decode('ascii', 'ignore')
except StopIteration as e:
self.exit_status = e.args[0]
else:
storm.emit([line.strip()])