您好我创建了一个Storm程序,它使用spout类逐行读取文本文件input.txt
并将这些元组发送到bolt,在bolt类中我想将元组写入output.txt
。我几乎已经完成但问题是风暴在输出文件中多次写入。查看我的input.txt
和output.txt
文件
INPUT.TXT
Kaveen,bigdata,29
varadha,cshart,30
vignesh,winrt,21
输出
varadha,cshart,30
vignesh,winrt,21
Kaveen,bigdata,29
varadha,cshart,30
Kaveen,bigdata,29
Kaveen,bigdata,29
vignesh,winrt,21
我想将输出文件写成与inputfile类似,但顺序不是问题。我如何实现这一目标,请帮助我。
答案 0 :(得分:0)
在将内容写入output.txt
文件之前,只需打开附加模式中的output.txt
即可。每当发生写入语句时,只需通过检查文件中的重复记录将内容附加到此output.txt
。
答案 1 :(得分:0)
看起来您有多个spout实例读取输入文件,导致输出中出现重复记录。
答案 2 :(得分:0)
我遇到了同样的问题,因此找到了解决方法。
在Spout中,当您正在阅读文件时,在 open()方法中创建 FileReader对象,因为那时它会初始化工作节点的reader对象。 并在nextTuple()方法中使用该对象 (有一个喷口和一个螺栓)
以下是open()和nextTuple方法的代码:
public void open(Map conf, TopologyContext context,
SpoutOutputCollector collector) {
try {
this.context = context;
File file = new File(filename);
this.fileReader = new FileReader(file);
} catch (FileNotFoundException e) {
throw new RuntimeException("Error reading file ["+ filename + "]");
}
this.collector = collector;
}
public void nextTuple() {
/**
* The nextuple it is called forever, so if we have been readed the file
* we will wait and then return
*/
if (completed) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// Do nothing
}
return;
}
String str;
BufferedReader reader = new BufferedReader(fileReader);
try {
// Read all lines
while ((str = reader.readLine()) != null) {
/**
* By each line emit a new value with the line as a their
*/
this.collector.emit(new Values(str), str);
}
} catch (Exception e) {
throw new RuntimeException("Error reading tuple", e);
} finally {
completed = true;
}
}
输出:
Kaveen,bigdata,29
varadha,cshart,30
vignesh,winrt,21
另一个问题可能是:
您可能正在为spout运行多个实例,这可能导致重复发送流,或者文件以附加模式写入。