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
    评论
要将JSON数据导入Kibana,可以按照以下步骤进行操作: 1. 确保你已经安装并启动了Kibana。如果还没有安装,你可以从Elasticsearch官网下载适合你系统的版本。 2. 将你的JSON数据保存为一个文件,确保数据的格式符合Kibana的要求。 3. 打开Kibana的网页界面,在左侧导航栏中选择"Management"(管理)。 4. 在管理页面中,选择"Kibana",然后选择"Index Patterns"(索引模式)。 5. 点击"Create index pattern"(创建索引模式)按钮。 6. 在"Index pattern"字段中输入你想要的索引模式名称,例如"mydata"。 7. 在"Step 2 of 2: Define index pattern"(第二步:定义索引模式)页面,选择你的时间字段(如果有的话),然后点击"Create index pattern"(创建索引模式)按钮。 8. 返回管理页面,在左侧导航栏中选择"Dev Tools"(开发工具)。 9. 在开发工具页面的控制台中,输入以下命令来导入JSON数据: ``` PUT /mydata/_bulk { "index": { "_index": "mydata", "_type": "_doc" } } { "field1": "value1", "field2": "value2" } { "index": { "_index": "mydata", "_type": "_doc" } } { "field1": "value3", "field2": "value4" } ... ``` 请将上述命令中的"mydata"替换为你在步骤6中定义的索引模式名称,并按照你的数据格式修改字段名和字段值。 10. 执行上述命令后,你的JSON数据就会被导入到Kibana中的相应索引模式中。 请注意,以上步骤仅提供了一种基本的导入JSON数据的方法,实际操作中可能会根据具体情况有所不同。如果你遇到任何问题,请参考Kibana的官方文档或寻求相关支持。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [地区的json数据_肺炎病毒疫情数据爬取](https://blog.csdn.net/weixin_39798497/article/details/111113036)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [Python可视化学习——使用JSON进行数据转换、pyecharts模块调用以及可视化案例的介绍(可视化案例数据暂无...](https://blog.csdn.net/Williamtym/article/details/130438123)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值