Repository navigation
Expand file tree
/
Copy pathMR4.java
More file actions
111 lines (87 loc) · 3.05 KB
/
Copy pathMR4.java
File metadata and controls
111 lines (87 loc) · 3.05 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package UserCF.step4;
import DAO.HdfsDAO;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
/**
* MapReduce 计算矩阵乘法
* 输入:步骤2的输出
* 缓存:步骤3的输出
* 输出:用户ID(行) --- 物品ID(列) ---分值
*/
public class MR4
{
//hdfs地址
private static final String HDFS = "hdfs://master:9000";
//输入文件的路径
private static String inPath = "src/main/data/input/UserCF/step2/output/";
//输出文件的路径
private static String outPath = "src/main/data/input/UserCF/step4/";
//输入文件名
private static String fileName = "part-r-00000";
//全局缓存文件路径
private static String cachePath = "/boy/UserCF/step3/output/part-r-00000";
public int run()
{
try
{
Configuration conf = new Configuration();
conf.set("mapreduce.app-submission.cross-platform", "true");
conf.set("mapred.jar", "target/WordC-1.0-SNAPSHOT-jar-with-dependencies.jar");
Job job = Job.getInstance(conf, "step4");
//缓存:步骤3的输出
URI uri = new URI(cachePath + "#SoreMatrixZhuanzhi");
job.addCacheArchive(uri);
job.setJarByClass(MR4.class);
job.setMapperClass(Mapper4.class);
job.setReducerClass(Reduce4.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
HdfsDAO dao = new HdfsDAO(HDFS, conf);
String hdfsFilePath = "/boy/UserCF/step4/";
dao.mkdirs(hdfsFilePath);
dao.copyFile(inPath + fileName, hdfsFilePath);
FileInputFormat.addInputPath(job, new Path(hdfsFilePath + fileName));
FileOutputFormat.setOutputPath(job, new Path(hdfsFilePath + "output"));
boolean flag = job.waitForCompletion(true) == true;
if (flag == true)
{
dao.download(hdfsFilePath + "output", outPath);
return 1;
}
else
{
return -1;
}
} catch (IOException e)
{
e.printStackTrace();
} catch (InterruptedException e)
{
e.printStackTrace();
} catch (ClassNotFoundException e)
{
e.printStackTrace();
} catch (URISyntaxException e)
{
e.printStackTrace();
}
return -1;
}
public static void main(String[] args)
{
int result = new MR4().run();
if (result == 1)
System.out.println("ok...");
else if (result == -1)
System.out.println("wrong...");
}
}