Files
address-batch-utils/src/BatchTest.java
T
shenjian@fangjia.com b496dc08f4 取消默认城市传参
2023-04-10 14:54:54 +08:00

181 lines
9.6 KiB
Java

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.util.DateUtils;
import com.alibaba.excel.util.StringUtils;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.fastjson.JSONObject;
import dto.FangJiaApiResponse;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.util.StopWatch;
import org.springframework.web.client.RestTemplate;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.net.URLEncoder;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicLong;
public class BatchTest {
final int column = 2;
public static void main(String[] args) throws FileNotFoundException, InterruptedException {
BatchTest test = new BatchTest();
test.run(args[0], args[1], args[2], Integer.valueOf(args[3]));
}
public void run(String ip, String file, String typeT, Integer nThreads) throws FileNotFoundException, InterruptedException {
ExecutorService executorService = Executors.newFixedThreadPool(nThreads);
final String type = StringUtils.isEmpty(typeT) ? "1" : typeT;
Map<String, List<String>> dataMap = new ConcurrentHashMap<>();
AtomicLong count = new AtomicLong(0);
String[] headArr = new String[]{"ID", "地址", "省份", "城市", "区域", "乡镇街道", "街道编码", "居委村委", "标准化地址", "是否涉农", "地址验证", "经度", "纬度", "百度经度", "百度纬度", "提示"};
List<List<String>> head = this.formatHead(headArr);
// 默认两行数据
String aimFilePath = file.substring(0, file.lastIndexOf(".xlsx")) + "结果" + DateUtils.format(new Date(), "yyyyMMddHHmm") + ".xlsx" ;
List<List<String>> dataList = ExcelUtils.read(new FileInputStream(file));
if (dataList.size() > 1000000) {
throw new RuntimeException("该批次数据量超过1000000条");
}
Set<String> set = new HashSet<>();
dataList.forEach(data -> {
if (data.size() == column ) {
set.add(data.get(column - 1));
} else {
// 存在为空情况,需保持一致
set.add("");
data.add("");
}
});
StopWatch stopWatch = new StopWatch();
stopWatch.start("总标准化耗时");
try {
CountDownLatch latch = new CountDownLatch(set.size());
RestTemplate restTemplate = new RestTemplate();
for (String addressTemp : set) {
final String address = addressTemp;
executorService.submit(() -> {
try {
String response = restTemplate.getForObject("http://" + ip + "/FangJiaAddressService/fiveDivisionStructure?address={address}&type={type}", String.class, address, type);
FangJiaApiResponse<JSONObject> apiResponse = JSONObject.parseObject(response, FangJiaApiResponse.class);
if (FangJiaApiResponse.ok().getStatus() == apiResponse.getStatus()) {
List<String> addressList = new ArrayList<>();
DivisionStructuredAddress structuredAddress = JSONObject.toJavaObject(apiResponse.getResult(), DivisionStructuredAddress.class);
addressList.add(structuredAddress.getProvince());
addressList.add(structuredAddress.getCity());
addressList.add(structuredAddress.getDistrict());
addressList.add(structuredAddress.getTownship());
addressList.add(structuredAddress.getTownshipCode());
addressList.add(structuredAddress.getCommunity());
addressList.add(structuredAddress.getFormatAddress());
addressList.add(structuredAddress.getIsAgriculture());
addressList.add(structuredAddress.getAddrVerify());
addressList.add(structuredAddress.getLng() != null ? String.valueOf(structuredAddress.getLng()) : "");
addressList.add(structuredAddress.getLat() != null ? String.valueOf(structuredAddress.getLat()) : "");
addressList.add(structuredAddress.getBdLng() != null ? String.valueOf(structuredAddress.getBdLng()) : "");
addressList.add(structuredAddress.getBdLat() != null ? String.valueOf(structuredAddress.getBdLat()) : "");
addressList.add(structuredAddress.getTips());
dataMap.put(address, addressList);
}
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("五秒后重试");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
String response = restTemplate.getForObject("http://" + ip + "/FangJiaAddressService/fiveDivisionStructure?address={address}", String.class, address);
FangJiaApiResponse<JSONObject> apiResponse = JSONObject.parseObject(response, FangJiaApiResponse.class);
try {
if (FangJiaApiResponse.ok().getStatus() == apiResponse.getStatus()) {
List<String> addressList = new ArrayList<>();
DivisionStructuredAddress structuredAddress = JSONObject.toJavaObject(apiResponse.getResult(), DivisionStructuredAddress.class);
addressList.add(structuredAddress.getProvince());
addressList.add(structuredAddress.getCity());
addressList.add(structuredAddress.getDistrict());
addressList.add(structuredAddress.getTownship());
addressList.add(structuredAddress.getTownshipCode());
addressList.add(structuredAddress.getCommunity());
addressList.add(structuredAddress.getFormatAddress());
addressList.add(structuredAddress.getIsAgriculture());
addressList.add(structuredAddress.getAddrVerify());
addressList.add(structuredAddress.getLng() != null ? String.valueOf(structuredAddress.getLng()) : "");
addressList.add(structuredAddress.getLat() != null ? String.valueOf(structuredAddress.getLat()) : "");
addressList.add(structuredAddress.getBdLng() != null ? String.valueOf(structuredAddress.getBdLng()) : "");
addressList.add(structuredAddress.getBdLat() != null ? String.valueOf(structuredAddress.getBdLat()) : "");
addressList.add(structuredAddress.getTips());
dataMap.put(address, addressList);
}
} catch (Exception e) {
System.out.println("地址" + address + "异常");
}
} finally {
latch.countDown();
count.incrementAndGet();
synchronized (this) {
if (count.longValue() % 1000 == 0) {
stopWatch.stop();
System.out.println("已标准化处理" + count.longValue() + "条地址, 耗时:" + stopWatch.getTotalTimeSeconds() / 60);
stopWatch.start();
}
}
}
});
}
latch.await();
if (stopWatch.isRunning()) {
stopWatch.stop();
}
System.out.println("已完成该批次地址标准化,耗时" + stopWatch.getTotalTimeSeconds() / 60);
for (List<String> data : dataList) {
String address = "";
if (data.size() == column) {
address = data.get(column - 1);
} else {
address = "";
}
List<String> addressRow = dataMap.get(address);
if (CollectionUtils.isNotEmpty(addressRow)) {
data.addAll(addressRow);
}
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
executorService.shutdown();
}
while (true) {
try {
ExcelWriter excelWriter = EasyExcel.write(aimFilePath).build();
WriteSheet writeSheet = EasyExcel.writerSheet(0, "标准化结果").head(head).build();
excelWriter.write(dataList, writeSheet);
excelWriter.finish();
break;
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("五秒后重试");
Thread.sleep(5000);
}
}
}
private List<List<String>> formatHead(String[] headArr) {
List<List<String>> head = new ArrayList<>();
for (String name: headArr) {
List<String> temp = new ArrayList<>();
temp.add(name);
head.add(temp);
}
return head;
}
}