elasticsearch查出json数据后,再导入

场景:需要从A服务器es导出json数据,再导入到B服务器的es。
es版本: 7.15

查出数据

windows可以用postman, linux可以用curl 命令。
在这里插入图片描述

{
    "took": 12,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 41,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "my_index",
                "_type": "_doc",
                "_id": "48eabd2bc0338dfd46dcdf361e4ec94d",
                "_score": 1.0,
                "_source": {
                    "evtProId": "1acb5772df8f4647b9d5bc622ac8ebc7",
                    "progress": 0
                }
            },
            {
                "_index": "my_index",
                "_type": "_doc",
                "_id": "899277630026aa088c5a0d3374293d69",
                "_score": 1.0,
                "_source": {
                    "evtProId": "1278b4fcb3b4488c94b1e4ec1421d252",
                    "progress": 0
                }
            }
        ]
    }
}

批量导入数据

从网上查找到资料可知批量导入可使用以下方式,需对导出来的json进行一定的处理

HTTP Method: POST
URI: /your_index/_bulk
Request body (should end with a new line):

{ "index" : {"_id" : "48eabd2bc0338dfd46dcdf361e4ec94d" } }
{"evtProId":"1acb5772df8f4647b9d5bc622ac8ebc7","progress":0}
{ "index" : {"_id" : "899277630026aa088c5a0d3374293d69" } }
{"evtProId":"1278b4fcb3b4488c94b1e4ec1421d252","progress":0}

以上可以直接调用postman发送请求
由于其他原因,我只能使用curl的方式调用,首先将请求体另存为data.json 上传到linux 服务器
然后执行curl:
curl --user username:password -XPOST 'http://localhost:9200/your_index/_bulk' -H 'Content-Type: application/json' --data-binary @data.json
在这里插入图片描述


附:导出的数据json转换成批量导入格式的文本

简单写一个的程序,请自行完善

package luo;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;

/**
*
*
* @author
* @since 2023/3/23
*/
@Slf4j
public class EsJsonSolve {

    public static void main(String[] args) {
        ClassPathResource res = new ClassPathResource("es.json");

        InputStream jsonFileIs = null;
        try {
            jsonFileIs = res.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }

        StringBuilder sb = new StringBuilder();

        try {
            JSONObject jsonObject = JSON.parseObject(jsonFileIs, JSONObject.class);
            JSONObject firstHits = jsonObject.getJSONObject("hits");
            JSONArray secondHitsArr = firstHits.getJSONArray("hits");
            Iterator<Object> iter = secondHitsArr.iterator();
            while (iter.hasNext()){
                JSONObject hitJsonObj = (JSONObject) iter.next();
                String source = hitJsonObj.getString("_source");
                // log.info(source);
                // String insertHead =  "{ \"index\" : { \"_index\" : \"ypa_event_form\", \"_id\" : \"%s\" } }";
                String insertHead =  "{ \"index\" : {\"_id\" : \"%s\" } }";
                insertHead = String.format(insertHead, hitJsonObj.getString("_id"));
                sb.append(insertHead).append("\n");
                sb.append(source).append("\n");
            }
            sb.append("\n");
            System.out.println(sb.toString());
            FileOutputStream fileOutputStream = new FileOutputStream("E:\\insetData.json");
            fileOutputStream.write(sb.toString().getBytes(StandardCharsets.UTF_8));
            fileOutputStream.close();
        } catch (IOException e) {
            log.error("", e);
        }
    }
}

导出来的数据json放入es.json, 然后控制台输出可批量导入格式的文本,文本较长可自行输出到一个新文件中
在这里插入图片描述



参考链接:

  • https://stackoverflow.com/questions/33340153/elasticsearch-bulk-index-json-data
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值