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
package mobvista.dmp.datasource.age.mapreduce;
import com.google.common.collect.Maps;
import mobvista.dmp.util.MRUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.compress.GzipCodec;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.LazyOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
import org.codehaus.jackson.map.ObjectMapper;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.net.URI;
import java.util.Map;
/**
* Created by liushuai on 2017/2/17 0017.
*/
public class CalcDeviceAgeMR {
public static void main(String[] args) throws InterruptedException, IOException, ClassNotFoundException {
Configuration conf = new Configuration();
conf.set("mapreduce.map.speculative", "true");
conf.set("mapreduce.reduce.speculative", "true");
conf.set("mapreduce.task.io.sort.mb", "500");
conf.set("mapreduce.reduce.java.opts", "-Xmx1536m");
conf.set("mapreduce.reduce.memory.mb", "2048");
conf.set("mapreduce.reduce.shuffle.parallelcopies", "50");
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
conf.set("file", otherArgs[2]);
conf.set("mapreduce.map.memory.mb", "1331");
Job job = Job.getInstance(conf, "CalcDeviceAgeMR");
job.setNumReduceTasks(0);
job.setJarByClass(CalcDeviceAgeMR.class);
FileOutputFormat.setCompressOutput(job, true);
FileOutputFormat.setOutputCompressorClass(job, GzipCodec.class);
job.setMapperClass(CalcDeviceYearMapper.class);
// 取消part-r-00000新式文件输出
LazyOutputFormat.setOutputFormatClass(job, TextOutputFormat.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
job.setInputFormatClass(TextInputFormat.class);
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
// job.setOutputFormatClass(TextMultipleOutputFormat.class);
// MultipleOutputs.addNamedOutput(job, "age", TextOutputFormat.class,
// Text.class, Text.class);
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
public static class CalcDeviceYearMapper extends Mapper<LongWritable, Text, Text, Text> {
Map<String, String> packageMap = Maps.newHashMapWithExpectedSize(10000);
Text outKey = new Text();
// Text outKey_v2 = new Text();
Text outValue = new Text();
ObjectMapper objectMapper = new ObjectMapper(); //转换器
// private String outPath;
// private String outPath_v2;
public void setup(Context context) throws IOException, InterruptedException {
Configuration conf = context.getConfiguration();
// outPath = conf.get("outPath");
// outPath_v2 = conf.get("outPath_v2");
//读取CalcPackageDictMR的结果,放入Map中
String path = conf.get("file");
FileSystem fileSystem = FileSystem.get(URI.create(path), context.getConfiguration());
BufferedReader reader = new BufferedReader(new InputStreamReader(fileSystem.open(new Path(path))));
String readLine = "";
while ((readLine = reader.readLine()) != null) {
String[] arr = MRUtils.SPLITTER.split(readLine, -1);
String age = arr[1].substring(1);
packageMap.put(arr[0], age + "\t" + arr[2]);
}
reader.close();
}
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
String[] fields = MRUtils.SPLITTER.split(line, -1);
if (fields.length != 4) {
return;
}
String[] arr = Util.wellSplit.split(fields[1], -1);
String score = "-1.0";
int num = 0;
String tag = "unknown";
Map<Integer, Double> stage = Maps.newHashMap();
Map<Integer, Integer> stageNum = Maps.newHashMap();
Map<Integer, Double> st = Maps.newHashMap();
for (String packageName : arr) {
String ageRatioSource = packageMap.get(packageName);
if (ageRatioSource == null) {
continue;
}
String[] dictValue = MRUtils.SPLITTER.split(ageRatioSource);
if (dictValue[1].equals("confirm")) {
score = dictValue[0];
tag = dictValue[1];
num = 1;
break;
} else if (dictValue[1].equals("unbelievable")) {
if (tag.equals("unknown")) {
score = "-1.0";
tag = dictValue[1];
}
} else {
num += 1;
String[] item = Util.verticalLine.split(dictValue[0], -1);
for (String ageRatios : item) {
if (ageRatios == null) {
continue;
}
int generation = Integer.parseInt(Util.colonSplit.split(ageRatios, -1)[0]);//年龄段
double ageRatio = Double.parseDouble(Util.colonSplit.split(ageRatios, -1)[1]);//权重
Double stageAgeRatio = stage.get(generation);
if (stageAgeRatio == null) {
stage.put(generation, ageRatio);
stageNum.put(generation, 1);
} else {
double val = stageAgeRatio + ageRatio;
stage.put(generation, val);
stageNum.put(generation, stageNum.get(generation) + 1);
}
}
score = "";
tag = dictValue[1];
}
}
if (num == 1 && tag.equals("confirm")) {
score = "|" + score;
}
if (num == 1 && tag.equals("calc")) {
for (Map.Entry<Integer, Double> entry : stage.entrySet()) {
score = score + '|' + entry.getKey() + ":" + entry.getValue();
}
}
if (num > 1) {
for (Map.Entry<Integer, Double> entry : stage.entrySet()) {
Integer gen = entry.getKey();
double rat = entry.getValue() / stageNum.get(gen);
st.put(gen, rat);
}
for (Map.Entry<Integer, Double> entry : st.entrySet()) {
score = score + '|' + entry.getKey() + ":" + entry.getValue();
}
}
Map<String, Double> outMap = Maps.newHashMap();
outMap.put("0-17", 0.0);
outMap.put("18-24", 0.0);
outMap.put("25-44", 0.0);
outMap.put("45-59", 0.0);
outMap.put("60+", 0.0);
String[] scoreArr = score.split("\\|", -1);
BigDecimal ageRatioDouble = null; //小数转换
for (String ageRatio : scoreArr) {
if (!ageRatio.equals("") && !ageRatio.equals("-1.0")) {
String[] tmpRatio = Util.colonSplit.split(ageRatio, -1);
String generation = Util.getAge(Integer.parseInt(tmpRatio[0]));
if (generation != null && !generation.equals("")) {
ageRatioDouble = new BigDecimal(Double.parseDouble(tmpRatio[1]))
.setScale(6, BigDecimal.ROUND_HALF_UP);
outMap.put(generation, ageRatioDouble.doubleValue());
}
} else if (ageRatio.equals("-1.0")) {
outMap = Maps.newHashMap();
outMap.put("unbelievable", -1.0);
}
}
Map<String, String> ageMap = Maps.newHashMap();
if (!fields[2].equals("null")) {
String[] ageTags = fields[2].split("\\$", -1);
for (String ageTag : ageTags) {
if (ageTag == null || ageTag.equals("")) {
return;
}
String[] ageForm = Util.wellSplit.split(ageTag, -1);
String generation = Util.getAge(Integer.parseInt(ageForm[0]));
if (!generation.equals("")) {
ageMap.put(generation, ageForm[1]);
}
}
} else {
ageMap.put("null", "null");
}
Map<String, Map> lastMap = Maps.newLinkedHashMap();
lastMap.put("age_and_source", ageMap);
lastMap.put("age_and_proportion", outMap);
String packageName = fields[1].replace("#", ",");//包名之间以逗号分隔
String[] ss = Util.wellSplit.split(fields[3], -1);
outValue.set(MRUtils.JOINER.join(objectMapper.writeValueAsString(lastMap), tag));
outKey.set(MRUtils.JOINER.join(fields[0], //device_id
ss[0], // device_type
packageName // 包名
));
context.write(outKey, outValue);
/*
outKey_v2.set(outPath_v2 + "," + MRUtils.JOINER.join(fields[0], //device_id
ss[0], // device_type
packageName, // 包名
ss[1] // update_date
));
context.write(outKey_v2, outValue);
*/
// context.write(outKey, outValue
}
}
}