python - 使用 python 在 Apache Storm Spout 中打开文件

标签 python apache-storm

我试图让 Apache Storm Spout 从文件中逐行读取。我曾尝试写下这些陈述,但没有成功。它给了我每次只迭代的第一行:

class SimSpout(storm.Spout):
    # Not much to do here for such a basic spout
    def initialize(self, conf, context):
        ## Open the file with read only permit
        self.f = open('data.txt', 'r')
        ## Read the first line 
        self._conf = conf
        self._context = context
        storm.logInfo("Spout instance starting...")

    # Process the next tuple
    def nextTuple(self):
        # check if it reach at the EOF to close it 
        for line in self.f.readlines():
            # Emit a random sentence
            storm.logInfo("Emiting %s" % line)
            storm.emit([line])

# Start the spout when it's invoked
SimSpout().run()

最佳答案

免责声明:由于我无法对此进行测试,因此该答案仅来自检查。

您无法保存在 initialize() 中打开的文件句柄。此编辑保存文件句柄,然后使用保存的文件句柄进行读取。它还修复(我希望)一些看起来错误的缩进。

class SimSpout(storm.Spout):
    # Not much to do here for such a basic spout
    def initialize(self, conf, context):
        ## Open the file with read only permit
        self.f = open('mydata.txt', 'r')
        self._conf = conf
        self._context = context

        storm.logInfo("Spout instance starting...")

    # Process the next tuple
    def nextTuple(self):
        # check if it reach at the EOF to close it
        for line in self.f.readlines():
            # Emit a random sentence
            storm.logInfo("Emiting %s" % line)
            storm.emit([line])

# Start the spout when it's invoked
SimSpout().run()

关于python - 使用 python 在 Apache Storm Spout 中打开文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41790430/

相关文章:

python - 为什么 print 和 f-string 在不同时间执行评估?

javascript - 使用 Python 抓取并登录 Angular webabb

python - 按键对python中的计数器进行排序

maven - KafkaSpout BrokerHosts 错误

web-crawler - 使用 php 在 Storm 中进行非阻塞 HTTP 调用

linux - 在 PATH 中添加 Storm

java - 如何将 storm mongo 库添加为 Maven 依赖项?

python - 哪个是检查请求条件的更好方法?

python - 使用中间层作为输入和输出的 keras 模型