Project

General

Profile

1
package eu.dnetlib.data.mapreduce.hbase.lodExport.preprocessing;
2

    
3
import java.io.IOException;
4

    
5
import org.apache.commons.lang.StringUtils;
6
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
import eu.dnetlib.data.mapreduce.hbase.lodExport.utils.configuration.LodConfiguration;
12

    
13
/**
14
 * Mapper Class that reads HBASE contents and prepares them for the StatsDB
15
 * export
16
 */
17
/*
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
/*
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
public class SourceMapper extends Mapper<LongWritable, Text, Text, Text> {
34

    
35
    private Logger log = Logger.getLogger(SourceMapper.class);
36

    
37
    private LodConfiguration lodConfiguration;
38

    
39
    public static enum SOURCE_COUNTERS {
40
        SOURCE_ENTITIES, TOTAL_ENTITIES
41
    }
42

    
43

    
44
    @Override
45
    protected void setup(Context context) throws IOException, InterruptedException {
46
        lodConfiguration = new LodConfiguration();
47
        lodConfiguration.load(context.getConfiguration().get("lod.sourceMappings"));
48
        log.info("file loaded!" + context.getConfiguration().get("lod.sourceMappings"));
49

    
50
    }
51

    
52

    
53
    @Override
54
    protected void map(final LongWritable keyIn, final Text result, final Context context) throws IOException {
55

    
56
        try {
57

    
58
            context.getCounter(SOURCE_COUNTERS.TOTAL_ENTITIES).increment(1);
59

    
60
            //get ID - output source_recordID so we can group by id and get all props of a record
61

    
62
            StringBuilder value = new StringBuilder();
63
            String[] inputParts = result.toString().split("\"");
64

    
65
            int i = 0;
66
            while (i < inputParts.length) {
67
                inputParts[i] = inputParts[i].replaceAll("\\s", "\t");
68
                i += 2;
69
            }
70

    
71
            String output = StringUtils.join(inputParts, "\"");
72
            String[] Fields = output.split("\t");
73

    
74
            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

    
79
                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
                                value.append(subject).append("\t").append(field).append("\t").append(fieldValue).append("\t.\t");
91
                            }
92
                            i += 2;
93
                        }
94
                        // write out type,source_ID as key, and rest of props as value
95
                        Text key = new Text("OA" + "," + type + "," + subject);
96
                       if(value.toString().length()>0){
97
                        context.write(key, new Text(value.toString()));
98
                        context.getCounter(SOURCE_COUNTERS.SOURCE_ENTITIES).increment(1);
99
                    }}
100
                }
101
            }
102
        } catch (Exception e) {
103
            log.error("Error writing entity to M/R output", e);
104
            // throw new InterruptedIOException(e.toString());
105
        }
106

    
107
    }
108

    
109
    private static String cleanInput(Text result) {
110
        String resulString = result.toString().replace("<", "").replace(">", "");
111

    
112
        int ind = resulString.lastIndexOf(".");
113
        if (ind >= 0) {
114
            resulString = resulString.substring(0, ind);
115
        }
116

    
117
        return resulString;
118
    }
119

    
120
    @Override
121
    protected void cleanup(Context context) throws IOException, InterruptedException {
122
        super.cleanup(context);
123
    }
124

    
125

    
126
}
(2-2/3)