表结构字段名称与json key名称一致:
package com.ruoyi.doris; import cn.hutool.core.io.FileUtil; import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpResponse; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import lombok.extern.slf4j.Slf4j; import java.io.File; import java.nio.charset.Charset; import java.util.UUID; @Slf4j public class PurReqLineTest { public static void main(String[] args) throws Exception { // be String host = "192.168.0.132"; // be接口 int port = 8030; String database = "br"; String table = "PurchaseRequestLines"; String user = "root"; String passwd = "xxx"; // String s = "[{\"longitude\": \"19\", \"city\": \"北京\", \"latitude\": \"39.916927\"},{\"longitude\": \"2010\", \"city\": \"北京\", \"latitude\": \"39.916927\"}]"; String s = FileUtil.readString(new File("D:\\采购需求.txt"), Charset.defaultCharset()); // System.out.println(s); JSONArray value = JSONObject.parseObject(s).getJSONArray("value"); log.info("size {}", value.size()); for(int i=0;i<value.size(); i++){ JSONArray linesArray = value.getJSONObject(i).getJSONArray("DocumentLines"); for(int j=0; j<linesArray.size(); j++) { JSONObject lineObj = linesArray.getJSONObject(j); log.info("{}",lineObj.getInteger("DocEntry").toString() + lineObj.getInteger("LineNum").toString()); lineObj.put("LineEntryId", lineObj.getInteger("DocEntry").toString() + lineObj.getInteger("LineNum").toString()); linesArray.set(j, lineObj); String label = getUUID(); HttpResponse response = HttpRequest.put("http://" + host + ":" + port + "/api/" + database + "/" + table + "/_stream_load") .basicAuth(user, passwd) .header("Expect", "100-continue") .header("label", label) .header("Content-Type", "text/plain; charset=UTF-8") .header("format", "json") // 导入json数组 // .header("strip_outer_array", "true") .body(lineObj.toString()).setFollowRedirects(true) .execute(); log.info(response.body()); // Thread.sleep(3*1000); } } } public static String getUUID() { String uuid = UUID.randomUUID().toString().trim().replaceAll("-", ""); return uuid; } }
表结构字段名称与json key不一致
package com.ruoyi.doris; import cn.hutool.core.io.FileUtil; import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpResponse; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import lombok.extern.slf4j.Slf4j; import java.io.File; import java.nio.charset.Charset; import java.util.UUID; @Slf4j public class PurReqLineTest { public static void main(String[] args) throws Exception { // be String host = "192.168.0.132"; // be接口 int port = 8030; String database = "br"; String table = "PurchaseRequestLines"; String user = "root"; String passwd = "xxxx"; // String s = "[{\"longitude\": \"19\", \"city\": \"北京\", \"latitude\": \"39.916927\"},{\"longitude\": \"2010\", \"city\": \"北京\", \"latitude\": \"39.916927\"}]"; String s = FileUtil.readString(new File("D:\\采购需求.txt"), Charset.defaultCharset()); // System.out.println(s); JSONArray value = JSONObject.parseObject(s).getJSONArray("value"); log.info("size {}", value.size()); for(int i=0;i<value.size(); i++){ JSONArray linesArray = value.getJSONObject(i).getJSONArray("DocumentLines"); for(int j=0; j<linesArray.size(); j++) { JSONObject lineObj = linesArray.getJSONObject(j); log.info("{}",lineObj.getInteger("DocEntry").toString() + lineObj.getInteger("LineNum").toString()); lineObj.put("LineEntryId", lineObj.getInteger("DocEntry").toString() + lineObj.getInteger("LineNum").toString()); linesArray.set(j, lineObj); String label = getUUID(); HttpResponse response = HttpRequest.put("http://" + host + ":" + port + "/api/" + database + "/" + table + "/_stream_load") .basicAuth(user, passwd) .header("Expect", "100-continue") .header("label", label) .header("Content-Type", "text/plain; charset=UTF-8") .header("format", "json") // 导入json数组 // .header("strip_outer_array", "true") .body(lineObj.toString()).setFollowRedirects(true) .execute(); log.info(response.body()); // Thread.sleep(3*1000); } } } public static String getUUID() { String uuid = UUID.randomUUID().toString().trim().replaceAll("-", ""); return uuid; } }