ExportMappingMain.java
15 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
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
package com.glsx.bzi.logistmap;
import com.glsx.conf.ConfigurationManager;
import com.glsx.connection.MysqlConn;
import com.glsx.connection.MysqlMutiConn;
import com.glsx.util.DateUtils;
import java.io.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 根据区域去映射SN
*/
public class ExportMappingMain {
public static void main(String[] args) throws IOException {
// writeFile();
// if (1 == 1) return;
long bgnTime = System.currentTimeMillis();
BufferedWriter bw = new BufferedWriter(new FileWriter(new File("data/logistmap/result")));
StringBuffer buffer = new StringBuffer();
// 存储已经存在的设备
HashMap<String, Integer> savaMap = new HashMap<>();
// 获取D+设备SN select device_sn from dj_devices
HashMap<String, Integer> snsMapForD = getSnsMapForD();
// 从文件中读取 data/logistmap/mapping
// <2020-09-07|西宁 = 6,SH53A2007294775,2020-09-07 15:51:33.0,青海,西宁|5,SH53A2007294775,2020-09-07 15:51:33.0,青海,西宁>
HashMap<String, String> dtCityMap = readFile();
System.out.println("dtCityMap.size()=" + dtCityMap.size());
// 从滴加获取真实的设备 select userId,date_format(activeDate, '%Y-%m-%d'),activeCity,devType from t_pap_first_active_pos_info where activeDate > '2021-12-01'
// <2020-09-07|西宁 = 1790225300-8|1700112234-8>
HashMap<String, String> dtCitySnsMap = getSnsMap();
System.out.println("dtCitySnsMap.size()=" + dtCitySnsMap.size());
// 思路 从映射表中 查询滴加获取的真实设备
for (Map.Entry<String, String> entry : dtCityMap.entrySet()) {
// <2020-09-07|西宁 = 6,SH53A2007294775,2020-09-07 15:51:33.0,青海,西宁|5,SH53A2007294775,2020-09-07 15:51:33.0,青海,西宁>
String key1 = entry.getKey();
String value1 = entry.getValue();
String[] values1 = value1.split("\\|");
int len1 = values1.length;
int sumIndex = 0;
// 获取最近10天的key
List<String> nearDayKey = getNearDayKey(key1);
for (int i = 0; i < nearDayKey.size(); i++) {
String value2 = dtCitySnsMap.get(nearDayKey.get(i));
if (value2 != null) {
String[] values2 = value2.split("\\|");
int len2 = values2.length;
int minLen = len1 < len2 ? len1 : len2;
for (int index = 0; index < minLen; index++) {
String splitChild[] = values1[index + sumIndex].split("\\,");
String realsn = values2[index].split("\\-")[0];
String fakesn = splitChild[1];
if (snsMapForD.get(realsn) == null) continue;
savaMap.put(fakesn, 1);
buffer.append(splitChild[1] + "," +
values2[index].split("\\-")[0] + "," +
values2[index].split("\\-")[1] + "," +
splitChild[0] + "," +
splitChild[2] + "," +
splitChild[3] + "," +
splitChild[4] + "," +
splitChild[5] + "," +
splitChild[6]).append("\n");
}
bw.write(buffer.toString());
bw.flush();
buffer.setLength(0);
if (len1 <= len2) break;
else {
len1 = len1 - len2;
sumIndex += minLen;
}
}
}
bw.write(buffer.toString());
bw.flush();
buffer.setLength(0);
}
// 跑第二遍 跑非D+设备
for (Map.Entry<String, String> entry : dtCityMap.entrySet()) {
// <2020-09-07|西宁 = 6,SH53A2007294775,2020-09-07 15:51:33.0,青海,西宁|5,SH53A2007294775,2020-09-07 15:51:33.0,青海,西宁>
String key1 = entry.getKey();
String value1 = entry.getValue();
String[] values1 = value1.split("\\|");
int len1 = values1.length;
int sumIndex = 0;
// 获取最近10天的key
List<String> nearDayKey = getNearDayKey(key1);
for (int i = 0; i < nearDayKey.size(); i++) {
String value2 = dtCitySnsMap.get(nearDayKey.get(i));
if (value2 != null) {
String[] values2 = value2.split("\\|");
int len2 = values2.length;
int minLen = len1 < len2 ? len1 : len2;
for (int index = 0; index < minLen; index++) {
String splitChild[] = values1[index + sumIndex].split("\\,");
String fakesn = splitChild[1];
if (savaMap.get(fakesn) != null) continue;
savaMap.put(fakesn, 1);
buffer.append(splitChild[1] + "," +
values2[index].split("\\-")[0] + "," +
values2[index].split("\\-")[1] + "," +
splitChild[0] + "," +
splitChild[2] + "," +
splitChild[3] + "," +
splitChild[4] + "," +
splitChild[5] + "," +
splitChild[6]).append("\n");
}
bw.write(buffer.toString());
bw.flush();
buffer.setLength(0);
if (len1 <= len2) break;
else {
len1 = len1 - len2;
sumIndex += minLen;
}
}
}
bw.write(buffer.toString());
bw.flush();
buffer.setLength(0);
}
bw.write(buffer.toString());
bw.flush();
bw.close();
System.out.println("The job total cost " + (System.currentTimeMillis() - bgnTime) / 1000 + "s");
}
private static HashMap<String, Integer> getSnsMapForD() {
HashMap<String, Integer> SnsMapForD = new HashMap<>();
MysqlMutiConn.addConnection("FORD", ConfigurationManager.getProperty("mysql_prd_login"));
Connection ford = MysqlMutiConn.getConnection("FORD");
String sql = "select device_sn from dj_devices";
MysqlMutiConn.executeQuery(ford, sql, new MysqlMutiConn.QueryCallback() {
@Override
public void process(ResultSet rs) throws Exception {
while (rs.next()) {
SnsMapForD.put(rs.getString(1), 1);
}
}
});
MysqlMutiConn.close(ford);
return SnsMapForD;
}
// 从2020-09-07|西宁 得到从2020-09-07|西宁市,2020-09-08|西宁市,...,2020-09-17|西宁市
private static List<String> getNearDayKey(String key1) {
ArrayList<String> list = new ArrayList<>();
String[] split = key1.split("\\|");
String key = split[0];
String value = split[1] + "市";
List<String> beforeTenDay = DateUtils.getBeforeTenDay(key);
for (int i = 0; i < beforeTenDay.size(); i++) {
list.add(beforeTenDay.get(i) + "|" + value);
}
return list;
}
// <2020-09-07|西宁 = 1790225300-4|1700112234-5>
private static HashMap<String, String> getSnsMap() {
HashMap<String, String> dtCitySnsMap = new HashMap<>();
Connection mysqlConn = MysqlConn.getConnection(ConfigurationManager.getProperty("mysql_pap"));
String sql = "select userId,date_format(activeDate, '%Y-%m-%d'),activeCity,devType from t_pap_first_active_pos_info where activeDate > '2021-12-01'";
MysqlConn.executeQuery(mysqlConn, sql, new MysqlConn.QueryCallback() {
@Override
public void process(ResultSet rs) throws Exception {
while (rs.next()) {
String key = rs.getString(2) + "|" + rs.getString(3);
if (dtCitySnsMap.get(key) == null)
dtCitySnsMap.put(key, rs.getString(1) + "-" + String.valueOf(rs.getInt(4)));
else {
if (dtCitySnsMap.get(key).contains(rs.getString(1))) continue;
dtCitySnsMap.put(key, dtCitySnsMap.get(key) + "|" + rs.getString(1) + "-" + String.valueOf(rs.getInt(4)));
}
}
}
});
MysqlConn.close(mysqlConn);
return dtCitySnsMap;
}
// <2020-09-07|西宁 = 1790225300-4|1700112234-5>
private static HashMap<String, String> getSnsMapForD111() {
HashMap<String, String> dtCitySnsMap = new HashMap<>();
Connection mysqlConn = MysqlConn.getConnection(ConfigurationManager.getProperty("mysql_pap"));
String sql = "select userId,date_format(activeDate, '%Y-%m-%d'),activeCity,devType from t_pap_first_active_pos_info";
MysqlConn.executeQuery(mysqlConn, sql, new MysqlConn.QueryCallback() {
@Override
public void process(ResultSet rs) throws Exception {
while (rs.next()) {
String key = rs.getString(2) + "|" + rs.getString(3);
if (dtCitySnsMap.get(key) == null)
dtCitySnsMap.put(key, rs.getString(1) + "-" + String.valueOf(rs.getInt(4)));
else {
if (dtCitySnsMap.get(key).contains(rs.getString(1))) continue;
dtCitySnsMap.put(key, dtCitySnsMap.get(key) + "|" + rs.getString(1) + "-" + String.valueOf(rs.getInt(4)));
}
}
}
});
MysqlConn.close(mysqlConn);
return dtCitySnsMap;
}
private static HashMap<String, String> readFile() throws IOException {
InputStreamReader read = new InputStreamReader(new FileInputStream(new File("data/logistmap/mapping")));
BufferedReader br = new BufferedReader(read);
String lineTxt = null;
//<2020-09-07|西宁 = 6,SH53A2007294775,2020-09-07 15:51:33.0,青海,西宁,44192128,甘肃赛亚汽车销售服务有限公司>
HashMap<String, String> dtCityMap = new HashMap<>();
while ((lineTxt = br.readLine()) != null) {
String[] split = lineTxt.split("\\,");
if (split[4].equals("")) continue;
// System.out.println(lineTxt);
String dt = split[2].substring(0, 10);
String key = dt + "|" + split[4];
if (dtCityMap.get(key) == null)
dtCityMap.put(key, lineTxt);
else
dtCityMap.put(key, dtCityMap.get(key) + "|" + lineTxt);
}
return dtCityMap;
}
private static void writeFile() throws IOException {
// Connection mysqlConn = MysqlConn.getConnection(ConfigurationManager.getProperty("mysql_dev_login"));
// Connection mysqlConn = MysqlConn.getConnection(ConfigurationManager.getProperty("mysql_prd_login"));
Connection mysqlConn = MysqlConn.getConnection(ConfigurationManager.getProperty("mysql_prd_login"));
BufferedWriter bw = new BufferedWriter(new FileWriter("data/logistmap/mapping"));
StringBuffer buffer = new StringBuffer();
// dj_device_info/external_device_info
// String sql = "select a.device_type,a.device_sn,a.active_time,b.province_name,b.city_name,b.merchant_id,b.merchant_name \n" +
// "from dj_device_info_gps a \n" +
// "join dj_system_merchant b on a.merchant=b.merchant_name " +
// "where city_name is not null and active_time<'2021-01-01:00:00:00' " +
// "and device_type != 66";
// String sql = "select a.device_type,a.device_sn,a.active_time,b.province_name,b.city_name,b.merchant_id,b.merchant_name \n" +
// "from dj_device_info_gps a \n" +
// "left join dj_system_merchant b on a.merchant=b.merchant_name ";
// String sql = "select a.device_type,a.sn,a.active_time,b.province_name,b.city_name,b.merchant_id,b.merchant_name \n" +
// "from dj_2021_settle_details_gps a \n" +
// "left join dj_system_merchant b on a.active_merchant=b.merchant_name where a.active_time is not null";
String sql = "select a.device_type,a.sn,a.active_time,b.province_name,b.city_name,b.merchant_id,b.merchant_name " +
"from dj_2021_settle_details a left join dj_system_merchant b on a.active_merchant=b.merchant_name " +
"where a.active_time >= '2023-01-01' and device_type = 8";
String newSql = "";
// String sql1 = "select a.device_type,a.device_sn,a.device_active_time,b.province_name,b.city_name,b.merchant_id,b.merchant_name \n" +
// "from external_device_info a \n" +
// "join dj_system_merchant b on a.merchant_id=b.merchant_id where city_name is not null and device_type != 66";
// String sql = "select a.device_type,a.device_sn,a.device_active_time,b.province_name,b.city_name,b.merchant_id,b.merchant_name\n" +
// "from dj_device_info a \n" +
// "join dj_system_merchant b on a.merchant_id=b.merchant_id\n" +
// "where city_name is not null and device_active_time<'2021-01-01:00:00:00' and device_type != 66\n" +
// "UNION\n" +
// "select a.device_type,a.device_sn,a.active_time,b.province_name,b.city_name,b.merchant_id,b.merchant_name\n" +
// "from external_device_info a\n" +
// "join dj_system_merchant b on a.merchant_id=b.merchant_id where city_name is not null and device_type != 66";
MysqlConn.executeQuery(mysqlConn, sql, new MysqlConn.QueryCallback() {
// MysqlConn.executeFlowQuery(mysqlConn, sql, new MysqlConn.QueryCallback() {
@Override
public void process(ResultSet rs) throws Exception {
int index = 0;
while (rs.next()) {
buffer.append(rs.getInt(1) + "," +
rs.getString(2) + "," +
rs.getString(3) + "," +
rs.getString(4) + "," +
rs.getString(5) + "," +
rs.getInt(6) + "," +
rs.getString(7)).append("\n");
if (++index % 1000 == 0) {
bw.write(buffer.toString());
bw.flush();
buffer.setLength(0);
}
}
bw.write(buffer.toString());
bw.flush();
bw.close();
buffer.setLength(0);
System.out.println("总共导出数据条数为:" + index);
}
});
MysqlConn.close(mysqlConn);
}
}