我建立了一个基本的网页解析器,它使用hadoop来把URL传递给多个线程。这很好地工作,直到我到达输入文件的末尾,Hadoop会在线程仍在运行时声明自己已完成。这会导致org.apache.hadoop.fs.FSError错误:java.io.IOException:Stream Closed。无论如何要保持这个流线足够长的时间来完成线程吗? (我可以用合理的准确度预测线程在单个url上花费的最大时间量)。如何防止hadoop流关闭?
继承人我是如何执行的线程
public static class Map extends MapReduceBase implements
Mapper<LongWritable, Text, Text, Text> {
private Text word = new Text();
private URLPile pile = new URLPile();
private MSLiteThread[] Threads = new MSLiteThread[16];
private boolean once = true;
@Override
public void map(LongWritable key, Text value,
OutputCollector<Text, Text> output, Reporter reporter) {
String url = value.toString();
StringTokenizer urls = new StringTokenizer(url);
Config.LoggerProvider = LoggerProvider.DISABLED;
System.out.println("In Mapper");
if (once) {
for (MSLiteThread thread : Threads) {
System.out.println("created thread");
thread = new MSLiteThread(pile);
thread.start();
}
once = false;
}
while (urls.hasMoreTokens()) {
try {
word.set(urls.nextToken());
String currenturl = word.toString();
pile.addUrl(currenturl, output);
} catch (Exception e) {
e.printStackTrace();
continue;
}
}
}
螺纹自己得到这样
public void run(){
try {
sleep(3000);
while(!done()){
try {
System.out.println("in thread");
MSLiteURL tempURL = pile.getNextURL();
String currenturl = tempURL.getURL();
urlParser.parse(currenturl);
urlText.set("");
titleText.set(currenturl+urlParser.export());
System.out.println(urlText.toString()+titleText.toString());
tempURL.getOutput().collect(urlText, titleText);
pile.doneParsing();
sleep(30);
} catch (Exception e) {
pile.doneParsing();
e.printStackTrace();
continue;
}
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Thread done");
}
的网址,并在urlpile相关的方法是
public synchronized void addUrl(String url,OutputCollector<Text, Text> output) throws InterruptedException {
while(queue.size()>16){
System.out.println("queue full");
wait();
}
finishedParcing--;
queue.add(new MSLiteURL(output,url));
notifyAll();
}
private Queue<MSLiteURL> queue = new LinkedList<MSLiteURL>();
private int sent = 0;
private int finishedParcing = 0;
public synchronized MSLiteURL getNextURL() throws InterruptedException {
notifyAll();
sent++;
//System.out.println(queue.peek());
return queue.remove();
}
那dec如果它真的超时,这个任务就会失败,这并不是我想要的,但它似乎是正确的。 – Chenab
啊!我可能错过了问题中的一些细节。你是说你有从单个地图任务运行的线程,并且当地图完成处理它的输入时,Hadoop退出了吗? –
或多或少。线程一段时间来处理每个输入,这就是为什么我有更多的一个。然而,一旦hadoop声明地图任务完成,线程就没有放置输出的地方。 – Chenab