Project

General

Profile

« Previous | Next » 

Revision 57712

logic of the compositekeys for propagation moved on dedicated package. Ordering datasource, organization, results on the key that groups for the reducer

View differences:

modules/dnet-mapreduce-jobs/trunk/src/main/java/eu/dnetlib/data/mapreduce/hbase/propagation/compositekeys/NaturalInstOrgKeyPartitioner.java
1
package eu.dnetlib.data.mapreduce.hbase.propagation.compositekeys;
2

  
3
import org.apache.hadoop.io.Text;
4
import org.apache.hadoop.mapreduce.Partitioner;
5

  
6
/**
7
 * Created by miriam on 17/08/2018.
8
 */
9
public class NaturalInstOrgKeyPartitioner extends Partitioner<InstOrgKey, Text > {
10

  
11
    @Override
12
    public int getPartition(InstOrgKey key, Text val, int numPartitions) {
13
        final int res = Math.abs(key.getId().hashCode() % numPartitions);
14

  
15
        return res;
16
    }
17
}
modules/dnet-mapreduce-jobs/trunk/src/main/java/eu/dnetlib/data/mapreduce/hbase/propagation/compositekeys/NaturalInstOrgKeyGroupingComparator.java
1
package eu.dnetlib.data.mapreduce.hbase.propagation.compositekeys;
2

  
3
import org.apache.hadoop.io.WritableComparable;
4
import org.apache.hadoop.io.WritableComparator;
5

  
6
/**
7
 * Created by miriam on 17/08/2018.
8
 */
9
public class NaturalInstOrgKeyGroupingComparator extends WritableComparator {
10

  
11
    protected NaturalInstOrgKeyGroupingComparator() {
12
        super(InstOrgKey.class, true);
13
    }
14

  
15
    @Override
16
    public int compare(WritableComparable w1, WritableComparable w2) {
17
        final InstOrgKey k1 = (InstOrgKey) w1;
18
        final InstOrgKey k2 = (InstOrgKey) w2;
19

  
20
        return k1.getId().compareTo(k2.getId());
21
    }
22
}
modules/dnet-mapreduce-jobs/trunk/src/main/java/eu/dnetlib/data/mapreduce/hbase/propagation/compositekeys/InstOrgKey.java
1
package eu.dnetlib.data.mapreduce.hbase.propagation.compositekeys;
2

  
3
import com.google.common.collect.ComparisonChain;
4
import org.apache.hadoop.io.IntWritable;
5
import org.apache.hadoop.io.Text;
6
import org.apache.hadoop.io.WritableComparable;
7

  
8
import java.io.DataInput;
9
import java.io.DataOutput;
10
import java.io.IOException;
11

  
12
import static eu.dnetlib.data.mapreduce.hbase.propagation.PropagationConstants.*;
13

  
14
/**
15
 * Created by miriam on 17/08/2018.
16
 */
17
public class InstOrgKey implements WritableComparable<InstOrgKey> {
18

  
19

  
20

  
21
    private IntWritable keyType;
22

  
23
    private Text id;
24

  
25
    public InstOrgKey() {}
26

  
27
    public static InstOrgKey create(final int keyType, final String id) {
28
        return new InstOrgKey(keyType, id);
29
    }
30

  
31
    public static InstOrgKey datasource(final String id) {
32
        return new InstOrgKey(DATASOURCE, id);
33
    }
34

  
35
    public static InstOrgKey organization(final String id) {
36
        return new InstOrgKey(ORGANIZATION, id);
37
    }
38

  
39
    public static InstOrgKey publication(final String id){
40
        return new InstOrgKey(PUBLICATION, id);
41
    }
42

  
43
    public InstOrgKey(final int keyType, final String id) {
44
        this.id = new Text(id);
45
        this.keyType = new IntWritable(keyType);
46
    }
47

  
48
    public void setKeyType(final IntWritable keyType) {
49
        this.keyType = keyType;
50
    }
51

  
52
    public void setId(final Text id) {
53
        this.id = id;
54
    }
55

  
56
    public Text getId() {
57
        return id;
58
    }
59

  
60
    public IntWritable getKeyType() {
61
        return keyType;
62
    }
63

  
64
    @Override
65
    public int compareTo(final InstOrgKey o) {
66
        final int res = ComparisonChain.start()
67
                .compare(getId(), o.getId())
68
                .compare(getKeyType(), o.getKeyType())
69
                .result();
70

  
71
        //System.out.println(String.format("%s.compareTo(%s) = %s", toString(), o.toString(), res));
72
        return res;
73
    }
74

  
75
    @Override
76
    public void write(final DataOutput out) throws IOException {
77
        keyType.write(out);
78
        id.write(out);
79
    }
80

  
81
    @Override
82
    public void readFields(final DataInput in) throws IOException {
83
        keyType = new IntWritable();
84
        keyType.readFields(in);
85
        id = new Text();
86
        id.readFields(in);
87
    }
88

  
89
    @Override
90
    public String toString() {
91
        return (new StringBuilder())
92
                .append('{')
93
                .append(getKeyType().get())
94
                .append(',')
95
                .append(getId())
96
                .append('}')
97
                .toString();
98
    }
99
}

Also available in: Unified diff