Repository navigation
Expand file tree
/
Copy pathMR3.java
More file actions
113 lines (90 loc) · 3.33 KB
/
Copy pathMR3.java
File metadata and controls
113 lines (90 loc) · 3.33 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
112
113
package ContentCF.step3;
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;
/**
* 利用评分矩阵,构建物品与物品的相似度矩阵
* 输入:步骤1的输入
* 缓存:步骤2的输出
* 输出:物品ID(行) --- 物品ID(列) ---相似度
*/
public class MR3
{
//hdfs地址
private static final String HDFS = "hdfs://master:9000";
//输入文件的路径
private static String inPath = "src/main/data/input/ContentCF/step1/";
//输出文件的路径
private static String outPath = "src/main/data/input/ContentCF/step3/";
//输入文件名
private static String fileName = "ItemProfile.txt";
//全局缓存文件路径
private static String cachePath = "/boy/ContentCF/step2/output/part-r-00000";
public int run()
{
try
{
//System.setProperty("hadoop.home.dir", "D:\\Hadoop\\hadoop-2.7.5"); //可选
Configuration conf = new Configuration();
//conf.set("fs.default.name", HDFS);//可选
conf.set("mapreduce.app-submission.cross-platform", "true");
//conf.set("mapreduce.framework.name", "yarn"); //可选
conf.set("mapred.jar", "target/WordC-1.0-SNAPSHOT-jar-with-dependencies.jar");
Job job = Job.getInstance(conf, "step3");
//job.addCacheArchive(new URI(cachePath + "#itemUserScore"));
URI uri = new URI(cachePath + "#itemUserScore");
job.addCacheArchive(uri);
job.setJarByClass(MR3.class);
job.setMapperClass(Mapper3.class);
job.setReducerClass(Reduce3.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/ContentCF/step3/";
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 (URISyntaxException e)
{
e.printStackTrace();
} catch (InterruptedException e)
{
e.printStackTrace();
} catch (ClassNotFoundException e)
{
e.printStackTrace();
}
return -1;
}
public static void main(String[] args)
{
int result = new MR3().run();
if (result == 1)
System.out.println("ok...");
else if (result == -1)
System.out.println("wrong...");
}
}