【问题标题】:error in spout logic喷口逻辑错误
【发布时间】:2017-04-04 08:51:51
【问题描述】:

在这个问题上我需要你的帮助。我读到 spout 负责读取数据或准备在 Bolt 中处理。所以我在 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()

对吗?

【问题讨论】:

    标签: python apache-storm


    【解决方案1】:

    您正在编写 Spout,它在 Storm 中的职责是发出元组以供下游螺栓处理。

    Spout 的 nextTuple 职责是在每次调用它时发出一个事件。在您的代码中,您将发出文件中的所有行。如果您的单个元组是单行。您应该在文件中保留一个偏移量并阅读它 偏移线和发射,更新偏移量 = 偏移量 + 1。 像下面的东西

    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
        self._offset = 0
        storm.logInfo("Spout instance starting...")
    
     # Process the next tuple
     def nextTuple(self):
        # check if it reach at the EOF to close it
        with open(self.f) as f:
          f.readlines()[self._offset]
          #Emit a random sentence
          storm.logInfo("Emiting %s" % line)
          storm.emit([line])
        self._offset = self._offset + 1
    

    【讨论】:

    • 非常感谢您的回复,我得到了这个“with open(self.f) as f: TypeError: coercing to Unicode: need string or buffer, file found”
    • 我解决的错误是因为我打开文件两次,但我得到另一个 AttributeError: 'SimSpout' object has no attribute '_offset' 我搜索解决它但没有找到任何帮助
    猜你喜欢
    • 2016-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-17
    • 2013-08-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多