读取百度api存入csv
1、将获取到的json数据映射Java实体类如下
所用的依赖
<!-- Jackson--><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.14.2</version></dependency>
-
//接收返回的百度api数据的总实体类 @Data public class BaiduReturn {private int status;private String message;private String description;private Evaluation evaluation;private List<RoadTraffic> road_traffic;}
-
//拥堵数据 @Data public class CongestionSection{private String congestion_distance;private String speed;private int status;private String congestion_trend;private String section_desc; }
-
//拥堵状态 @Data public class Evaluation {private int status;private String status_desc;}
-
//路况数据 @Data public class RoadTraffic {private List<CongestionSection> congestion_sections;private String road_name; }
-
/*** 将百度获取的json映射为对象* @param fileName* @return*/private static List<BaiduReturn> getBaiduReturn(String fileName) {try {List<String> nameList;List<BaiduReturn> resultList = new ArrayList<>();//获取道路数据nameList = getRoadName(fileName);for(int i = 0;i<nameList.size();i++){BaiduApi snCal = new BaiduApi();// 创建一个Map对象,用于存储请求参数Map params = new LinkedHashMap<String, String>();params.put("road_name",nameList.get(i));params.put("city", "成都市");//URL和AK都是可以在百度官网获取params.put("ak", AK);String result = snCal.requestGetAK(URL, params);ObjectMapper objectMapper = new ObjectMapper();// 使用 readValue 方法解析 JSON 字符串为 BaiduReturn 对象BaiduReturn baiduReturn = objectMapper.readValue(result, BaiduReturn.class);if (baiduReturn.getStatus() == 0) {resultList.add(baiduReturn);}}//将BaiduReturn对象返回用于写入return resultList;} catch (Exception e) {throw new RuntimeException(e);}}/*** 提取csv文件中的道路名称* @param fileName* @return List<String>*/private static List<String> getRoadName(String fileName) {try {BufferedReader reader = new BufferedReader(new FileReader(fileName));//换成你的文件名reader.readLine();//第一行信息,为标题信息,不用,如果需要,注释掉String line = null;String[] roadArray = null;List<String> roadList = new ArrayList<>();while((line=reader.readLine())!=null) {if (line.trim().isEmpty()) {continue; // 跳过空行}roadArray = line.split(",");//CSV格式文件为逗号分隔符文件,这里根据逗号切分roadList.add(roadArray[0]); //保存所有街道}return roadList;} catch (IOException e) {throw new RuntimeException(e);}}
2、将百度api对象写入csv
所用的依赖
<!-- csv工具--><!-- https://mvnrepository.com/artifact/org.apache.commons/commons-csv --><dependency><groupId>org.apache.commons</groupId><artifactId>commons-csv</artifactId><version>1.9.0</version></dependency>
-
/*** 将BaiduReturn对象写入csv文件* @param resultList* @param outputFileName*/private static void writeBaiduReturnToCsv(List<BaiduReturn> resultList, String outputFileName,boolean append) {try (BufferedWriter writer = new BufferedWriter(new FileWriter(outputFileName, append))) {// 如果是第一次写入,写入CSV文件的表头if (!append || new File(outputFileName).length() == 0) {writer.write("status,message,description,evaluation_status,evaluation_status_desc,");writer.write("road_name,congestion_distance,speed,congestion_status,");writer.write("congestion_trend,section_desc\n");}for (BaiduReturn baiduReturn : resultList) {writer.write(baiduReturn.getStatus() + ",");writer.write(baiduReturn.getMessage() + ",");writer.write(baiduReturn.getDescription() + ",");writer.write(baiduReturn.getEvaluation().getStatus() + ",");writer.write(baiduReturn.getEvaluation().getStatus_desc() + ",");for (RoadTraffic roadTraffic : baiduReturn.getRoad_traffic()) {writer.write(roadTraffic.getRoad_name() + ",");if (roadTraffic.getCongestion_sections() != null) {for (CongestionSection congestionSection : roadTraffic.getCongestion_sections()) {writer.write(congestionSection.getCongestion_distance() + ",");writer.write(congestionSection.getSpeed() + ",");writer.write(congestionSection.getStatus() + ",");writer.write(congestionSection.getCongestion_trend() + ",");writer.write(congestionSection.getSection_desc() + ",");}}writer.newLine(); // 每个RoadTraffic对象写完后换行}}} catch (IOException e) {throw new RuntimeException(e);}}
package xyz.zzj.traffic_zzj.service;import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import xyz.zzj.traffic_zzj.dao.domain.BaiduReturn;
import xyz.zzj.traffic_zzj.dao.domain.CongestionSection;
import xyz.zzj.traffic_zzj.dao.domain.RoadTraffic;
import xyz.zzj.traffic_zzj.utils.BaiduApi;import java.io.*;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;import static xyz.zzj.traffic_zzj.utils.BaiduApi.AK;
import static xyz.zzj.traffic_zzj.utils.BaiduApi.URL;/*** @BelongsPackage: xyz.zzj.traffic_zzj.service* @ClassName: BaiduCsv* @Author: zengz* @CreateTime: 2024/12/26 21:20* @Description: 将数据读取并写入csv文件* @Version: 1.0*/
@EnableScheduling
@Component
public class BaiduCsv {
// public static void main(String[] args) throws Exception {@Scheduled(cron = "0 0/30 * * * ?")public void scheduledTask(){String fileName = "src\\main\\resources\\static\\cd_road_name.csv";String outputFileName = "src\\main\\resources\\static\\cd_road_traffic.csv";List<BaiduReturn> resultList = getBaiduReturn(fileName);writeBaiduReturnToCsv(resultList, outputFileName,true);}/*** 将百度获取的json映射为对象* @param fileName* @return*/private static List<BaiduReturn> getBaiduReturn(String fileName) {try {List<String> nameList;List<BaiduReturn> resultList = new ArrayList<>();nameList = getRoadName(fileName);for(int i = 0;i<nameList.size();i++){BaiduApi snCal = new BaiduApi();// 创建一个Map对象,用于存储请求参数Map params = new LinkedHashMap<String, String>();params.put("road_name",nameList.get(i));params.put("city", "成都市");params.put("ak", AK);String result = snCal.requestGetAK(URL, params);ObjectMapper objectMapper = new ObjectMapper();// 使用 readValue 方法解析 JSON 字符串为 BaiduReturn 对象BaiduReturn baiduReturn = objectMapper.readValue(result, BaiduReturn.class);if (baiduReturn.getStatus() == 0) {resultList.add(baiduReturn);}}//将BaiduReturn对象返回用于写入return resultList;} catch (Exception e) {throw new RuntimeException(e);}}/*** 提取csv文件中的道路名称* @param fileName* @return List<String>*/private static List<String> getRoadName(String fileName) {try {BufferedReader reader = new BufferedReader(new FileReader(fileName));//换成你的文件名reader.readLine();//第一行信息,为标题信息,不用,如果需要,注释掉String line = null;String[] roadArray = null;List<String> roadList = new ArrayList<>();while((line=reader.readLine())!=null) {if (line.trim().isEmpty()) {continue; // 跳过空行}roadArray = line.split(",");//CSV格式文件为逗号分隔符文件,这里根据逗号切分roadList.add(roadArray[0]); //保存所有街道}return roadList;} catch (IOException e) {throw new RuntimeException(e);}}/*** 将BaiduReturn对象写入csv文件* @param resultList* @param outputFileName*/private static void writeBaiduReturnToCsv(List<BaiduReturn> resultList, String outputFileName,boolean append) {try (BufferedWriter writer = new BufferedWriter(new FileWriter(outputFileName, append))) {// 如果是第一次写入,写入CSV文件的表头if (!append || new File(outputFileName).length() == 0) {writer.write("status,message,description,evaluation_status,evaluation_status_desc,");writer.write("road_name,congestion_distance,speed,congestion_status,");writer.write("congestion_trend,section_desc,createTime\n");}for (BaiduReturn baiduReturn : resultList) {writer.write(baiduReturn.getStatus() + ",");writer.write(baiduReturn.getMessage() + ",");writer.write(baiduReturn.getDescription() + ",");writer.write(baiduReturn.getEvaluation().getStatus() + ",");writer.write(baiduReturn.getEvaluation().getStatus_desc() + ",");for (RoadTraffic roadTraffic : baiduReturn.getRoad_traffic()) {writer.write(roadTraffic.getRoad_name() + ",");if (roadTraffic.getCongestion_sections() != null) {for (CongestionSection congestionSection : roadTraffic.getCongestion_sections()) {writer.write(congestionSection.getCongestion_distance() + ",");writer.write(congestionSection.getSpeed() + ",");writer.write(congestionSection.getStatus() + ",");writer.write(congestionSection.getCongestion_trend() + ",");writer.write(congestionSection.getSection_desc() + ",");}}else {writer.write("null" + ",");writer.write("null" + ",");writer.write("null" + ",");writer.write("null" + ",");writer.write("null" + ",");}// 获取当前时间戳long timestamp = System.currentTimeMillis();// 将时间戳转换为Instant对象Instant instant = Instant.ofEpochMilli(timestamp);// 转换为本地时间,指定时区LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());// 定义日期时间格式DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");// 格式化日期时间String formattedDateTime = localDateTime.format(formatter);writer.write(formattedDateTime+ ",");writer.newLine(); // 每个RoadTraffic对象写完后换行}}} catch (IOException e) {throw new RuntimeException(e);}}
}