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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package common.task;
import com.google.common.base.Splitter;
import common.model.AppCategory;
import common.model.AppInfo;
import common.repository.AppCategoryRepository;
import common.repository.AppInfoRepository;
import common.repository.CityRepository;
import dmp.model.OtPkgDevStats;
import dmp.model.TagCrawlerAppsWandoujia;
import dmp.repository.OtPkgDevStatsRepository;
import dmp.repository.TagCrawlerAppsWandoujiaRepository;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.anarres.lzo.LzoAlgorithm;
import org.anarres.lzo.LzoDecompressor;
import org.anarres.lzo.LzoInputStream;
import org.anarres.lzo.LzoLibrary;
import org.apache.commons.collections.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import util.*;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by zxy on 2017/12/26.
*/
public class SyncAppDataTask
{
private static Logger logger = LoggerFactory.getLogger(SyncAppDataTask.class);
@Autowired TagCrawlerAppsWandoujiaRepository tagRepository;
@Autowired AppInfoRepository appInfoRepository;
@Autowired AppCategoryRepository appCategoryRepository;
@Autowired CityRepository cityRepository;
@Autowired OtPkgDevStatsRepository otPkgDevStatsRepository;
/*public void syncAppData()
{
System.out.println(DateUtil.getBeforeDays(1));
List<TagCrawlerAppsWandoujia> list = tagRepository.findAllByDs("2017-12-15");
//从网上找了一个一线城市以及地级市的列表
List<String> citys = cityRepository.findCitys();
List<AppInfo> appInfos = new ArrayList<>();
List<AppCategory> appCategories = appCategoryRepository.findAll();
Map<String, List<String>> map = new HashMap<>();
for (AppCategory appCategory : appCategories) {
List<String> innerList = map.get(appCategory.getLevel());
if (innerList == null) {
innerList = new ArrayList<>();
}
innerList.add(appCategory.getName());
map.put(appCategory.getLevel(), innerList);
}
List<String> ry_pkgNameList = findReyunPkgList();
List<AppCategory> newCategorys = new ArrayList<>();
List<AppInfo> newInfos = new ArrayList<>();
for (TagCrawlerAppsWandoujia tag : list) {
AppInfo appInfo = new AppInfo();
appInfo.setName(tag.getName());
appInfo.setCompany(tag.getMaker());
//豌豆荚爬取的都是安卓的
appInfo.setOs("Android");
appInfo.setPkgName(tag.getPkgname());
//判断这批app中是否有热云已有的
if (ry_pkgNameList.contains(tag.getPkgname())) {
appInfo.setReyun(1);
}
appInfo.setLogoUrl(tag.getApplogo_link());
String types = tag.getMix_types();
//类别按照_分割,第一个是一级类别,第二个是二级类别,第三个是三级类别,剩下到组合起来放到其他类别中
//同时将爬取的数据中的类别字典取出来
if (!StringUtil.isEmpty(types)) {
String[] typeArray = types.split("_");
String otherCase = "";
for (int i=0; i<typeArray.length; i++) {
if (map.containsKey(String.valueOf(i+1)) && !map.get(String.valueOf(i+1)).contains(typeArray[i])) {
List<String> mapValue = map.get(String.valueOf(i+1));
AppCategory newCate = new AppCategory();
newCate.setName(typeArray[i]);
newCate.setLevel(String.valueOf(i+1));
newCategorys.add(newCate);
mapValue.add(typeArray[i]);
map.put(String.valueOf(i+1), mapValue);
} else if (!map.containsKey(String.valueOf(i+1))) {
AppCategory newCate = new AppCategory();
newCate.setName(typeArray[i]);
newCate.setLevel(String.valueOf(i+1));
newCategorys.add(newCate);
List<String> mapValue = new ArrayList<>();
mapValue.add(typeArray[i]);
map.put(String.valueOf(i+1), mapValue);
}
if (i == 0) {
appInfo.setFirstCate(typeArray[i]);
}
if (i == 1) {
appInfo.setSecondCate(typeArray[i]);
}
if (i == 2) {
appInfo.setThirdCate(typeArray[i]);
}
if (i > 2) {
otherCase += "_" + typeArray[i];
}
}
if (otherCase.length() > 0) {
appInfo.setOtherCate(otherCase.substring(1));
}
}
//看公司名字中是含有城市列表中的某个,记录app的公司所在地
for (String city : citys) {
if (tag.getMaker().indexOf(city) > -1) {
appInfo.setLocation(city);
break;
}
}
newInfos.add(appInfo);
System.out.println(appInfo);
}
//把以前爬取的app不是热云的拿出来,看一下现在是不是热云的,是的话,更新进去。
List<AppInfo> oldNotRyAppList = appInfoRepository.findAppListByReyun();
System.out.println(oldNotRyAppList);
for (AppInfo app : oldNotRyAppList) {
if (ry_pkgNameList.contains(app.getPkgName())) {
app.setReyun(1);
System.out.println(app);
newInfos.add(app);
}
}
appInfoRepository.save(newInfos);
appCategoryRepository.save(newCategorys);
}*/
public void syncAppDeviceNum() {
String ds = DateUtil.getBeforeDays(2);
//ds = "2017-12-22";
System.out.println(ds);
String skey = "export/pkg_dev_stats/ds=" + ds + "/";
final AwsS3Util s3Util = AwsS3Util.getInstance();
List itemList = AwsS3Util.getInstance().getS3Keys("reyunbpu", skey);
if (itemList == null || itemList.size() == 0) {
System.out.println("pkg_dev_stats null");
return;
}
for (int i = 0; i < itemList.size(); i++) {
String s3key = String.valueOf(itemList.get(i));
readS3File(s3Util, s3key, "deviceNum");
}
System.out.println("pkg_dev_stats end");
}
public void syncAppInfo() {
System.out.println("syncAppInfo start");
appInfoRepository.deleteAll();
String skey = "export/crawler_data/";
final AwsS3Util s3Util = AwsS3Util.getInstance();
List itemList = AwsS3Util.getInstance().getS3Keys("reyunbpu", skey);
if (itemList == null || itemList.size() == 0) {
return;
}
for (int i = 0; i < itemList.size(); i++) {
String s3key = String.valueOf(itemList.get(i));
readS3File(s3Util, s3key, "appInfo");
}
System.out.println("delete start");
//删除重复数据
try{
deleteRepeatData();
} catch (Exception e){
logger.error("delete faild " + e.getMessage(), e);
}
System.out.println("delete end");
}
private void readS3File(AwsS3Util s3Util, String s3key, String getContent)
{
List<AppCategory> newCategorys = new ArrayList<>();
List<String> citys = new ArrayList<>();
List<AppCategory> appCategories = new ArrayList<>();
Map<String, List<String>> map = new HashMap<>();
List<String> ry_pkgNameList = new ArrayList<>();
if (getContent.equals("appInfo")) {
citys = cityRepository.findCitys();
appCategories = appCategoryRepository.findAll();
for (AppCategory appCategory : appCategories) {
List<String> innerList = map.get(appCategory.getLevel());
if (innerList == null) {
innerList = new ArrayList<>();
}
innerList.add(appCategory.getName());
map.put(appCategory.getLevel(), innerList);
}
ry_pkgNameList = findReyunPkgList();
}
InputStream inputStream = null;
BufferedReader br = null;
try {
final byte[] fileBytes = s3Util.downloadBytesFromS3("reyunbpu", s3key);
inputStream = new ByteArrayInputStream(fileBytes);
if (s3key.endsWith(".lzo_deflate")) {
LzoAlgorithm algorithm = LzoAlgorithm.LZO1X;
LzoDecompressor deCompressor = LzoLibrary.getInstance().newDecompressor(algorithm, null);
LzoInputStream stream = new LzoInputStream(inputStream, deCompressor);
br = new BufferedReader(new InputStreamReader(stream));
}
else {
br = new BufferedReader(new InputStreamReader(inputStream));
}
String line = null;
List<OtPkgDevStats> resultList = new ArrayList<>();
List<AppInfo> appResultList = new ArrayList<>();
while ((line = br.readLine()) != null) {
// System.out.println(line);
Object[] array = Splitter.on("\t").trimResults().splitToList(line).toArray();
if (array.length == 3) {
OtPkgDevStats opds = new OtPkgDevStats();
opds.setPkgname(array[0].toString());
opds.setDevNum(Long.valueOf(array[1].toString()));
opds.setDs(array[2].toString());
resultList.add(opds);
if (resultList.size() >= 2000) {
//System.out.println(resultList);
otPkgDevStatsRepository.save(resultList);
resultList = new ArrayList<>();
}
} else if (array.length == 19) {
AppInfo appInfo = generateAppInfo(array, ry_pkgNameList, map, newCategorys, citys);
appResultList.add(appInfo);
if (appResultList.size() >= 2000) {
//System.out.println(appResultList);
appInfoRepository.save(appResultList);
appResultList = new ArrayList<>();
}
}
}
if (resultList.size() > 0) {
otPkgDevStatsRepository.save(resultList);
}
if (appResultList.size() > 0) {
appInfoRepository.save(appResultList);
}
//System.out.println(newCategorys);
if (newCategorys.size() > 0) {
appCategoryRepository.save(newCategorys);
}
}
catch (Exception e) {
logger.error("读取s3文件错误", e);
}
finally {
try {
if (br != null) {
br.close();
}
if (inputStream != null) {
inputStream.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
public List<String> findReyunPkgList() {
Map<String, String> conditions = new HashedMap();
String url = Constant.reportUrl + "/api/manager/bysql";
String sql = "select distinct pkgname from tkio.tkio_app_pkgname where pkgname!='NULL'";
conditions.put("sql", sql);
conditions.put("dbtype", "mysql");
conditions.put("datatype", "list");
conditions.put("reportname", "pkgname_list");
String responseJson = HttpClientUtil.doHttpPostRequest(url, "manager", conditions);
JSONObject object = JSONObject.fromObject(responseJson);
JSONArray array = object.getJSONArray("val");
List<String> pkgName = new ArrayList<>();
for (int i=0; i<array.size(); i++) {
JSONObject o = array.getJSONObject(i);
pkgName.add(o.getString("pkgname"));
}
return pkgName;
}
public AppInfo generateAppInfo(Object[] array, List<String> ry_pkgNameList, Map<String, List<String>> map, List<AppCategory> newCategorys, List<String> citys) {
AppInfo appInfo = new AppInfo();
appInfo.setName(array[4].toString());
appInfo.setCompany(array[9].toString());
//豌豆荚爬取的都是安卓的
appInfo.setOs("Android");
appInfo.setPkgName(array[6].toString());
//判断这批app中是否有热云已有的
if (ry_pkgNameList.contains(array[6].toString())) {
appInfo.setReyun(1);
}
appInfo.setLogoUrl(array[7].toString());
String types = array[12].toString();
//类别按照_分割,第一个是一级类别,第二个是二级类别,第三个是三级类别,剩下到组合起来放到其他类别中
//同时将爬取的数据中的类别字典取出来
if (!StringUtil.isEmpty(types)) {
String[] typeArray = types.split("_");
String otherCase = "";
for (int i=0; i<typeArray.length; i++) {
if (map.containsKey(String.valueOf(i+1)) && !map.get(String.valueOf(i+1)).contains(typeArray[i])) {
List<String> mapValue = map.get(String.valueOf(i+1));
AppCategory newCate = new AppCategory();
newCate.setName(typeArray[i]);
newCate.setLevel(String.valueOf(i+1));
newCategorys.add(newCate);
mapValue.add(typeArray[i]);
map.put(String.valueOf(i+1), mapValue);
} else if (!map.containsKey(String.valueOf(i+1))) {
AppCategory newCate = new AppCategory();
newCate.setName(typeArray[i]);
newCate.setLevel(String.valueOf(i+1));
newCategorys.add(newCate);
List<String> mapValue = new ArrayList<>();
mapValue.add(typeArray[i]);
map.put(String.valueOf(i+1), mapValue);
}
if (i == 0) {
appInfo.setFirstCate(typeArray[i]);
}
if (i == 1) {
appInfo.setSecondCate(typeArray[i]);
}
if (i == 2) {
appInfo.setThirdCate(typeArray[i]);
}
if (i > 2) {
otherCase += "_" + typeArray[i];
}
}
if (otherCase.length() > 0) {
appInfo.setOtherCate(otherCase.substring(1));
}
}
//看公司名字中是含有城市列表中的某个,记录app的公司所在地
for (String city : citys) {
if (array[9].toString().indexOf(city) > -1) {
appInfo.setLocation(city);
break;
}
}
return appInfo;
}
//删除app_info的重复数据
void deleteRepeatData(){
String sql = "DELETE FROM app_info WHERE id IN (SELECT id FROM ( SELECT id as id FROM app_info" +
" WHERE pkg_name IN ( SELECT pkg_name FROM app_info GROUP BY pkg_name " +
"HAVING COUNT(*) > 1) AND id NOT IN ( SELECT min(id) FROM app_info GROUP BY " +
"id HAVING count(*) > 1 )) b)";
DBUtil.newInstance().excute(sql);
};
/*public static void main(String[] args)
{
SyncAppDataTask task = new SyncAppDataTask();
task.syncAppData();
}*/
}