• 使用Http请求实现数据的批量导入


      😊 @ 作者: 一恍过去
      🎊 @ 社区: Java技术栈交流
      🎉 @ 主题: 使用Http请求实现数据的批量导入
      ⏱️ @ 创作时间: 2022年08月17日

      前言

      通过HTTP请求的方式,实现数据批量导入到ES的指定索引中,可以将json参数写入body中或者已文件的形式进行上传。
      导入前必须要先创建索引,创建如下索引:

      • 请求方式:PUT
      • 请求地址:http…/索引名称
      • 请求参数:json
      # PUT http://192.168.80.121:9200/cars
      
      # 请求参数
      {
        "settings": {
          "number_of_shards": 2,
          "number_of_replicas": 1
        },
        "mappings": {
            "properties": {
              "color": {
                "type": "keyword"
              },
              "make": {
                "type": "keyword"
              },
              "price": {
                "type": "float"
              },
                "sold": {
                "type": "keyword"
              }
            }
          } 
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25

      1、JSON参数请求导入

      第一步:发起请求

      • 请求方式:POST
      • 请求地址:http…/索引名称/_bulk
      • 请求参数:json
      # POST http://192.168.80.121:9200/cars/_bulk
      
      # 请求参数,每个数据之间需要留空格,最后一行也要留空格
      
      {"index": {"_index": "cars", "_type": "_doc", "_id": 1}}
      { "price" : 10000, "color" : "red", "make" : "honda", "sold" : "2022-10-28" }
      
      {"index": {"_index": "cars", "_type": "_doc", "_id": 2}}
      { "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2022-11-05" }
      
      {"index": {"_index": "cars", "_type": "_doc", "_id": 3}}
      { "price" : 30000, "color" : "green", "make" : "ford", "sold" : "2022-05-18" }
      
      {"index": {"_index": "cars", "_type": "_doc", "_id": 4}}
      { "price" : 15000, "color" : "blue", "make" : "toyota", "sold" : "2022-07-02" }
      
      {"index": {"_index": "cars", "_type": "_doc", "_id": 5}}
      { "price" : 12000, "color" : "green", "make" : "toyota", "sold" : "2022-08-19" }
      
      {"index": {"_index": "cars", "_type": "_doc", "_id": 6}}
      { "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2022-11-05" }
      
      {"index": {"_index": "cars", "_type": "_doc", "_id": 7}}
      { "price" : 80000, "color" : "red", "make" : "bmw", "sold" : "2022-01-01" }
      
      {"index": {"_index": "cars", "_type": "_doc", "_id": 8}}
      { "price" : 25000, "color" : "blue", "make" : "ford", "sold" : "2022-02-12" }
      
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28

      第二步:验证

      如果存在数据,则表示导入成功

      # 请求:
      http://192.168.80.121:9200/cars/_search
      
      • 1
      • 2

      第二步:效果
      在这里插入图片描述

      2、上传文件请求导入

      第一步:创建json文件

      // 每个数据之间需要留空格,最后一行也要留空格
      {"index": {"_index": "cars", "_type": "_doc", "_id": 1}}
      { "price" : 10000, "color" : "red", "make" : "honda", "sold" : "2022-10-28" }
      
      {"index": {"_index": "cars", "_type": "_doc", "_id": 2}}
      { "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2022-11-05" }
      
      {"index": {"_index": "cars", "_type": "_doc", "_id": 3}}
      { "price" : 30000, "color" : "green", "make" : "ford", "sold" : "2022-05-18" }
      
      {"index": {"_index": "cars", "_type": "_doc", "_id": 4}}
      { "price" : 15000, "color" : "blue", "make" : "toyota", "sold" : "2022-07-02" }
      
      {"index": {"_index": "cars", "_type": "_doc", "_id": 5}}
      { "price" : 12000, "color" : "green", "make" : "toyota", "sold" : "2022-08-19" }
      
      {"index": {"_index": "cars", "_type": "_doc", "_id": 6}}
      { "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2022-11-05" }
      
      {"index": {"_index": "cars", "_type": "_doc", "_id": 7}}
      { "price" : 80000, "color" : "red", "make" : "bmw", "sold" : "2022-01-01" }
      
      {"index": {"_index": "cars", "_type": "_doc", "_id": 8}}
      { "price" : 25000, "color" : "blue", "make" : "ford", "sold" : "2022-02-12" }
      
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25

      第二步:创建json文件

      • 请求方式:POST
      • 请求地址:http…/索引名称/_bulk
      • 请求参数:file

      在这里插入图片描述

      第三步:验证

      如果存在数据,则表示导入成功

      # 请求:
      http://192.168.80.121:9200/cars/_search
      
      • 1
      • 2
    • 相关阅读:
      sql 限制返回的行数、从表中随机返回n行数据、将NULL转换为实际值
      JavaScript:模拟拍照
      机器学习数据挖掘十大经典算法 数学建模常用算法
      文档课堂作业1
      有一个整形数组, a[3] = {7,2,5}, 要求使用指针实现数组成员由小到大的顺序排列,即结果为:a[3] = {2,5,7};(C++指针练习2)
      CentOS7 自带防火墙+Nginx封堵高频访问的恶意IP
      Apipost连接数据库详解
      001 redis高并发减库存
      mysql出现提示错误10061的解决方法
      基于JAVA考研驿站网站计算机毕业设计源码+系统+mysql数据库+lw文档+部署
    • 原文地址:https://blog.csdn.net/zhuocailing3390/article/details/126336906