2017-01-22 3 views
1

Apache Storm Spout을 파일별로 한 줄씩 읽으려고합니다. 이 진술을 쓰려고했지만 작동하지 않았습니다. 그것은 나를 첫 번째 줄은 매번 반복했다 :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() 

답변

0

면책 조항 :이 테스트 할 방법이 없기 때문에는,이 대답은 단순히 검사에서 일 것이다.

열린 파일 핸들을 initialize()에 저장하지 못했습니다. 이 편집은 파일 핸들을 저장 한 다음 저장된 파일 핸들을 사용하여 읽습니다. 그것도 수정 (나는 희망을) 잘못 본 indenting.

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

에필로그 : 그것은 유래에 두 개의 관련이없는 질문을 나쁜 형태입니다. 두 번째 질문을 별도의 질문으로 제안하십시오.

+0

감사합니다. 두 번째 질문은 제거했지만 코드는 첫 번째 줄만 입력 했으므로 파일에 많이 들어 있습니다. – user5520049

+0

전체 파일을 수행하는 루프를 추가하도록 편집했습니다. –

+0

도움 주셔서 감사합니다,하지만 java.lang.RuntimeException있어 : backtype.storm.multilang.NoOutputException : 파이프 하위 프로세스가 손상된 것 같습니다! 출력을 읽지 않습니다. 시리얼 예외 : 파일 "simspout.py", 라인 (22) storm.logInfo ("내뿜는 냉온 %의"% 선) ^ IndentationError : 들여 쓰기 블록 – user5520049