本地化部署测试初始化
This commit is contained in:
@@ -0,0 +1,146 @@
|
||||
import com.alibaba.excel.EasyExcel;
|
||||
import com.alibaba.excel.ExcelWriter;
|
||||
import com.alibaba.excel.write.metadata.WriteSheet;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
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 RiskLevelTest {
|
||||
static final String IP = "open.fangjia.com";
|
||||
final String sourceFilePath = "D:\\goojia\\风险评级测试.xlsx";
|
||||
final String aimFilePath = "D:\\goojia\\风险评级测试结果.xlsx";
|
||||
final int column = 2;
|
||||
final Integer nThreads = 1;
|
||||
|
||||
public static void main(String[] args) throws FileNotFoundException, InterruptedException {
|
||||
RiskLevelTest test = new RiskLevelTest();
|
||||
test.run();
|
||||
}
|
||||
|
||||
public void run() throws FileNotFoundException, InterruptedException {
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(nThreads);
|
||||
Map<String, List<String>> dataMap = new ConcurrentHashMap<>();
|
||||
AtomicLong count = new AtomicLong(0);
|
||||
String[] headArr = new String[]{"ID", "地址", "评级"};
|
||||
List<List<String>> head = this.formatHead(headArr);
|
||||
// 默认两行数据
|
||||
List<List<String>> dataList = ExcelUtils.read(new FileInputStream(sourceFilePath));
|
||||
if (dataList.size() > 100000) {
|
||||
throw new RuntimeException("该批次数据量超过100000条");
|
||||
}
|
||||
Set<String> set = new HashSet<>();
|
||||
dataList.forEach(data -> {
|
||||
if (data.size() == column ) {
|
||||
set.add(data.get(column - 1));
|
||||
} else {
|
||||
// 存在为空情况,需保持一致
|
||||
set.add("");
|
||||
data.add("");
|
||||
}
|
||||
});
|
||||
|
||||
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 + "/address/risk?address={address}&token=F54F52381C49BB9EB4A33EB1B65604AE4B71A28AEE53518A94A2F360408B9056D57553931D15CE6DDE765562DAD286BA38E05A4CDAFC51C3D527A4959BF8E75A3B95DB7108FCEA340DDE61925616DB55118E1851E67D83EAD800460D100D6B667A4ED8EE67C8F7FB", String.class, address);
|
||||
FangJiaApiResponse<JSONObject> apiResponse = JSONObject.parseObject(response, FangJiaApiResponse.class);
|
||||
if ("ok".equals(apiResponse.getMsg())) {
|
||||
List<String> addressList = new ArrayList<>();
|
||||
RiskLevelInfo riskLevelInfo = JSONObject.toJavaObject(apiResponse.getResult(), RiskLevelInfo.class);
|
||||
addressList.add(riskLevelInfo.getRiskLevel());
|
||||
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 + "/address/risk?address={address}", String.class, address);
|
||||
FangJiaApiResponse<JSONObject> apiResponse = JSONObject.parseObject(response, FangJiaApiResponse.class);
|
||||
try {
|
||||
if ("ok".equals(apiResponse.getMsg())) {
|
||||
List<String> addressList = new ArrayList<>();
|
||||
RiskLevelInfo riskLevelInfo = JSONObject.toJavaObject(apiResponse.getResult(), RiskLevelInfo.class);
|
||||
addressList.add(riskLevelInfo.getRiskLevel());
|
||||
dataMap.put(address, addressList);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println("give up");
|
||||
}
|
||||
} finally {
|
||||
latch.countDown();
|
||||
count.incrementAndGet();
|
||||
synchronized (this) {
|
||||
if (count.longValue() % 1000 == 0) {
|
||||
System.out.println("已风险评级" + count.longValue() + "条地址");
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
latch.await();
|
||||
System.out.println("已完成该批次地址风险评级");
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user