在这里插入图片描述
在这里插入图片描述
JSON 文件内的数据格式,json文件数据条数不要过多,会请求参数过大,最好控制再10000以内。

import cn.hutool.core.collection.ListUtil;
import com.alibaba.fastjson2.JSONObject;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public static void main(String[] args) {
        List<String> lines = new ArrayList<>();
        String basePath = "G:\\data";
        // 使用 ClassLoader 来获取文件流
        String str = "xxx.json";//需要处理的json 数据文件,esdump 导出来的就可以
        // 使用 BufferedReader 逐行读取文件内容
        try {
            BufferedReader reader = new BufferedReader(new FileReader(basePath + "\\" + str));
            String line = "";
            while ((line = reader.readLine()) != null) {
                // 将每一行添加到列表中
                JSONObject jsonObject = JSONObject.parseObject(line);
                String uuid = jsonObject.getString("uuid");
                String idstr = "{\"index\":{\"_id\":\"" + uuid + "\"}}";
                lines.add(idstr);
                lines.add(line);
            }
            List<List<String>> split = ListUtil.split(lines, 10000);
            for (int i = 0; i < split.size(); i++) {
                String filePath = basePath + "\\" + "xxx.split-" + i + ".json";
                try {
                    // 创建文件对象
                    File file = new File(filePath);
                    // 创建文件写入对象
                    FileWriter writer = new FileWriter(file);
                    // 遍历List中的每个元素,将其写入到txt文件中
                    for (String item : split.get(i)) {
                        writer.write(item + "\n"); // 使用换行符分隔每个元素
                    }
                    // 关闭文件写入对象
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
{"index":{"_id":"baec07466732902d22a24ba01ff09751"}}
{"uuid":"baec07466732902d22a24ba01ff09751","gid":73111111111111111,"insert_time":"2025-01-07 13:46:26","coin_count":0}

也可以用CURL 命令

curl -XPOST 'http://xxx.xxx.xxx.xxxx:9200/xxxx/_bulk' 
--header 'Content-Type: application/json' 
--header 'Authorization: Basic ZWxhc3RpYzpXcE5GQFI4ejlWUw==' 
--data-binary '@/xxx.json'
Logo

火山引擎开发者社区是火山引擎打造的AI技术生态平台,聚焦Agent与大模型开发,提供豆包系列模型(图像/视频/视觉)、智能分析与会话工具,并配套评测集、动手实验室及行业案例库。社区通过技术沙龙、挑战赛等活动促进开发者成长,新用户可领50万Tokens权益,助力构建智能应用。

更多推荐