/
MapperRSJ.java
96 lines (68 loc) · 2.52 KB
/
MapperRSJ.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package reduceSide;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
public class MapperRSJ extends Mapper<LongWritable, Text, RSJcompositeKey, Text> {
RSJcompositeKey ckwKey = new RSJcompositeKey();
Text txtValue = new Text("");
int intSrcIndex = 0;
StringBuilder strMapValueBuilder = new StringBuilder("");
List<Integer> lstRequiredAttribList = new ArrayList<Integer>();
@Override
protected void setup(Context context) throws IOException,InterruptedException {
// Get the source index; (employee = 1, salary = 2)
// Added as configuration in driver
FileSplit fsFileSplit = (FileSplit) context.getInputSplit();
intSrcIndex = Integer.parseInt(context.getConfiguration().get(fsFileSplit.getPath().getName()));
// Initialize the list of fields to emit as output based on
// intSrcIndex (1=employee, 2=current salary, 3=historical salary)
if (intSrcIndex == 1) // employee
{
lstRequiredAttribList.add(1); // FName
lstRequiredAttribList.add(2); // LName
lstRequiredAttribList.add(3); // Gender
}
else // salary
{
lstRequiredAttribList.add(1); // Salary
lstRequiredAttribList.add(3); // Effective-to-date (Value of
// 9999-01-01 indicates current
// salary)
}
}
private String buildMapValue(String arrEntityAttributesList[]) {
// This method returns csv list of values to emit based on data entity
strMapValueBuilder.setLength(0);// Initialize
// Build list of attributes to output based on source - employee/salary
for (int i = 1; i < arrEntityAttributesList.length; i++)
{
// If the field is in the list of required output
// append to stringbuilder
if (lstRequiredAttribList.contains(i))
{
strMapValueBuilder.append(arrEntityAttributesList[i]).append(",");
}
}
if (strMapValueBuilder.length() > 0)
{
// Drop last comma
strMapValueBuilder.setLength(strMapValueBuilder.length() - 1);
}
return strMapValueBuilder.toString();
}
@Override
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
if (value.toString().length() > 0)
{
String arrEntityAttributes[] = value.toString().split("\t");
ckwKey.setJoinKey(arrEntityAttributes[0].toString());
ckwKey.setSourceIndex(intSrcIndex);
txtValue.set(buildMapValue(arrEntityAttributes));
context.write(ckwKey, txtValue);
}
}
}