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
package mobvista.prd.datasource.table;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import mobvista.prd.datasource.dao.ExportExcelDAO;
import mobvista.prd.datasource.dao.impl.ExportExcelDAOImpl;
import org.apache.poi.hssf.usermodel.*;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
* Created by liushuai on 2017/3/13 0013.
* desc:M系统指定app的国家性别年龄
*/
public class CreateMAppTable {
public static Map<String, Map<String, String>> maxAppMap = Maps.newHashMap();
public static void main(String[] args) {
HSSFWorkbook wb = null;
FileOutputStream fos = null;
try {
maxAppMap = readMaxApp("./maxAppName.txt");
Map<String, Map<String, List<String>>> countryMap = readCountryApp("./appCountNum.txt", args[0]);
fos = new FileOutputStream("./M系统指定app的国家性别年龄.xls");
wb = new HSSFWorkbook();
HSSFSheet s;
int sheet = 0;
for (Map.Entry<String, Map<String, List<String>>> country : countryMap.entrySet()) {
s = wb.createSheet();
wb.setSheetName(sheet, country.getKey());//以国家名创建sheet
List<String> head = Lists.newArrayList();//表头
head.add("APP name");
head.add("APPID");
head.add("platform(iOS/Android)");
head.add("package name");
head.add("address(GP/App Store)");
head.add("category");
head.add("DMP DAU");
head.add("Male");
head.add("Female");
head.add("Gender unknown");
head.add("Age 0-18");
head.add("Age 19-25");
head.add("Age 26-45");
head.add("Age 46-60");
head.add("Age 61 and above");
head.add("Age unknown");
head.add("mysql DAU");
int columnHead = 0;
HSSFRow rowOne = s.createRow(0);
for (String column : head) {
HSSFCell cell = rowOne.createCell(columnHead, 0);
HSSFRichTextString hts = new HSSFRichTextString(column);
cell.setCellValue(hts);
columnHead++;
}
int rowLine = 1;
for (Map.Entry<String, List<String>> packageCount : country.getValue().entrySet()) {
HSSFRow row = s.createRow(rowLine); //行
int columnNum = 0;
for (String column : packageCount.getValue()) {
HSSFCell cell = row.createCell(columnNum, 0);
HSSFRichTextString hts = new HSSFRichTextString(column);
cell.setCellValue(hts);
columnNum++;
}
rowLine++;
}
sheet++;
}
} catch (Exception e) {
e.printStackTrace();
System.exit(255);
} finally {
try {
if (wb != null) {
wb.write(fos);
}
if (fos != null) {
fos.flush();
fos.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static Map readCountryApp(String file, String date) throws Exception {
Map<String, Map<String, List<String>>> countryMap = Maps.newHashMap();
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String s = br.readLine();
while (s != null) {
Map<String, List<String>> partMap = Maps.newHashMap();
String[] fields = s.split("\t", -1);
String country = fields[15];
String packageName = "";
Map<String, String> cou = maxAppMap.get(country);
if (cou == null) {
packageName = "";
fields[3] = "";
fields[4] = "";
} else {
String line = cou.get(fields[0]);
if (line == null) {
packageName = "";
fields[3] = "";
fields[4] = "";
} else {
packageName = line.split("\t", -1)[2];
fields[2] = line.split("\t", -1)[3];
fields[3] = line.split("\t", -1)[0];
fields[4] = line.split("\t", -1)[1];
}
}
ExportExcelDAO dao = new ExportExcelDAOImpl();
String dau = dao.getAppDau(country, fields[1], date, fields[2]);//country,app_id,date,platform
List<String> list = Lists.newArrayList();
list.add(fields[0]);//APP name
list.add(fields[1]);//APPID
list.add(fields[2]);//platform(iOS/Android)
list.add(packageName);
list.add(fields[3]);//address(GP/App Store)
list.add(fields[4]);//category
list.add(fields[5]);//DMP DAU
list.add(fields[6]);//男
list.add(fields[7]);//女
list.add(fields[8]);//未知性别
list.add(fields[9]);//0-17
list.add(fields[10]);//18-24
list.add(fields[11]);//25-44
list.add(fields[12]);//45-59
list.add(fields[13]);//60+
list.add(fields[14]);//未知年龄
list.add(dau);//mysql dau
partMap = countryMap.get(country);
if (partMap == null) {
partMap = Maps.newHashMap();
}
List<String> mList = partMap.get(fields[0]); //app_name
if (mList == null) {
partMap.put(fields[0], list);
} else {
int mCount = Integer.parseInt(list.get(6));//DAU
int oldMCount = Integer.parseInt(mList.get(6)); //DAU
if (mCount > oldMCount) {
partMap.put(fields[0], list);
}
}
countryMap.put(country, partMap);//包信息
s = br.readLine();//再读下一行
}
return countryMap;
}
public static Map readMaxApp(String file) throws IOException {
Map<String, Map<String, String>> maxMap = Maps.newHashMap();
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String s = br.readLine();
while (s != null) {
Map<String, String> partMap = Maps.newHashMap();
String[] fields = s.split("\t", -1);
partMap = maxMap.get(fields[7]);
if (partMap == null) {
partMap = Maps.newHashMap();
partMap.put(fields[0],
fields[4] + "\t" + fields[5] + "\t" + fields[2] + "\t" + fields[3] + "\t" + fields[6]);
maxMap.put(fields[7], partMap);
} else {
String maxCount = partMap.get(fields[0]);
if (maxCount == null) {
partMap.put(fields[0],
fields[4] + "\t" + fields[5] + "\t" + fields[2] + "\t" + fields[3] + "\t" + fields[6]);
maxMap.put(fields[7], partMap);
} else {
String[] arr = maxCount.split("\t", -1);
if (Integer.parseInt(arr[4]) < Integer.parseInt(fields[6])) {
partMap.put(fields[0],
fields[4] + "\t" + fields[5] + "\t" + fields[2] + "\t" + fields[3] + "\t" + fields[6]);
maxMap.put(fields[7], partMap);
}
}
}
s = br.readLine();//再读下一行
}
return maxMap;
}
}