|
@@ -62,8 +62,9 @@ public class AsyncPushTask {
|
|
|
private static final String MEDIA_TYPE = "application/json;charset=UTF-8";
|
|
|
private OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
|
|
|
|
|
|
- @Async
|
|
|
+ @Async("executor")
|
|
|
public Future<String> pushRecords(List<MonthResult> resultList) throws InterruptedException {
|
|
|
+ log.info("&&&&&&&&&&&&&&&&&&&&&&&&&&开启线程&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
|
|
|
MediaType mediaType = MediaType.parse(MEDIA_TYPE);
|
|
|
RequestBody requestBody = RequestBody.create(mediaType, JSON.toJSONString(resultList));
|
|
|
|
|
@@ -72,9 +73,9 @@ public class AsyncPushTask {
|
|
|
.url(recordsPushUrl)
|
|
|
.build();
|
|
|
OkHttpClient client = okHttpClient.newBuilder()
|
|
|
- .connectTimeout(10000, TimeUnit.MILLISECONDS)
|
|
|
- .readTimeout(10000, TimeUnit.MILLISECONDS)
|
|
|
- .writeTimeout(10000, TimeUnit.MILLISECONDS)
|
|
|
+ .connectTimeout(5000, TimeUnit.MILLISECONDS)
|
|
|
+ .readTimeout(30000, TimeUnit.MILLISECONDS)
|
|
|
+ .writeTimeout(30000, TimeUnit.MILLISECONDS)
|
|
|
.build();
|
|
|
String responseContext = "";
|
|
|
List<MonthResult> errorList = new ArrayList<>();
|
|
@@ -93,136 +94,142 @@ public class AsyncPushTask {
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
- CommonResponseObject responseObject = JSON.toJavaObject(JSON.parseObject(responseContext), CommonResponseObject.class);
|
|
|
- if (!responseObject.getCode().equals(CommonConstant.SUCCESS_CODE)) {
|
|
|
- JSONArray responseErrorArray = JSON.parseArray(responseObject.getMessage());
|
|
|
- errorList = responseErrorArray.toJavaList(MonthResult.class);
|
|
|
+ if (StringUtils.isNotBlank(responseContext)) {
|
|
|
+ CommonResponseObject responseObject = JSON.toJavaObject(JSON.parseObject(responseContext), CommonResponseObject.class);
|
|
|
+ if (!responseObject.getCode().equals(CommonConstant.SUCCESS_CODE)) {
|
|
|
+ JSONArray responseErrorArray = JSON.parseArray(responseObject.getMessage());
|
|
|
+ errorList = responseErrorArray.toJavaList(MonthResult.class);
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
List<String> errorData = new ArrayList<>();
|
|
|
for (MonthResult monthResult : errorList) {
|
|
|
errorData.add(monthResult.toString());
|
|
|
}
|
|
|
- // 异常数据写入本地文件
|
|
|
- FileUtil.INSTANCE.addAll(errorData);
|
|
|
- FileUtil.INSTANCE.write(errorRecordsPath+System.currentTimeMillis()+".csv");
|
|
|
- return new AsyncResult<>("推送完成");
|
|
|
- }
|
|
|
-
|
|
|
- @Async
|
|
|
- public void push(String value, String filePath) {
|
|
|
- List<MonthResult> results = getMonthResult(value, filePath);
|
|
|
- // 数据加密分区间
|
|
|
- List<MonthResult> maskResults = new ArrayList<>();
|
|
|
- for (MonthResult monthResult : results) {
|
|
|
- // 车牌号加密;
|
|
|
- String encryptPlateNumber = vehicleIdEncrypt(monthResult.getVehicleid());
|
|
|
- if (StringUtils.isBlank(encryptPlateNumber)) {
|
|
|
- // todo
|
|
|
- log.info(JSON.toJSONString(monthResult));
|
|
|
- continue;
|
|
|
- }
|
|
|
- maskResults.add(divideData(monthResult));
|
|
|
- }
|
|
|
- results = null;
|
|
|
-
|
|
|
- int times;
|
|
|
- if (maskResults.size() % 10000 != 0 ) {
|
|
|
- times = (maskResults.size() / 10000) + 1;
|
|
|
- } else {
|
|
|
- times = maskResults.size() / 10000;
|
|
|
- }
|
|
|
- List<List<MonthResult>> partResultsList = new ArrayList<>();
|
|
|
- for (int i = 0; i < times; i++) {
|
|
|
- List<MonthResult> partResults;
|
|
|
- if (i == times-1) {
|
|
|
- partResults = maskResults.subList(i*10000, maskResults.size());
|
|
|
- } else {
|
|
|
- partResults = maskResults.subList(i*10000, (i+1)*10000);
|
|
|
- }
|
|
|
- partResultsList.add(partResults);
|
|
|
- }
|
|
|
- // todo 此处需要考虑异步
|
|
|
- for (List<MonthResult> partResults : partResultsList) {
|
|
|
- try {
|
|
|
- pushRecords(partResults);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private List<MonthResult> getMonthResult(String i, String filePath) {
|
|
|
- Dataset<MonthResult> monthResultDataset = sparkSession
|
|
|
- .read()
|
|
|
- .option("header", true)
|
|
|
- .csv(filePath)
|
|
|
- .toDF("month", "vehicleid", "max_exTime", "sum_travel_time", "max_travel_time", "sum_feemileage",
|
|
|
- "sum_fee", "sum_weight_mileage","exTimes_count", "axlecount", "vehicletype",
|
|
|
- "travel_provinces_count", "transtime_count")
|
|
|
- .as(Encoders.bean(MonthResult.class))
|
|
|
- .coalesce(36);
|
|
|
- String tempViewName = "monthDataset" + i;
|
|
|
- monthResultDataset.createOrReplaceTempView(tempViewName);
|
|
|
-
|
|
|
- Dataset<MonthResult> rows = sparkSession.sql("select * from " + tempViewName + " where sum_travel_time < 2678400 "+
|
|
|
- "and max_travel_time < 604800 and sum_feemileage < 89280000 and sum_weight_mileage < 89280000 and exTimes_count < 1200" )
|
|
|
- .as(Encoders.bean(MonthResult.class));
|
|
|
-// rows.write().csv("/Users/jkxy/Desktop/outPut/test" + System.currentTimeMillis());
|
|
|
- List<MonthResult> results = rows.javaRDD().collect();
|
|
|
- sparkSession.sqlContext().dropTempTable(tempViewName);
|
|
|
- return results;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 对vehicle_id字段进行加密
|
|
|
- * @param vehicleId 待加密vehicleId
|
|
|
- * @return 加密后vehicleId
|
|
|
- */
|
|
|
- private String vehicleIdEncrypt(String vehicleId) {
|
|
|
- try {
|
|
|
- byte[] plainData = vehicleId.getBytes("UTF-8");
|
|
|
- byte[] iv = ivStr.getBytes("UTF-8");
|
|
|
- Long startTime = System.currentTimeMillis();
|
|
|
- // 加密
|
|
|
- byte[] cipherData = CipherSuiteUtils.encrypt(plainData, algorithm, keyId, metadata, iv);
|
|
|
- log.info("encrypt result: " + DatatypeConverter.printHexBinary(cipherData));
|
|
|
- log.info("encrypt success. time:" + (System.currentTimeMillis() - startTime) + "ms.");
|
|
|
- return DatatypeConverter.printHexBinary(cipherData);
|
|
|
- } catch (UnsupportedEncodingException ue) {
|
|
|
- ue.printStackTrace();
|
|
|
- log.error("UnsupportedEncodingException:", ue);
|
|
|
- return "";
|
|
|
- } catch (CipherSuiteException cse) {
|
|
|
- cse.printStackTrace();
|
|
|
- log.error("CipherSuiteException:", cse);
|
|
|
- return "";
|
|
|
- } catch (CipherSuiteMacException csme) {
|
|
|
- csme.printStackTrace();
|
|
|
- log.error("CipherSuiteMacException:", csme);
|
|
|
- return "";
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- log.error("Exception:", e);
|
|
|
- return "";
|
|
|
- } catch (Error error) {
|
|
|
- error.printStackTrace();
|
|
|
- log.error("Error:", error);
|
|
|
- return "";
|
|
|
+ if (errorData.size() != 0) {
|
|
|
+ // 异常数据写入本地文件
|
|
|
+ FileUtil.INSTANCE.addAll(errorData);
|
|
|
+ FileUtil.INSTANCE.write(errorRecordsPath+System.currentTimeMillis()+".csv");
|
|
|
}
|
|
|
+ return new AsyncResult<>("推送完成");
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 对数据脱敏,分区间
|
|
|
- * @param monthResult 待脱敏月结果数据
|
|
|
- * @return 脱敏后月结果数据
|
|
|
- */
|
|
|
- private MonthResult divideData(MonthResult monthResult) {
|
|
|
- monthResult.setExTimes_count(DivideDataUtil.divideExTimesCount(monthResult.getExTimes_count()));
|
|
|
- monthResult.setSum_fee(DivideDataUtil.divideSumFee(monthResult.getSum_fee()));
|
|
|
- monthResult.setSum_travel_time(DivideDataUtil.divideSumTravelTime(monthResult.getSum_travel_time()));
|
|
|
- monthResult.setMax_travel_time(DivideDataUtil.divideMaxTravelTime(monthResult.getMax_travel_time()));
|
|
|
- monthResult.setSum_feemileage(DivideDataUtil.divideSumFeeMileage(monthResult.getSum_feemileage()));
|
|
|
- monthResult.setSum_weight_mileage(DivideDataUtil.divideSumWeightMileage(monthResult.getSum_weight_mileage()));
|
|
|
- return monthResult;
|
|
|
- }
|
|
|
+// @Async
|
|
|
+// public void push(String value, String filePath) {
|
|
|
+// List<MonthResult> results = getMonthResult(value, filePath);
|
|
|
+// log.info("************************{}##########################", results.size());
|
|
|
+// // 数据加密分区间
|
|
|
+// List<MonthResult> maskResults = new ArrayList<>();
|
|
|
+// for (MonthResult monthResult : results) {
|
|
|
+// // 车牌号加密;
|
|
|
+// String encryptPlateNumber = vehicleIdEncrypt(monthResult.getVehicleid());
|
|
|
+// if (StringUtils.isBlank(encryptPlateNumber)) {
|
|
|
+// // todo
|
|
|
+// log.info(JSON.toJSONString(monthResult));
|
|
|
+// continue;
|
|
|
+// }
|
|
|
+// maskResults.add(divideData(monthResult));
|
|
|
+// }
|
|
|
+// results = null;
|
|
|
+//
|
|
|
+// int times;
|
|
|
+// if (maskResults.size() % 10000 != 0 ) {
|
|
|
+// times = (maskResults.size() / 10000) + 1;
|
|
|
+// } else {
|
|
|
+// times = maskResults.size() / 10000;
|
|
|
+// }
|
|
|
+// List<List<MonthResult>> partResultsList = new ArrayList<>();
|
|
|
+// for (int i = 0; i < times; i++) {
|
|
|
+// List<MonthResult> partResults;
|
|
|
+// if (i == times-1) {
|
|
|
+// partResults = maskResults.subList(i*10000, maskResults.size());
|
|
|
+// } else {
|
|
|
+// partResults = maskResults.subList(i*10000, (i+1)*10000);
|
|
|
+// }
|
|
|
+// partResultsList.add(partResults);
|
|
|
+// }
|
|
|
+// // todo 此处需要考虑异步
|
|
|
+// for (List<MonthResult> partResults : partResultsList) {
|
|
|
+// try {
|
|
|
+// pushRecords(partResults);
|
|
|
+// } catch (Exception e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// private List<MonthResult> getMonthResult(String i, String filePath) {
|
|
|
+// Dataset<MonthResult> monthResultDataset = sparkSession
|
|
|
+// .read()
|
|
|
+// .option("header", true)
|
|
|
+// .csv(filePath)
|
|
|
+// .toDF("month", "vehicleid", "max_exTime", "sum_travel_time", "max_travel_time", "sum_feemileage",
|
|
|
+// "sum_fee", "sum_weight_mileage","exTimes_count", "axlecount", "vehicletype",
|
|
|
+// "travel_provinces_count", "transtime_count")
|
|
|
+// .as(Encoders.bean(MonthResult.class))
|
|
|
+// .coalesce(36);
|
|
|
+// String tempViewName = "monthDataset" + i;
|
|
|
+// monthResultDataset.createOrReplaceTempView(tempViewName);
|
|
|
+//
|
|
|
+// Dataset<MonthResult> rows = sparkSession.sql("select * from " + tempViewName + " where sum_travel_time < 2678400 "+
|
|
|
+// "and max_travel_time < 604800 and sum_feemileage < 89280000 and sum_weight_mileage < 89280000 and exTimes_count < 1200" )
|
|
|
+// .as(Encoders.bean(MonthResult.class));
|
|
|
+//// rows.write().csv("/Users/jkxy/Desktop/outPut/test" + System.currentTimeMillis());
|
|
|
+// List<MonthResult> results = rows.javaRDD().collect();
|
|
|
+// sparkSession.sqlContext().dropTempTable(tempViewName);
|
|
|
+// return results;
|
|
|
+// }
|
|
|
+//
|
|
|
+// /**
|
|
|
+// * 对vehicle_id字段进行加密
|
|
|
+// * @param vehicleId 待加密vehicleId
|
|
|
+// * @return 加密后vehicleId
|
|
|
+// */
|
|
|
+// private String vehicleIdEncrypt(String vehicleId) {
|
|
|
+// try {
|
|
|
+// byte[] plainData = vehicleId.getBytes("UTF-8");
|
|
|
+// byte[] iv = ivStr.getBytes("UTF-8");
|
|
|
+// Long startTime = System.currentTimeMillis();
|
|
|
+// // 加密
|
|
|
+// byte[] cipherData = CipherSuiteUtils.encrypt(plainData, algorithm, keyId, metadata, iv);
|
|
|
+// log.info("encrypt result: " + DatatypeConverter.printHexBinary(cipherData));
|
|
|
+// log.info("encrypt success. time:" + (System.currentTimeMillis() - startTime) + "ms.");
|
|
|
+// return DatatypeConverter.printHexBinary(cipherData);
|
|
|
+// } catch (UnsupportedEncodingException ue) {
|
|
|
+// ue.printStackTrace();
|
|
|
+// log.error("UnsupportedEncodingException:", ue);
|
|
|
+// return "";
|
|
|
+// } catch (CipherSuiteException cse) {
|
|
|
+// cse.printStackTrace();
|
|
|
+// log.error("CipherSuiteException:", cse);
|
|
|
+// return "";
|
|
|
+// } catch (CipherSuiteMacException csme) {
|
|
|
+// csme.printStackTrace();
|
|
|
+// log.error("CipherSuiteMacException:", csme);
|
|
|
+// return "";
|
|
|
+// } catch (Exception e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// log.error("Exception:", e);
|
|
|
+// return "";
|
|
|
+// } catch (Error error) {
|
|
|
+// error.printStackTrace();
|
|
|
+// log.error("Error:", error);
|
|
|
+// return "";
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// /**
|
|
|
+// * 对数据脱敏,分区间
|
|
|
+// * @param monthResult 待脱敏月结果数据
|
|
|
+// * @return 脱敏后月结果数据
|
|
|
+// */
|
|
|
+// private MonthResult divideData(MonthResult monthResult) {
|
|
|
+// monthResult.setExTimes_count(DivideDataUtil.divideExTimesCount(monthResult.getExTimes_count()));
|
|
|
+// monthResult.setSum_fee(DivideDataUtil.divideSumFee(monthResult.getSum_fee()));
|
|
|
+// monthResult.setSum_travel_time(DivideDataUtil.divideSumTravelTime(monthResult.getSum_travel_time()));
|
|
|
+// monthResult.setMax_travel_time(DivideDataUtil.divideMaxTravelTime(monthResult.getMax_travel_time()));
|
|
|
+// monthResult.setSum_feemileage(DivideDataUtil.divideSumFeeMileage(monthResult.getSum_feemileage()));
|
|
|
+// monthResult.setSum_weight_mileage(DivideDataUtil.divideSumWeightMileage(monthResult.getSum_weight_mileage()));
|
|
|
+// return monthResult;
|
|
|
+// }
|
|
|
}
|