• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
黄洪波写点东西的地方
博客园    首页    新随笔    联系   管理    订阅  订阅
java导入json数据至doris

表结构字段名称与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;
        }
        }

 

posted on 2024-09-09 08:54  红无酒伤  阅读(116)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3