elasticsearch通过文件批量导入数据

这篇具有很好参考价值的文章主要介绍了elasticsearch通过文件批量导入数据。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

前言

有一个需求,需要测试es单个索引的性能,需要将一个索引灌1亿条数据,比较了3种常用的批量导入方式,选择了文件+shell批量导入

索引的mapping,如下

PUT corpus_details_17
{
  "settings": {
    "index.blocks.read_only_allow_delete": "false",
    "index.max_result_window": "10000000",
    "number_of_replicas": "0",
    "number_of_shards": "1"
  },
  "mappings": {
      "properties": {
        "targetContent": {
          "type": "text"
        },
        "sourceContent": {
          "type": "text"
        },
        "sourceLanguageId": {
          "type": "long"
        },
        "realmCode": {
          "type": "long"
        },
        "createTime": {
          "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis",
          "type": "date"
        },
        "corpusScore": {
          "type": "float"
        },
        "id": {
          "type": "long"
        },
        "targetLanguageId": {
          "type": "long"
        }
      
    }
  }
}

方式1:restAPI+shell

通过restAPI导入数据在数据量非常小的情况下也可以使用,一次性导入一亿条数据,这个要很长时间,非常慢,不推荐

方式2:java客户端批量导入

这种方式可以使用多线程,但是这种方式与restAPI无本质区别,无非是快了几倍,但是对于1亿数量级来说也非常慢,不推荐

方式3:批量生成数据文件+shell

官网:https://www.elastic.co/guide/cn/elasticsearch/guide/current/bulk.html#bulk
这篇文章简单介绍了批量导入数据的操作

1、首先生成导入的数据文件,文件是json的格式,最后一定要多一行回车

{"index":{"_index":"corpus_details_17","_type":"_doc"}}
{"id":15,"sourceContent":"测试数据","sourceLanguageId":1,"targetContent":"It's a cold winter AA.1","targetLanguageId":2,"realmCode":0,"corpusScore":0.842105,"createTime":1672292073000}
{"index":{"_index":"corpus_details_17","_type":"_doc"}}
{"id":16,"sourceContent":"测试数据","sourceLanguageId":1,"targetContent":"It's a cold winter AA.2","targetLanguageId":2,"realmCode":0,"corpusScore":0.842105,"createTime":1672292073000}
{"index":{"_index":"corpus_details_17","_type":"_doc"}}
{"id":17,"sourceContent":"测试数据","sourceLanguageId":1,"targetContent":"It's a cold winter AA.3","targetLanguageId":2,"realmCode":0,"corpusScore":0.842105,"createTime":1672292073000}
{"index":{"_index":"corpus_details_17","_type":"_doc"}}
{"id":18,"sourceContent":"测试数据","sourceLanguageId":1,"targetContent":"It's a cold winter AA.4","targetLanguageId":2,"realmCode":0,"corpusScore":0.842105,"createTime":1672292073000}
{"index":{"_index":"corpus_details_17","_type":"_doc"}}
{"id":19,"sourceContent":"测试数据","sourceLanguageId":1,"targetContent":"It's a cold winter AA.5","targetLanguageId":2,"realmCode":0,"corpusScore":0.842105,"createTime":1672292073000}

_index:索引、_type:类型(es默认_doc),下面是要插入的数据,一个数据文件的大小控制在25M左右。

@Slf4j
public class GenerateFile {
    
    public static void main(String[] args) throws Exception {

        final LocalDateTime time = LocalDateTime.of(2022, 12, 29, 13, 34, 33);

        int count = 1;

        String filePath = "test" + count + ".json";

        File file = new File(filePath);

        FileOutputStream out = new FileOutputStream(file, false);

        for (int i = 0; i <= 100000000; i++) {

            CorpusDetailsMapping mapping = new CorpusDetailsMapping();
            mapping.setId((long) (i+14));
            mapping.setSourceContent("测试数据");
            mapping.setSourceLanguageId(1);
            mapping.setTargetContent("It's a cold winter AA." + i);
            mapping.setTargetLanguageId(2);
            mapping.setRealmCode(0);
            mapping.setCorpusScore(0.842105f);
            mapping.setCreateTime(time);

            String json = JSONUtil.toJsonStr(mapping);

            json = "{\"index\":{\"_index\":\"corpus_details_17\",\"_type\":\"_doc\"}}\n" + json + "\n";

            out.write(json.getBytes(StandardCharsets.UTF_8));

            // 换新文件写入
            if (i % 100000  == 0) {
                if (out != null) {
                    out.close();
                }
                count++;
                log.info("已写入文件:"+filePath);
                filePath = "test" + count + ".json";
                file = new File(filePath);
                out = new FileOutputStream(file, false);
            }

        }
        out.close();
    }
}

2、可以通过curl -u name:'pwd' -XPUT "172.16.0.65:7201/_bulk" -H "Content-Type:application/json" --data-binary @test1.json 执行批量导入

3、我们通过java脚本生成了1001个文件,以shell脚本同时执行多个文件

  int=0
  while(($int<1001))
  do
	  let "int++"
	  echo test"$int".json
	  curl -u name:'pwd' -XPUT "172.16.0.65:7201/_bulk" -H "Content-Type:application/json" --data-binary @test"$int".json
  done

4、结论,单线程半个小时生成一个亿的数据文件,一个小时左右完成了1亿数据的导入文章来源地址https://www.toymoban.com/news/detail-406800.html

到了这里,关于elasticsearch通过文件批量导入数据的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • 文件上传(模板导出、批量导入)

    html部分 b class=\\\"addBtn UseraddBtn\\\" style=\\\"background: #3d7bde;right: 93px;\\\"+ 批量导入/b           input type=\\\"file\\\" id=\\\"fileInput\\\"             style=\\\"opacity: 0;right: 93px;width: 88px;height:29px;position: absolute;top:-5px\\\" οnchange=\\\"BatchImport()\\\"           a href=\\\"./模板.xlsx\\\" class=\\\"addBtn UseraddBtn\\\" target=\\\"_blank\\\"        

    2023年04月08日
    浏览(12)
  • Python批量导入图片到Word文件

    Python批量导入图片到Word文件

    问题背景:2017年4月应华章公司邀请,翻译一本来自美国的Python入门教材,全书共750页,2018年4月完成翻译交稿,2018年9月责任编辑寄来样稿进行印刷前校对。经过半个月的检查和校对,又进行了一些小的修改和完善。为避免辛苦修改后的样稿被寄丢,也为了节省邮寄路上的时

    2023年04月16日
    浏览(9)
  • 【ElasticSearch】基于 Java 客户端 RestClient 实现对 ElasticSearch 索引库、文档的增删改查操作,以及文档的批量导入

    【ElasticSearch】基于 Java 客户端 RestClient 实现对 ElasticSearch 索引库、文档的增删改查操作,以及文档的批量导入

    ElasticSearch 官方提供了各种不同语言的客户端,用来操作 ES。这些客户端的本质就是组装 DSL 语句,通过 HTTP 请求发送给 ES 服务器。 官方文档地址:https://www.elastic.co/guide/en/elasticsearch/client/index.html。 在本文中,我们将着重介绍 ElasticSearch Java 客户端中的 RestClient,并演示如何

    2024年02月08日
    浏览(16)
  • ELK(Elasticsearch、Kibana、Logstash)以及向ES导入mysql数据库数据或CSV文件数据,创建索引和可视化数据

    ELK(Elasticsearch、Kibana、Logstash)以及向ES导入mysql数据库数据或CSV文件数据,创建索引和可视化数据

    地址:Past Releases of Elastic Stack Software | Elastic 在Products和version处分别选择需要下载的产品和版本,E(elasticsearch)L(logstash)K(kibana)三者版本必须相同 将下载好的elk分别解压到相同路径下 本文中elasticsearch=E=ES=es;L=logstash;K=kibana 一般情况下使用默认配置即可,下面对我的

    2024年02月15日
    浏览(52)
  • 【大数据】Hive 中的批量数据导入

    在博客【大数据】Hive 表中插入多条数据 中,我简单介绍了几种向 Hive 表中插入数据的方法。然而更多的时候,我们并不是一条数据一条数据的插入,而是以批量导入的方式。在本文中,我将较为全面地介绍几种向 Hive 中批量导入数据的方法。 overwrite :表示覆盖表中已有数

    2024年02月11日
    浏览(8)
  • EasyExcel 批量导入并校验数据

    EasyExcel 批量导入并校验数据

    EasyExcel 批量导入并校验数据 日期形式的字段因为校验需要,提供了String类型的字段,再转换赋值给真正的数据库字段对象,如果不考虑校验问题可直接转换 @ExcelProperty(value = \\\"处罚信息公示日期\\\", index = 5, converter = LocalDateConverter.class) 读取数据后Validation校验,校验通过直接保

    2024年02月08日
    浏览(10)
  • java批量导入Excel数据

    java批量导入Excel数据

    1.后台导入代码 2.实体类 2.1设置表格下拉选项  3.vue前端导入功能代码

    2024年02月09日
    浏览(10)
  • 【前端】批量导入和导出Excel数据

    【前端】批量导入和导出Excel数据

    excel导入功能需要使用npm包 xlsx ,所以需要安装 xlsx 插件,读取和写入都依赖她 vue-element-admin模板提供了一个导入excel数据的文件,我们只需用即可 代码地址: https://github.com/PanJiaChen/vue-element-admin/blob/master/src/components/UploadExcel/index.vue 将vue-element-admin提供的导入功能新建一个组件

    2024年02月09日
    浏览(17)
  • 尚融宝10-Excel数据批量导入

    尚融宝10-Excel数据批量导入

    目录 一、数据字典 (一)、什么是数据字典 (二)、数据字典的设计 二、Excel数据批量导入 (一)后端接口 1、添加依赖 2、创建Excel实体类 3、创建监听器 4、Mapper层批量插入 5、Service层创建监听器实例 6、controller层接受客户端请求 7、添加mapper发布配置 8、Swagger接口测试

    2023年04月10日
    浏览(12)
  • MySQL批量导入Excel数据【超详细】

    MySQL批量导入Excel数据【超详细】

    查看此文章前强烈建议先看这篇文章:Java江湖路 | 专栏目录 今天遇到一个需求,批量更新数据库中全国各地的物流价格,产品经理很贴心的为我做好了数据表格,说是上一个技术也是这么做的,好,压力给到我这边。话不多说,直接上步骤。 1、准备好我们需要导入的excel表

    2024年02月05日
    浏览(15)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包