1
|
package eu.dnetlib.data.mapreduce.hbase.index;
|
2
|
|
3
|
import eu.dnetlib.data.proto.TypeProtos;
|
4
|
import org.apache.commons.lang3.StringUtils;
|
5
|
import org.apache.commons.lang3.exception.ExceptionUtils;
|
6
|
import org.apache.commons.logging.Log;
|
7
|
import org.apache.commons.logging.LogFactory;
|
8
|
import org.apache.hadoop.io.NullWritable;
|
9
|
import org.apache.hadoop.io.Text;
|
10
|
import org.apache.hadoop.mapreduce.Mapper;
|
11
|
import org.dom4j.Document;
|
12
|
import org.dom4j.DocumentException;
|
13
|
import org.dom4j.io.DocumentResult;
|
14
|
import org.dom4j.io.DocumentSource;
|
15
|
import org.dom4j.io.SAXReader;
|
16
|
|
17
|
import javax.xml.transform.Transformer;
|
18
|
import javax.xml.transform.TransformerConfigurationException;
|
19
|
import javax.xml.transform.TransformerFactory;
|
20
|
import java.io.IOException;
|
21
|
import java.io.StringReader;
|
22
|
import java.util.regex.Matcher;
|
23
|
import java.util.regex.Pattern;
|
24
|
|
25
|
public class CountXmlRecordsMapper extends Mapper<Text, Text, NullWritable, NullWritable> {
|
26
|
|
27
|
private static final Log log = LogFactory.getLog(CountXmlRecordsMapper.class); // NOPMD by marko on 11/24/08 5:02 PM
|
28
|
|
29
|
private SAXReader saxReader;
|
30
|
|
31
|
@Override
|
32
|
protected void setup(final Context context) throws IOException, InterruptedException {
|
33
|
super.setup(context);
|
34
|
|
35
|
saxReader = new SAXReader();
|
36
|
}
|
37
|
|
38
|
@Override
|
39
|
protected void map(final Text key, final Text value, final Context context) throws IOException, InterruptedException {
|
40
|
try {
|
41
|
|
42
|
final Document doc = saxReader.read(new StringReader(value.toString()));
|
43
|
|
44
|
final Boolean deleted = Boolean.valueOf(doc.valueOf("//*[local-name()='entity']//datainfo/deletedbyinference"));
|
45
|
final String oaftype = doc.valueOf("local-name(//*[local-name()='entity']/*)");
|
46
|
|
47
|
context.getCounter(oaftype, String.format("%s deleted %s", oaftype, deleted)).increment(1);
|
48
|
|
49
|
if (TypeProtos.Type.result.toString().equals(oaftype)) {
|
50
|
|
51
|
final String resulttypeid = doc.valueOf("//*[local-name()='entity']/*[local-name()='result']/resulttype/@classid");
|
52
|
context.getCounter(oaftype, String.format("%s deleted %s", resulttypeid, deleted)).increment(1);
|
53
|
|
54
|
}
|
55
|
|
56
|
} catch (final Throwable e) {
|
57
|
//log.error("error parsing record\n" + value.toString(), e);
|
58
|
|
59
|
context.getCounter("error", e.getClass().getName()).increment(1);
|
60
|
}
|
61
|
}
|
62
|
|
63
|
}
|