Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

[NUTCH-3017] Allow fast-urlfilter to load from HDFS/S3 and support gzipped input

Julien Nioched1025fd634e7
diff --git a/src/plugin/urlfilter-fast/src/java/org/apache/nutch/urlfilter/fast/FastURLFilter.java b/src/plugin/urlfilter-fast/src/java/org/apache/nutch/urlfilter/fast/FastURLFilter.java
index ffcd0138a..79ad7b6ca 100644
--- a/src/plugin/urlfilter-fast/src/java/org/apache/nutch/urlfilter/fast/FastURLFilter.java
+++ b/src/plugin/urlfilter-fast/src/java/org/apache/nutch/urlfilter/fast/FastURLFilter.java
@@ -20,6 +20,8 @@ import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Multimap;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.fs.FileSystem;
import org.apache.nutch.net.URLFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -27,10 +29,13 @@ import org.slf4j.LoggerFactory;
import java.lang.invoke.MethodHandles;
import java.io.BufferedReader;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
+import java.util.zip.GZIPInputStream;
/**
* Filters URLs based on a file of regular expressions using host/domains
@@ -181,9 +186,23 @@ public class FastURLFilter implements URLFilter {
public void reloadRules() throws IOException {
String fileRules = conf.get(URLFILTER_FAST_FILE);
- try (Reader reader = conf.getConfResourceAsReader(fileRules)) {
- reloadRules(reader);
+
+ InputStream is;
+
+ Path fileRulesPath = new Path(fileRules);
+ if (fileRulesPath.toUri().getScheme() != null) {
+ FileSystem fs = fileRulesPath.getFileSystem(conf);
+ is = fs.open(fileRulesPath);
+ }
+ else {
+ is = conf.getConfResourceAsInputStream(fileRules);
}
+
+ if (fileRules.endsWith(".gz")) {
+ is = new GZIPInputStream(is);
+ }
+
+ reloadRules(new InputStreamReader(is));
}
private void reloadRules(Reader rules) throws IOException {