Apache Nutch is an extensible and scalable web crawler
Fix for NUTCH-2037: Job endpoint to support Indexing from the REST API contributed by Sujen Shah <[email protected]> this closes #29
Chris Mattmannb754b9076174
diff --git a/CHANGES.txt b/CHANGES.txtindex 299300fe0..c90e9400d 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -2,6 +2,8 @@ Nutch Change LogNutch Current Development 1.11-SNAPSHOT+* NUTCH-2037 Job endpoint to support Indexing from the REST API (Sujen Shah via mattmann)+* NUTCH-2017 Remove debug log from MimeUtil (snagel)* NUTCH-2027 seed list REST endpoint for Nutch 1.10 (Asitang Mishra via mattmann)diff --git a/src/java/org/apache/nutch/indexer/IndexingJob.java b/src/java/org/apache/nutch/indexer/IndexingJob.javaindex fd1ecac32..b38779131 100644--- a/src/java/org/apache/nutch/indexer/IndexingJob.java+++ b/src/java/org/apache/nutch/indexer/IndexingJob.java@@ -16,16 +16,20 @@*/package org.apache.nutch.indexer;+import java.io.File;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.ArrayList;+import java.util.Arrays;+import java.util.Comparator;+import java.util.HashMap;import java.util.List;import java.util.Locale;+import java.util.Map;import java.util.Random;import org.apache.nutch.segment.SegmentChecker;import org.apache.hadoop.conf.Configuration;-import org.apache.hadoop.conf.Configured;import org.apache.hadoop.fs.FileStatus;import org.apache.hadoop.fs.FileSystem;import org.apache.hadoop.fs.Path;@@ -40,6 +44,7 @@ import org.apache.hadoop.util.ToolRunner;import org.apache.nutch.util.HadoopFSUtil;import org.apache.nutch.util.NutchConfiguration;import org.apache.nutch.util.NutchJob;+import org.apache.nutch.util.NutchTool;import org.apache.nutch.util.TimingUtil;import org.slf4j.Logger;import org.slf4j.LoggerFactory;@@ -48,7 +53,7 @@ import org.slf4j.LoggerFactory;* Generic indexer which relies on the plugins implementing IndexWriter**/-public class IndexingJob extends Configured implements Tool {+public class IndexingJob extends NutchTool implements Tool {public static Logger LOG = LoggerFactory.getLogger(IndexingJob.class);@@ -197,4 +202,88 @@ public class IndexingJob extends Configured implements Tool {new IndexingJob(), args);System.exit(res);}+++ //Used for REST API+ @Override+ public Map<String, Object> run(Map<String, String> args, String crawlId) throws Exception {+ boolean noCommit = false;+ boolean deleteGone = false;+ boolean filter = false;+ boolean normalize = false;+ boolean isSegment = false;+ String params= null;+ Configuration conf = getConf();++ String crawldb = crawlId+"/crawldb";+ Path crawlDb = new Path(crawldb);+ Path linkDb = null;+ List<Path> segments = new ArrayList<Path>();++ if(args.containsKey("linkdb")){+ linkDb = new Path(crawlId+"/linkdb");+ }++ if(args.containsKey("dir")){+ isSegment = true;+ Path dir = new Path(crawlId+"/segments");+ FileSystem fs = dir.getFileSystem(getConf());+ FileStatus[] fstats = fs.listStatus(dir,+ HadoopFSUtil.getPassDirectoriesFilter(fs));+ Path[] files = HadoopFSUtil.getPaths(fstats);+ for (Path p : files) {+ if (SegmentChecker.isIndexable(p,fs)) {+ segments.add(p);+ }+ }+ }++ if(args.containsKey("segments")){+ isSegment = true;+ String listOfSegments[] = args.get("segments").split(",");+ for(String s: listOfSegments){+ segments.add(new Path(s));+ }+ }++ if(!isSegment){+ String segment_dir = crawlId+"/segments";+ File segmentsDir = new File(segment_dir);+ File[] segmentsList = segmentsDir.listFiles();+ Arrays.sort(segmentsList, new Comparator<File>(){+ @Override+ public int compare(File f1, File f2) {+ if(f1.lastModified()>f2.lastModified())+ return -1;+ else+ return 0;+ }+ });++ Path segment = new Path(segmentsList[0].getPath());+ segments.add(segment);+ }++ if(args.containsKey("noCommit")){+ noCommit = true;+ }+ if(args.containsKey("deleteGone")){+ deleteGone = true;+ }+ if(args.containsKey("normalize")){+ normalize = true;+ }+ if(args.containsKey("filter")){+ filter = true;+ }+ if(args.containsKey("params")){+ params = args.get("params");+ }+ setConf(conf);+ index(crawlDb, linkDb, segments, noCommit, deleteGone, params, filter,+ normalize);+ Map<String, Object> results = new HashMap<String, Object>();+ results.put("result", 0);+ return results;+ }}diff --git a/src/java/org/apache/nutch/service/impl/JobFactory.java b/src/java/org/apache/nutch/service/impl/JobFactory.javaindex b427179c7..a74e36214 100644--- a/src/java/org/apache/nutch/service/impl/JobFactory.java+++ b/src/java/org/apache/nutch/service/impl/JobFactory.java@@ -28,6 +28,7 @@ import org.apache.nutch.crawl.Generator;import org.apache.nutch.crawl.Injector;import org.apache.nutch.crawl.LinkDb;import org.apache.nutch.fetcher.Fetcher;+import org.apache.nutch.indexer.IndexingJob;import org.apache.nutch.parse.ParseSegment;import org.apache.nutch.util.NutchTool;@@ -42,6 +43,7 @@ public class JobFactory {typeToClass.put(JobType.GENERATE, Generator.class);typeToClass.put(JobType.FETCH, Fetcher.class);typeToClass.put(JobType.PARSE, ParseSegment.class);+ typeToClass.put(JobType.INDEX, IndexingJob.class);typeToClass.put(JobType.UPDATEDB, CrawlDb.class);typeToClass.put(JobType.INVERTLINKS, LinkDb.class);typeToClass.put(JobType.DEDUP, DeduplicationJob.class);