需要字符串或缓冲区,找到文件

ubof19bj  于 2021-06-21  发布在  Storm
关注(0)|答案(1)|浏览(321)

我应该在文件中保留一个偏移量,读取偏移线并发射,update offset=offset+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
g6ll5ycj

g6ll5ycj1#

您正在此行中打开一个文件

self.f = open('data.txt', 'r')

并尝试打开文件句柄而不是此行中的同一个文件

with open(self.f) as f:

相反,在 nextTuple ,只需使用 self.f 而不是 f

相关问题