package com.csv;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.List;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;
/**
*
* @Description 写入CSV
* @Author wangymd
* @Date 2021-11-18 18:12:57
*/
public class WriteCSVFile{
private static RestTemplate restTemplate;
public static void main(String[] args) {
String fileName = "D:\\dev\\tempFile\\42_out2CSVFile.csv";//TODO
//读取excel
List buildCsv = buildCsv();
try {
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "GBK"));
if(CollectionUtils.isNotEmpty(buildCsv)) {
out2CSVFileTitle(out);//标题
for (String taskId : buildCsv) {
System.out.println(taskId);
ESImageDupCheckingVO esImageDupCheckingVO = invokeHttp(taskId);
//写入csv文件
out2CSVFile(out, esImageDupCheckingVO);
}
}
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void out2CSVFileTitle(BufferedWriter out) {
try {
out.write("contentId");
out.write(",");
out.write("contentId1");
out.write(",");
out.write("imgUrl1");
out.write(",");
out.write("score1");
out.write(",");
out.write("contentId2");
out.write(",");
out.write("imgUrl2");
out.write(",");
out.write("score2");
out.write(",");
out.write("contentId3");
out.write(",");
out.write("imgUrl3");
out.write(",");
out.write("score3");
out.write(",");
out.write("contentId4");
out.write(",");
out.write("imgUrl4");
out.write(",");
out.write("score4");
out.write(",");
out.write("contentId5");
out.write(",");
out.write("imgUrl5");
out.write(",");
out.write("score5");
out.newLine();
out.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
*
* @Description 写入esv文件
* @Author wangymd
* @Date 2021-11-22 10:46:06
* @param out
* @param esImageDupCheckingVO
*
*/
private static void out2CSVFile(BufferedWriter out, ESImageDupCheckingVO esImageDupCheckingVO) {
try {
out.write(esImageDupCheckingVO.getTaskId());
out.write(",");
List result = esImageDupCheckingVO.getResult();
if(CollectionUtils.isNotEmpty(result)) {
for (ImageDupCheckingVO imageDupCheckingVO : result) {
if(imageDupCheckingVO != null) {
out.write(imageDupCheckingVO.getContentId());
out.write(",");
out.write(imageDupCheckingVO.getImgUrl());
out.write(",");
out.write(imageDupCheckingVO.getScore().toString());
out.write(",");
}
}
}
out.newLine();
out.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
public static List buildCsv() {
String fileName = "D:\\dev\\tempFile\\42_20211119140531.csv";//TODO
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName),"GBK"));//换成你的文件名UTF-8
reader.readLine();//第一行信息,为标题信息,不用,如果需要,注释掉
String line = null;
List listJSONObject = Lists.newArrayList();
while((line = reader.readLine()) != null){
String item[] = line.split(",");//CSV格式文件为逗号分隔符文件,这里根据逗号切分
String taskId = item[0];
listJSONObject.add(taskId);
}
return listJSONObject;
}catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
*
* @Description 调用接口
* @Author wangymd
* @Date 2021-11-22 10:08:34
* @param taskId
*
*/
private static ESImageDupCheckingVO invokeHttp(String taskId) {
restTemplate = new RestTemplate();
HttpHeaders headersUpload = new HttpHeaders();
headersUpload.setContentType(MediaType.APPLICATION_JSON);
ESImageDupCheckingVO esImageDupCheckingVO = restTemplate.getForObject("http://data.com/es/queryByTaskId?taskId=" + taskId, ESImageDupCheckingVO.class);
return esImageDupCheckingVO;
}
}