This repository has been archived by the owner on Jul 10, 2019. It is now read-only.
/
DocumentFilter.java
239 lines (201 loc) · 8.36 KB
/
DocumentFilter.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/**
* 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 com.digitalpebble.behemoth;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.MapWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Filters used by Mappers / Reducers to skip Behemoth documents based on their
* metadata. Can have either positive or negative filters but not both. The
* filters values are regular expressions and the document will be kept or
* skipped if it matches ANY of the filters in OR mode or all the constraints if
* document.filter.md.mode is set to 'AND'. It can filter based on the document
* URL and or mime-type using regular expressions.
**/
public class DocumentFilter {
private static final Logger LOG = LoggerFactory
.getLogger(DocumentFilter.class);
public static final String DocumentFilterParamNamePrefixKeep = "document.filter.md.keep.";
public static final String DocumentFilterParamNameMode = "document.filter.md.mode";
public static final String DocumentFilterParamNamePrefixSkip = "document.filter.md.skip.";
public static final String DocumentFilterParamNameURLFilterKeep = "document.filter.url.keep";
public static final String DocumentFilterParamNameMimeTypeFilterKeep = "document.filter.mimetype.keep";
public static final String DocumentFilterParamNameLength = "document.filter.max.content.length";
private Map<String, String> KVpatterns = new HashMap<String, String>();
private boolean negativeMode = true;
private Pattern URLRegex;
private Pattern MimetypeRegex;
private int maxContentLength = -1;
private String medataMode = "AND";
/**
* Checks whether any filters have been specified in the configuration
**/
public static boolean isRequired(Configuration conf) {
DocumentFilter filter = DocumentFilter.getFilters(conf);
if (filter.KVpatterns.size() > 0)
return true;
if (filter.URLRegex != null)
return true;
if (filter.MimetypeRegex != null)
return true;
if (filter.maxContentLength != -1)
return true;
return false;
}
/** Builds a document filter given a Configuration object **/
public static DocumentFilter getFilters(Configuration conf) {
// extracts the patterns
Map<String, String> PositiveKVpatterns = conf
.getValByRegex(DocumentFilterParamNamePrefixKeep + ".+");
Map<String, String> NegativeKVpatterns = conf
.getValByRegex(DocumentFilterParamNamePrefixSkip + ".+");
Map<String, String> tmpMap;
DocumentFilter filter = new DocumentFilter();
filter.medataMode = conf.get(DocumentFilterParamNameMode, "AND");
// has to be either prositive or negative but not both
if (PositiveKVpatterns.size() > 0 && NegativeKVpatterns.size() > 0) {
throw new RuntimeException(
"Can't have positive AND negative document filters - check your configuration");
} else if (PositiveKVpatterns.size() > 0) {
filter.negativeMode = false;
tmpMap = PositiveKVpatterns;
} else {
filter.negativeMode = true;
tmpMap = NegativeKVpatterns;
}
// normalise the keys
Iterator<Entry<String, String>> kviter = tmpMap.entrySet().iterator();
while (kviter.hasNext()) {
Entry<String, String> ent = kviter.next();
String k = ent.getKey();
String v = ent.getValue();
k = k.substring(DocumentFilterParamNamePrefixKeep.length());
StringBuilder message = new StringBuilder();
if (filter.negativeMode)
message.append("Negative ");
else
message.append("Positive ");
message.append("filter found : ").append(k).append(" = ").append(v);
LOG.info(message.toString());
filter.KVpatterns.put(k, v);
}
String URLPatternS = conf.get(DocumentFilterParamNameURLFilterKeep, "");
if (URLPatternS.length() > 0) {
try {
filter.URLRegex = Pattern.compile(URLPatternS);
} catch (PatternSyntaxException e) {
filter.URLRegex = null;
LOG.error("Can't create regular expression for URL from "
+ URLPatternS);
}
}
String MTPatternS = conf.get(DocumentFilterParamNameMimeTypeFilterKeep,
"");
if (MTPatternS.length() > 0) {
try {
filter.MimetypeRegex = Pattern.compile(MTPatternS);
} catch (PatternSyntaxException e) {
filter.MimetypeRegex = null;
LOG.error("Can't create regular expression for MimeType from "
+ MTPatternS);
}
}
filter.maxContentLength = conf
.getInt(DocumentFilterParamNameLength, -1);
return filter;
}
/** Returns true if the document can be kept, false otherwise **/
public boolean keep(BehemothDocument input) {
// filter if null
if (input == null)
return false;
// check length content
if (input.getContent() != null && maxContentLength != -1) {
if (input.getContent().length > maxContentLength)
return false;
}
// check on the URL
if (URLRegex != null) {
if (input.getUrl() == null)
return false;
boolean match = URLRegex.matcher(input.getUrl()).matches();
if (!match)
return false;
}
// check on the MimeType
if (MimetypeRegex != null) {
if (input.getContentType() == null)
return false;
boolean match = MimetypeRegex.matcher(input.getContentType())
.matches();
if (!match)
return false;
}
MapWritable metadata = input.getMetadata();
// no rules at all -> fine!
if (KVpatterns.size() == 0)
return true;
// document MUST have a certain value to be kept
if (metadata == null || metadata.isEmpty()) {
if (!negativeMode)
return false;
else
return true;
}
boolean hasMatch = false;
// find common keys between filters and content of doc
boolean matchesAll = true;
Iterator<String> kiter = KVpatterns.keySet().iterator();
while (kiter.hasNext()) {
String k = kiter.next();
String regex = KVpatterns.get(k);
// see if we have a metadata for that key
Writable value = metadata.get(new Text(k));
if (value == null) {
matchesAll = false;
continue;
}
if (value.toString().matches(regex)) {
hasMatch = true;
} else
matchesAll = false;
}
boolean successMatching = false;
if (medataMode.equalsIgnoreCase("AND")) {
if (matchesAll)
successMatching = true;
} else if (hasMatch)
successMatching = true;
if (successMatching) {
return (!negativeMode);
}
// no negative rule matching
if (negativeMode)
return true;
// no positive rule matching
return false;
}
}