Repository navigation
Expand file tree
/
Copy pathMapper4.java
More file actions
86 lines (69 loc) · 2.56 KB
/
Copy pathMapper4.java
File metadata and controls
86 lines (69 loc) · 2.56 KB
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
package recommend.step4;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
public class Mapper4 extends Mapper<LongWritable, Text, Text, Text>
{
private Text outKey = new Text();
private Text outValue = new Text();
private List<String> cacheList = new ArrayList<>();
private DecimalFormat df = new DecimalFormat("0.00");
@Override
protected void setup(Context context) throws IOException, InterruptedException
{
super.setup(context);
FileReader fr = new FileReader("SoreMatrixZhuanzhi");
BufferedReader br = new BufferedReader(fr);
String line = null;
while ((line = br.readLine()) != null)
{
cacheList.add(line);
}
br.close();
fr.close();
}
@Override
/**
* 左侧矩阵
* key : 行
* value : 行 tab 列_值,列_值,列_值,列_值
*/ protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException
{
//行
String row_matrix1 = value.toString().split("\t")[0];
//列_值
String[] column_value_array_matrix1 = value.toString().split("\t")[1].split(",");
for (String line : cacheList)
{
//右侧矩阵
String row_matrix2 = line.toString().split("\t")[0];
String[] column_value_array_matrix2 = line.toString().split("\t")[1].split(",");
//矩阵相乘
double result = 0;
for (String column_value_matrix1 : column_value_array_matrix1)
{
String column_matrix1 = column_value_matrix1.split("_")[0];
String value_matrix1 = column_value_matrix1.split("_")[1];
for (String column_value_matrix2 : column_value_array_matrix2)
{
if (column_value_matrix2.startsWith(column_matrix1 + "_"))
{
String value_matrix2 = column_value_matrix2.split("_")[1];
result += Double.valueOf(value_matrix1) * Double.valueOf(value_matrix2);
}
}
}
if (result == 0)
continue;
outKey.set(row_matrix1);
outValue.set(row_matrix2 + "_" + df.format(result));
context.write(outKey, outValue);
}
}
}