Project

General

Profile

1 42914 eri.katsar
package eu.dnetlib.data.mapreduce.hbase.lodExport.preprocessing;
2 42692 eri.katsar
3 43143 giorgos.al
import java.io.IOException;
4
5 43102 eri.katsar
import org.apache.commons.lang.StringUtils;
6 42692 eri.katsar
import org.apache.hadoop.io.LongWritable;
7
import org.apache.hadoop.io.Text;
8
import org.apache.hadoop.mapreduce.Mapper;
9
import org.apache.log4j.Logger;
10
11 45843 eri.katsar
import eu.dnetlib.data.mapreduce.hbase.lodExport.utils.configuration.LodConfiguration;
12 42692 eri.katsar
13
/**
14
 * Mapper Class that reads HBASE contents and prepares them for the StatsDB
15
 * export
16
 */
17 42802 eri.katsar
/*
18
-> Parse LOD dump files
19
20
        Process lod input files and divide by entity type (both source and target)
21
        Transform to id, array of [ properties] format
22
        Store to HDFS
23
        For -> Multiple outputs and inputs
24
        Multiple inputs: all source and target datasets and their corresponding mappings
25
        M/O: separate output files for each dataset: mark records so that they are written to the correct one
26
*/
27
28 45528 eri.katsar
/*
29
<http://lod.openaire.eu/data/result/doajarticles::89217af00809a91acc15a416e56b3782> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.eurocris.org/ontologies/cerif/1.3#ResultEntity> .
30
<http://lod.openaire.eu/data/result/doajarticles::89217af00809a91acc15a416e56b3782> <http://www.eurocris.org/ontologies/cerif/1.3#name> "Une nouvelle anomalie a allure h r ditaire chez des agneaux  it khouzistans /it" .
31
*/
32
33 42692 eri.katsar
public class SourceMapper extends Mapper<LongWritable, Text, Text, Text> {
34
35 43347 eri.katsar
    private Logger log = Logger.getLogger(SourceMapper.class);
36 42692 eri.katsar
37 43347 eri.katsar
    private LodConfiguration lodConfiguration;
38 42802 eri.katsar
39 43485 eri.katsar
    public static enum SOURCE_COUNTERS {
40 45621 eri.katsar
        SOURCE_ENTITIES, TOTAL_ENTITIES
41 43347 eri.katsar
    }
42 43095 eri.katsar
43 42692 eri.katsar
44 43347 eri.katsar
    @Override
45
    protected void setup(Context context) throws IOException, InterruptedException {
46
        lodConfiguration = new LodConfiguration();
47 43526 eri.katsar
        lodConfiguration.load(context.getConfiguration().get("lod.sourceMappings"));
48 44137 eri.katsar
        log.info("file loaded!" + context.getConfiguration().get("lod.sourceMappings"));
49 45528 eri.katsar
50 43347 eri.katsar
    }
51 42692 eri.katsar
52
53 43347 eri.katsar
    @Override
54
    protected void map(final LongWritable keyIn, final Text result, final Context context) throws IOException {
55 42692 eri.katsar
56 43347 eri.katsar
        try {
57 45621 eri.katsar
58
            context.getCounter(SOURCE_COUNTERS.TOTAL_ENTITIES).increment(1);
59
60 43347 eri.katsar
            //get ID - output source_recordID so we can group by id and get all props of a record
61 43504 eri.katsar
62 43940 eri.katsar
            StringBuilder value = new StringBuilder();
63 45528 eri.katsar
            String[] inputParts = result.toString().split("\"");
64 42692 eri.katsar
65 43347 eri.katsar
            int i = 0;
66
            while (i < inputParts.length) {
67
                inputParts[i] = inputParts[i].replaceAll("\\s", "\t");
68
                i += 2;
69
            }
70 42692 eri.katsar
71 43347 eri.katsar
            String output = StringUtils.join(inputParts, "\"");
72
            String[] Fields = output.split("\t");
73 42918 eri.katsar
74 44174 eri.katsar
            if (Fields.length >= 2) {
75
                //here addd all  fields as array props and append "\t"
76
                // betweeen them so we can write directly to output
77
                //DO NOT enter id- we'll get it from key output
78 42918 eri.katsar
79 44174 eri.katsar
                i = 1;
80
                //extract entity type from subject
81
                String[] tmp = Fields[0].split("/");
82
                if (tmp.length >= 5) {
83
                    String type = tmp[4];
84
                    String subject = Fields[0];
85
                    if (lodConfiguration.entityExists(type)) {
86
                        while (i < Fields.length - 1) {
87
                            String field = Fields[i];
88
                            String fieldValue = Fields[i + 1];
89
                            if (lodConfiguration.isValidField(field)) {
90 45528 eri.katsar
                                value.append(subject).append("\t").append(field).append("\t").append(fieldValue).append("\t.\t");
91 44174 eri.katsar
                            }
92
                            i += 2;
93
                        }
94 45517 eri.katsar
                        // write out type,source_ID as key, and rest of props as value
95
                        Text key = new Text("OA" + "," + type + "," + subject);
96 45528 eri.katsar
                       if(value.toString().length()>0){
97 45517 eri.katsar
                        context.write(key, new Text(value.toString()));
98
                        context.getCounter(SOURCE_COUNTERS.SOURCE_ENTITIES).increment(1);
99 45528 eri.katsar
                    }}
100 43347 eri.katsar
                }
101 43537 eri.katsar
            }
102 44137 eri.katsar
        } catch (Exception e) {
103 43347 eri.katsar
            log.error("Error writing entity to M/R output", e);
104 45517 eri.katsar
            // throw new InterruptedIOException(e.toString());
105 43347 eri.katsar
        }
106 43102 eri.katsar
107 43347 eri.katsar
    }
108 43102 eri.katsar
109 43504 eri.katsar
    private static String cleanInput(Text result) {
110 44053 eri.katsar
        String resulString = result.toString().replace("<", "").replace(">", "");
111 43102 eri.katsar
112 43504 eri.katsar
        int ind = resulString.lastIndexOf(".");
113
        if (ind >= 0) {
114
            resulString = resulString.substring(0, ind);
115
        }
116
117
        return resulString;
118
    }
119
120 43347 eri.katsar
    @Override
121
    protected void cleanup(Context context) throws IOException, InterruptedException {
122
        super.cleanup(context);
123
    }
124 43102 eri.katsar
125
126 42692 eri.katsar
}