Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

Add second pass to Grep demo that sorts by frequency of match.

Douglass Cuttinge60a37594b1c
diff --git a/lib/commons-logging-api-1.0.4.jar b/lib/commons-logging-api-1.0.4.jar
new file mode 100644
index 000000000..ade9a13c7
Binary files /dev/null and b/lib/commons-logging-api-1.0.4.jar differ
diff --git a/src/java/org/apache/nutch/mapReduce/JobClient.java b/src/java/org/apache/nutch/mapReduce/JobClient.java
index 2f2e4162a..d2882a632 100644
--- a/src/java/org/apache/nutch/mapReduce/JobClient.java
+++ b/src/java/org/apache/nutch/mapReduce/JobClient.java
@@ -190,11 +190,12 @@ public class JobClient implements MRConstants {
* Get a filesystem handle. We need this to prepare jobs
* for submission to the MapReduce system.
*/
- private synchronized void obtainFS() throws IOException {
- if (this.fs == null) {
- String fsName = jobSubmitClient.getFilesystemName();
- this.fs = NutchFileSystem.getNamed(fsName);
- }
+ public synchronized NutchFileSystem getFs() throws IOException {
+ if (this.fs == null) {
+ String fsName = jobSubmitClient.getFilesystemName();
+ this.fs = NutchFileSystem.getNamed(fsName);
+ }
+ return fs;
}
/**
@@ -217,9 +218,6 @@ public class JobClient implements MRConstants {
// semantics. (that is, if the job file is deleted right after
// submission, we can still run the submission to completion)
//
- if (fs == null) {
- obtainFS();
- }
// Create a number of filenames in the JobTracker's fs namespace
File submitJobDir = new File(JobConf.getSystemDir(), "submit_" + Integer.toString(Math.abs(r.nextInt()),36));
@@ -231,14 +229,14 @@ public class JobClient implements MRConstants {
// Modify the job file, write to JobTracker's fs
job.setJar(submitJarFile.toString());
NFSDataOutputStream out =
- new NFSDataOutputStream(fs.create(submitJobFile));
+ new NFSDataOutputStream(getFs().create(submitJobFile));
try {
job.write(out);
} finally {
out.close();
}
// Copy jar to JobTracker's fs
- fs.copyFromLocalFile(new File(originalJarPath), submitJarFile);
+ getFs().copyFromLocalFile(new File(originalJarPath), submitJarFile);
//
// Now, actually submit the job (using the submit name)
@@ -268,8 +266,10 @@ public class JobClient implements MRConstants {
* complete. */
public static void runJob(JobConf job) throws IOException {
JobClient jc = new JobClient();
+ boolean error = true;
+ RunningJob running = null;
try {
- RunningJob running = jc.submitJob(job);
+ running = jc.submitJob(job);
String jobId = running.getJobID();
LOG.info("Running job: " + jobId);
while (!running.isComplete()) {
@@ -286,7 +286,11 @@ public class JobClient implements MRConstants {
}
}
LOG.info("Job complete: " + jobId);
+ error = false;
} finally {
+ if (error && (running != null)) {
+ running.killJob();
+ }
jc.close();
}
}
diff --git a/src/java/org/apache/nutch/mapReduce/demo/Grep.java b/src/java/org/apache/nutch/mapReduce/demo/Grep.java
index f4c0109e7..7e9097e33 100644
--- a/src/java/org/apache/nutch/mapReduce/demo/Grep.java
+++ b/src/java/org/apache/nutch/mapReduce/demo/Grep.java
@@ -18,14 +18,18 @@ package org.apache.nutch.mapReduce.demo;
import org.apache.nutch.mapReduce.JobConf;
import org.apache.nutch.mapReduce.JobClient;
import org.apache.nutch.mapReduce.RunningJob;
+import org.apache.nutch.mapReduce.InputFormats;
+import org.apache.nutch.mapReduce.OutputFormats;
import org.apache.nutch.mapReduce.lib.RegexMapper;
+import org.apache.nutch.mapReduce.lib.InverseMapper;
import org.apache.nutch.mapReduce.lib.LongSumReducer;
import org.apache.nutch.io.UTF8;
import org.apache.nutch.io.LongWritable;
import java.io.File;
+import java.util.Random;
/* Extracts matching regexs from input files and counts them. */
public class Grep {
@@ -37,24 +41,48 @@ public class Grep {
System.exit(-1);
}
- JobConf job = new JobConf();
+ File tempDir =
+ new File("grep-temp-"+
+ Integer.toString(new Random().nextInt(Integer.MAX_VALUE)));
- job.setInputDir(new File(args[0]));
- job.setOutputDir(new File(args[1]));
+ JobConf grepJob = new JobConf();
- job.setMapperClass(RegexMapper.class);
- job.set("mapred.mapper.regex", args[2]);
+ grepJob.setNumMapTasks(36);
+ grepJob.setInputDir(new File(args[0]));
+
+ grepJob.setMapperClass(RegexMapper.class);
+ grepJob.set("mapred.mapper.regex", args[2]);
if (args.length == 4)
- job.set("mapred.mapper.regex.group", args[3]);
+ grepJob.set("mapred.mapper.regex.group", args[3]);
- job.setCombinerClass(LongSumReducer.class);
- job.setReducerClass(LongSumReducer.class);
+ grepJob.setCombinerClass(LongSumReducer.class);
+ grepJob.setReducerClass(LongSumReducer.class);
+
+ grepJob.setNumReduceTasks(6);
+ grepJob.setOutputDir(tempDir);
+ grepJob.setOutputFormat(OutputFormats.get("seq"));
+ grepJob.setOutputKeyClass(UTF8.class);
+ grepJob.setOutputValueClass(LongWritable.class);
+
+ JobClient.runJob(grepJob);
+
+ JobConf sortJob = new JobConf();
+
+ sortJob.setNumMapTasks(6);
+
+ sortJob.setInputDir(tempDir);
+ sortJob.setInputFormat(InputFormats.get("seq"));
+ sortJob.setInputKeyClass(UTF8.class);
+ sortJob.setInputValueClass(LongWritable.class);
+
+ sortJob.setMapperClass(InverseMapper.class);
- job.setOutputKeyClass(UTF8.class);
- job.setOutputValueClass(LongWritable.class);
+ sortJob.setNumReduceTasks(1); // write a single file
+ sortJob.setOutputDir(new File(args[1]));
- JobClient.runJob(job);
+ JobClient.runJob(sortJob);
+ new JobClient().getFs().delete(tempDir);
}
}