-
Notifications
You must be signed in to change notification settings - Fork 0
/
SlopeByZhang
288 lines (247 loc) · 11.6 KB
/
SlopeByZhang
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.KeyValueTextInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.jobcontrol.ControlledJob;
import org.apache.hadoop.mapreduce.lib.jobcontrol.JobControl;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.log4j.BasicConfigurator;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class SlopeOne {
public static class Job1Mapper extends Mapper<LongWritable, Text, Text, Text> {
private Text word1 = new Text();
private Text word2 = new Text();
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String strings = value.toString();
String[] split = strings.split(" ");
word1.set(split[0]);
if (split.length > 2) {
word2.set(split[1] + " " + split[2]);
} else {
word2.set(split[1] + " " + "-1");
}
context.write(word1, word2);
}
}
public static class Job1Reducer extends Reducer<Text, Text, Text, Text> {
private Text word1 = new Text();
private Text word2 = new Text();
@Override
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
Iterator<Text> it = values.iterator();
String[] s;
List<Integer> s_name = new ArrayList<Integer>(); //商铺名
List<Integer> score = new ArrayList<Integer>();
while (it.hasNext()) {
s = it.next().toString().split(" ");
s_name.add(Integer.parseInt(s[0]));
score.add(Integer.parseInt(s[1]));
}
System.out.println("qqqqqqqqqq+="+ s_name.size());
for (int i = 0; i < s_name.size(); i++) {
for (int j = 0; j < s_name.size(); j++) {
if (s_name.get(i) < s_name.get(j)) {
System.out.println("qqqqqqqqqq+="+ s_name.get(i)+" "+s_name.get(j));
String status;
//1表示第二个是需要预测的,0表示第一个是需要预测的。
if (score.get(i) != -1&& score.get(j)!= -1)
status = "-1";
else if (score.get(i) == -1&& score.get(j)== -1)
status = "-1";
else if (score.get(i) == -1&& score.get(j)!= -1)
status = "1";
else
status = "0";
word1.set(s_name.get(j) + " " + s_name.get(i));
if (status.equals("-1"))
word2.set(key.toString()+" "+Integer.toString(score.get(j) - score.get(i) ) + " " + status);
else if (status.equals("0"))
word2.set(key.toString()+" "+Integer.toString(score.get(i) ) + " " + status);
else
word2.set(key.toString()+" "+Integer.toString(score.get(j) ) + " " + status);
context.write(word1, word2);
}
}
}
}
}
public static class Job2Mapper extends Mapper<Text, Text, Text, Text>{
@Override
protected void map(Text key, Text value, Context context) throws IOException, InterruptedException {
context.write(key,value);
}
}
public static class Job2Reducer extends Reducer<Text ,Text, Text, Text>{
private Text word1 = new Text();
private Text word2 = new Text();
@Override
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
System.out.println("aaaaaaaaaaaaaaaa+=" + key.toString());
boolean need = false;
List<String> name = new ArrayList<String>();
List<Integer> score = new ArrayList<Integer>();
List<Integer> flag = new ArrayList<Integer>();
List<Integer> rem = new ArrayList<Integer>();
Iterator<Text> it= values.iterator();
while (it.hasNext()) {
String[] ans = it.next().toString().split(" ");
name.add(ans[0]);
score.add(Integer.parseInt(ans[1]));
flag.add(Integer.parseInt(ans[2]));
if (Integer.parseInt(ans[2])!=-1) need = true;
}
if(need) {
int num = 0,sum = 0;
for (int i = 0; i < score.size();i++) {
if (flag.get(i)==-1) {
num++;
sum += score.get(i);
}
if(flag.get(i)!=-1){
rem.add(i);
}
}
int avg = sum / num;
int fsum = 0;
String keys[] = key.toString().split(" ");
for (int i = 0; i < rem.size(); i++)
{
int index = rem.get(i);//需要评估的下标
int wk;//是哪个需要评估
if (flag.get(index) == 0)
{
fsum = score.get(index) + avg;
wk = 0;
}
else{
fsum = score.get(index) - avg;
wk = 1;
}
word1.set(name.get(index) + " " + keys[wk]);
word2.set(fsum + "");
context.write(word1,word2);
}
}
}
}
public static class Job3Mapper extends Mapper<Text, Text, Text, Text>{
@Override
protected void map(Text key, Text value, Context context) throws IOException, InterruptedException {
context.write(key,value);
}
}
public static class Job3Reducer extends Reducer<Text, Text, Text, Text>{
private Text word1 = new Text();
@Override
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
System.out.println("zzzzzzzzzzzzzzzzz");
Iterator<Text> it = values.iterator();
int sum = 0,score = 0;
while (it.hasNext()){
String s = it.next().toString();
sum++;
score += Integer.parseInt(s);
}
word1.set(score/sum +"");
context.write(key,word1);
}
}
public static void main(String[] args) throws IllegalArgumentException, IOException,
ClassNotFoundException, InterruptedException {
BasicConfigurator.configure();//log输出
//本类需要输入输出路径两个参数,因此需要判断输入参数是否是两个
//String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
/*
if (otherArgs.length < 2){//若参数个数不符合要求,则退出运行
System.err.println("Usage: wordcount <in> [<in>...] <out>");
System.exit(2);
}
*/
Configuration conf = new Configuration();
// conf.setStrings("mapreduce.reduce.shuffle.memory.limit.percent", "0.1");
Job job = Job.getInstance(conf);
job.setJarByClass(SlopeOne.class);//反射加载MapReduce主类
// 输入输出路径设置
FileInputFormat.addInputPath(job, new Path("E:\\testBig\\in.in"));
FileOutputFormat.setOutputPath(job, new Path("E:\\testBig\\out.out"));
//设置MR输入数据格式
job.setInputFormatClass(TextInputFormat.class);
//设置map阶段主类
job.setMapperClass(Job1Mapper.class);
//设置map阶段输出key的类型
job.setMapOutputKeyClass(Text.class);
//设置map阶段输出value的类型
job.setMapOutputValueClass(Text.class);
//设置reduce阶段主类
job.setReducerClass(Job1Reducer.class);
//设置reduce阶段输出key的类型(若map阶段并未设置输出key类型,则此参数同样适用于map阶段输出key)
job.setOutputKeyClass(Text.class);
//设置reduce阶段输出value的类型(若reduce阶段并未设置输出value类型,则此参数同样适用于reduce阶段输出value)
job.setOutputValueClass(Text.class);
//System.exit(job.waitForCompletion(true) ? 0 : 1); //job测试
Job job2 = Job.getInstance(conf);
job2.setJarByClass(SlopeOne.class);
job2.setInputFormatClass(KeyValueTextInputFormat.class);
FileInputFormat.addInputPath(job2, new Path("E:\\testBig\\out.out" + "/part-r-00000"));
FileOutputFormat.setOutputPath(job2, new Path("E:\\testBig\\out.out2"));
job2.setMapperClass(Job2Mapper.class);
job2.setMapOutputKeyClass(Text.class);
job2.setMapOutputValueClass(Text.class);
job2.setReducerClass(Job2Reducer.class);
job2.setOutputKeyClass(Text.class);
//设置reduce阶段输出value的类型(若reduce阶段并未设置输出value类型,则此参数同样适用于reduce阶段输出value)
job2.setOutputValueClass(Text.class);
Job job3 = Job.getInstance(conf);
job3.setJarByClass(SlopeOne.class);
job3.setInputFormatClass(KeyValueTextInputFormat.class);
FileInputFormat.addInputPath(job3, new Path("E:\\testBig\\out.out2" + "/part-r-00000"));
FileOutputFormat.setOutputPath(job3, new Path("E:\\testBig\\out.out3"));
job3.setMapperClass(Job3Mapper.class);
job3.setMapOutputKeyClass(Text.class);
job3.setMapOutputValueClass(Text.class);
job3.setReducerClass(Job3Reducer.class);
job3.setOutputKeyClass(Text.class);
//设置reduce阶段输出value的类型(若reduce阶段并未设置输出value类型,则此参数同样适用于reduce阶段输出value)
job3.setOutputValueClass(Text.class);
//运行job并根据最后的job状态退出程序
//job1加入控制器
ControlledJob ctrlJob1 = new ControlledJob(conf);
ctrlJob1.setJob(job);
//job2加入控制器
ControlledJob ctrlJob2 = new ControlledJob(conf);
ctrlJob2.setJob(job2);
//job2加入控制器
ControlledJob ctrlJob3 = new ControlledJob(conf);
ctrlJob3.setJob(job3);
//设置作业之间的依赖关系,job2的输入依赖job1的输出
ctrlJob2.addDependingJob(ctrlJob1);
ctrlJob3.addDependingJob(ctrlJob2);
//设置主控制器,控制job1和job2两个作业
JobControl jobCtrl = new JobControl("myCtrl");
//添加到总的JobControl里,进行控制
jobCtrl.addJob(ctrlJob1);
jobCtrl.addJob(ctrlJob2);
jobCtrl.addJob(ctrlJob3);
//在线程中启动,记住一定要有这个
Thread thread = new Thread(jobCtrl);
thread.start();
while (true) {
if (jobCtrl.allFinished()) {
System.out.println(jobCtrl.getSuccessfulJobList());
jobCtrl.stop();
break;
}
}
}
}