用python在Apache风暴喷口中打开文件

2024-09-27 07:22:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图让Apache Storm spoutle从一个文件中逐行读取。我试着写这些声明,但没用。它给我的第一行只是每次迭代:

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()

Tags: thetoselfforapacheconfdefcontext
1条回答
网友
1楼 · 发布于 2024-09-27 07:22:52

免责声明:因为我没有办法测试这个问题,所以这个答案只能通过检查。在

无法保存在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()

相关问题 更多 >

    热门问题