Project

General

Profile

1
package eu.dnetlib.data.mapreduce.hbase.broker;
2

    
3
import eu.dnetlib.data.proto.DatasourceProtos;
4
import eu.dnetlib.data.proto.FieldTypeProtos;
5
import eu.dnetlib.data.proto.PersonProtos;
6
import org.apache.commons.lang3.StringUtils;
7
import org.apache.hadoop.hbase.util.Bytes;
8
import org.apache.hadoop.io.Text;
9

    
10
/**
11
 * Created by michele on 12/10/15.
12
 */
13
public class CalculatePersonDistributionUtils {
14

    
15
	public static final String SEPARATOR = "||@@||";
16
	private static final String PERSON_PREFIX = "PERSON" + SEPARATOR;
17
	private static final String DS_TYPE_PREFIX = "DSTYPE" + SEPARATOR;
18

    
19
	public static String createPersonValue(final PersonProtos.Person p) {
20
		try {
21
			final String fn = p.getMetadata().getFirstname().getValue();
22
			String sn = "";
23
			for (FieldTypeProtos.StringField sf : p.getMetadata().getSecondnamesList()) {
24
				sn += sf.getValue();
25
			}
26
			if (StringUtils.isNotBlank(fn) && StringUtils.isNotBlank(sn)) {
27
				return PERSON_PREFIX + sn + "_" + fn.substring(0, 1);
28
			}
29
			return null;
30
		} catch (Throwable e) {
31
			return null;
32
		}
33
	}
34

    
35
	public static String createDsTypeValue(final DatasourceProtos.Datasource ds) {
36
		return DS_TYPE_PREFIX + ds.getMetadata().getDatasourcetype().getClassid();
37
	}
38

    
39
	public static boolean isPerson(byte[] b) {
40
		return Bytes.toString(b).startsWith(PERSON_PREFIX);
41
	}
42

    
43
	public static boolean isDsType(byte[] b) {
44
		return Bytes.toString(b).startsWith(DS_TYPE_PREFIX);
45
	}
46

    
47
	public static String getPersonName(final byte[] b) {
48
		return StringUtils.substringAfter(Bytes.toString(b), PERSON_PREFIX);
49
	}
50

    
51
	public static String getType(final byte[] b) {
52
		return StringUtils.substringAfter(Bytes.toString(b), DS_TYPE_PREFIX);
53
	}
54

    
55
	public static Text prepareHdfsValue(String dsId, String name) {
56
		return new Text(dsId + CalculatePersonDistributionUtils.SEPARATOR + name);
57
	}
58

    
59
	public static Text prepareHdfsKey(final String dsId, final int count) {
60
		return new Text(dsId + "_" + count);
61
	}
62

    
63
	public static Text getDsIdFromHdfsValue(Text text) {
64
		return new Text(StringUtils.substringBefore(Bytes.toString(text.copyBytes()), SEPARATOR));
65
	}
66

    
67
	public static Text getNameFromHdfsValue(Text text) {
68
		return new Text(StringUtils.substringAfter(Bytes.toString(text.copyBytes()), SEPARATOR));
69
	}
70

    
71
}
(5-5/5)