input.txt文本示例:
{"code:":0,"typeID":100,"runTime":599
,"":[{"position":[0,0,0],"words":"VU"},{"position":[0,0,0],"words":"877"},{"position":[0,0,0],"words":"粤Z-01p3港"}]}
{"code:":0,"typeID":100,"runTime":599
,"":[{"position":[0,0,0],"words":"ww"},{"position":[0,0,0]
,"words":"112"},{"position":[0,0,0],"words":"粤Z-01p3港"}]}
{"code:":0,"typeID":100,"runTime":599
,"":[{"position":[0,0,0],"words":"MS"},{"position":[0,0,0],"words":"81"},{"position":[0,0,0],"words":"粤Z-01p3澳"}]}
Entity实体类
@Data
@ApiModel
@Accessors(chain = true)
public class CarNumberEntity {
@ApiModelProperty("主键")
private String id;
@ApiModelProperty("大陆车牌")
private String dlp;
@ApiModelProperty("澳门车牌")
private String amp;
@ApiModelProperty("香港车牌")
private String xgp;
@ApiModelProperty("双牌车")
private String sp;
@ApiModelProperty("创建时间")
private String createrTime;
}
package com.example;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.UUID;
public class Test {
public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
//解决乱码问题
InputStreamReader reader = new InputStreamReader(new FileInputStream("D:\\input.txt"),"GBK");
BufferedReader br = new BufferedReader(reader);
String line="";
ArrayList<CarNumberEntity> entityList = new ArrayList<>();
while((line=br.readLine()) != null){
JSONObject object = JSONObject.parseObject("line");
JSONArray content = object.getJSONArray("contect");
// 因为content里面有很多个对象,需对其进行遍历,获取里面的key 为words的内容
ArrayList<String> wordsList = new ArrayList<>();
for (int i = 0; i < content.size(); i++) {
JSONObject item = (JSONObject) content.get(i);
String words = item.getString("words");
// 将获取到的words内容存入到wordsList集合中
wordsList.add(words);
}
StringBuilder sb = new StringBuilder();
//对集合内容进行判断
for (int i = 0; i < wordsList.size(); i++) {
if(i == 0){
sb.append(wordsList.get(i));
}else{
//以粤开头以港结尾的数据
if(wordsList.get(i-1).length() > 4 || wordsList.get(i).matches("^粤.*?港$")){
sb.append("\t").append(wordsList.get(i));
}else{
sb.append(wordsList.get(i));
}
}
}
//创建一个实体类去存这些车牌号
CarNumberEntity entity = new CarNumberEntity();
String[] split = sb.toString().split("\t");
for (String str : split) {
//车牌以粤开头以港或澳结尾那么就是双牌车
if(str.matches("^粤.*?[港|澳]$")){
entity.setSp(str);
continue;
}
//以M开头的车牌号为澳门车牌
if(str.matches("^M.*?")){
entity.setAmp(str);
continue;
}
//以汉字开头不以港结尾的是大陆车牌
if(str.matches("^[u4E00-u9FAS]") && !str.matches(".*?港$")){
entity.setDlp(str);
continue;
}
entity.setXgp(str);
}
//将整个对象存入list集合中
entityList.add(entity);
}
br.close();
reader.close();
//将上面的数据入库
Class.forName("com.huawei.gauss200.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:gaussdb://ip:port/dbName", "***", "******");
PreparedStatement ps = conn.prepareStatement("insert into \"zsk\".\"xxx\"(\"id\",\"dlp\",\"\",\"\",\"\",\"\") values (?,?,?,?,?,?);");
for (CarNumberEntity entity : entityList) {
ps.setString(1, UUID.randomUUID().toString().replace("-",""));
ps.setString(2,entity.getAmp());
ps.setString(3,entity.getDlp());
ps.setString(4,entity.getSp());
ps.setString(5,entity.getXgp());
ps.setString(6,entity.getCreaterTime());
ps.addBatch();
}
ps.executeBatch();
ps.close();
conn.close();
}
}