forked from apache/nutch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DomainStatistics.java
202 lines (167 loc) · 6.87 KB
/
DomainStatistics.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.nutch.util.domain;
import java.io.IOException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.SequenceFileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import org.apache.nutch.crawl.CrawlDatum;
import org.apache.nutch.util.NutchConfiguration;
import org.apache.nutch.util.TimingUtil;
import org.apache.nutch.util.URLUtil;
/**
* Extracts some very basic statistics about domains from the crawldb
*/
public class DomainStatistics extends Configured implements Tool {
private static final Logger LOG = LoggerFactory.getLogger(DomainStatistics.class);
private static final Text FETCHED_TEXT = new Text("FETCHED");
private static final Text NOT_FETCHED_TEXT = new Text("NOT_FETCHED");
public static enum MyCounter {FETCHED, NOT_FETCHED, EMPTY_RESULT};
private static final int MODE_HOST = 1;
private static final int MODE_DOMAIN = 2;
private static final int MODE_SUFFIX = 3;
private int mode = 0;
public int run(String[] args) throws Exception {
if (args.length < 3) {
System.out.println("usage: DomainStatistics inputDirs outDir host|domain|suffix [numOfReducer]");
return 1;
}
String inputDir = args[0];
String outputDir = args[1];
int numOfReducers = 1;
if (args.length > 3) {
numOfReducers = Integer.parseInt(args[3]);
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long start = System.currentTimeMillis();
LOG.info("DomainStatistics: starting at " + sdf.format(start));
int mode = 0;
String jobName = "DomainStatistics";
if(args[2].equals("host")) {
jobName = "Host statistics";
mode = MODE_HOST;
} else if(args[2].equals("domain")) {
jobName = "Domain statistics";
mode = MODE_DOMAIN;
} else if(args[2].equals("suffix")) {
jobName = "Suffix statistics";
mode = MODE_SUFFIX;
}
Configuration conf = getConf();
conf.setInt("domain.statistics.mode", mode);
conf.setBoolean("mapreduce.fileoutputcommitter.marksuccessfuljobs", false);
Job job = new Job(conf, jobName);
job.setJarByClass(DomainStatistics.class);
String[] inputDirsSpecs = inputDir.split(",");
for (int i = 0; i < inputDirsSpecs.length; i++) {
FileInputFormat.addInputPath(job, new Path(inputDirsSpecs[i]));
}
job.setInputFormatClass(SequenceFileInputFormat.class);
FileOutputFormat.setOutputPath(job, new Path(outputDir));
job.setOutputFormatClass(TextOutputFormat.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(LongWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class);
job.setMapperClass(DomainStatisticsMapper.class);
job.setReducerClass(DomainStatisticsReducer.class);
job.setCombinerClass(DomainStatisticsCombiner.class);
job.setNumReduceTasks(numOfReducers);
try {
job.waitForCompletion(true);
} catch (Exception e) {
throw e;
}
long end = System.currentTimeMillis();
LOG.info("DomainStatistics: finished at " + sdf.format(end) + ", elapsed: " + TimingUtil.elapsedTime(start, end));
return 0;
}
static class DomainStatisticsMapper extends Mapper<Text, CrawlDatum, Text, LongWritable> {
int mode = 0;
public void setup(Context context) {
mode = context.getConfiguration().getInt("domain.statistics.mode", MODE_DOMAIN);
}
public void map(Text urlText, CrawlDatum datum, Context context) throws IOException, InterruptedException {
if(datum.getStatus() == CrawlDatum.STATUS_DB_FETCHED
|| datum.getStatus() == CrawlDatum.STATUS_DB_NOTMODIFIED) {
try {
URL url = new URL(urlText.toString());
String out = null;
switch (mode) {
case MODE_HOST:
out = url.getHost();
break;
case MODE_DOMAIN:
out = URLUtil.getDomainName(url);
break;
case MODE_SUFFIX:
out = URLUtil.getDomainSuffix(url).getDomain();
break;
}
if(out.trim().equals("")) {
LOG.info("url : " + url);
context.getCounter(MyCounter.EMPTY_RESULT).increment(1);
}
context.write(new Text(out), new LongWritable(1));
} catch (Exception ex) { }
context.getCounter(MyCounter.FETCHED).increment(1);
context.write(FETCHED_TEXT, new LongWritable(1));
}
else {
context.getCounter(MyCounter.NOT_FETCHED).increment(1);
context.write(NOT_FETCHED_TEXT, new LongWritable(1));
}
}
}
static class DomainStatisticsReducer extends Reducer <Text, LongWritable, LongWritable, Text> {
public void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
long total = 0;
for (LongWritable val : values) {
total += val.get();
}
context.write(new LongWritable(total), key);
}
}
public static class DomainStatisticsCombiner extends Reducer <Text, LongWritable, Text, LongWritable> {
public void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
long total = 0;
for (LongWritable val : values) {
total += val.get();
}
context.write(key, new LongWritable(total));
}
}
public static void main(String[] args) throws Exception {
ToolRunner.run(NutchConfiguration.create(), new DomainStatistics(), args);
}
}