Repository navigation
Expand file tree
/
Copy pathMR5.java
More file actions
109 lines (87 loc) · 3.14 KB
/
Copy pathMR5.java
File metadata and controls
109 lines (87 loc) · 3.14 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
package UserCF.step5;
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;
/**
* 根据评分矩阵,将步骤4的输出中,用户已经有用过行为的商品评分置0
* 输入:步骤4的输出
* 缓存:步骤1的输出
* 输出:用户ID(行) --- 物品ID(列) --- 分值 (最终的推荐列表)
*/
public class MR5
{
//hdfs地址
private static final String HDFS = "hdfs://master:9000";
//输入文件的路径
private static String inPath = "src/main/data/input/UserCF/step4/output/";
//输出文件的路径
private static String outPath = "src/main/data/input/UserCF/step5/";
//输入文件名
private static String fileName = "part-r-00000";
//全局缓存文件路径
private static String cachePath = "/boy/UserCF/step1/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, "step5");
//缓存:步骤3的输出
URI uri = new URI(cachePath + "#itemUserScore3");
job.addCacheArchive(uri);
job.setJarByClass(MR5.class);
job.setMapperClass(Mapper5.class);
job.setReducerClass(Reduce5.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/step5/";
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 MR5().run();
if (result == 1)
System.out.println("ok...");
else if (result == -1)
System.out.println("wrong...");
}
}