我应该在文件中保留一个偏移量,读取偏移线并发射,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
1条答案
按热度按时间g6ll5ycj1#
您正在此行中打开一个文件
并尝试打开文件句柄而不是此行中的同一个文件
相反,在
nextTuple
,只需使用self.f
而不是f