zoukankan      html  css  js  c++  java
  • 37.es中批量写入数据

    批量写入:

    # https://www.cnblogs.com/Neeo/articles/10788573.html可以看这个博客
    import time
    from elasticsearch import Elasticsearch
    from elasticsearch import helpers
    es = Elasticsearch(["127.0.0.1:9200"])
    
    def timer(func):
        def wrapper(*args, **kwargs):
            start = time.time()
            res = func(*args, **kwargs)
            print('共耗时约 {:.2f} 秒'.format(time.time() - start))
            return res
        return wrapper
    
    @timer
    def gen():
        """ 使用生成器批量写入数据 """
        with open("./word.txt", "r", encoding="utf-8") as f:
            actions = ({
                    "_index": "word",
                    "_type": "doc",
                    "_source": {   # 这个字段中写你需要输入的数据,因为是一个字典所以可以写入多个字段
                        "word": line.strip()
                    }
                } for line in f)  # 建议这里使用生成器,不要直接使用列表,没有写过大数据的可以试一下,如果是一个列表的话,这个时候你查看任务管理器,会发现内存被撑爆了,程序直接停止运行,同时电脑卡主,重启吧。
    
            helpers.bulk(es, actions=actions)
    gen()
    

    最后我这里写入了21万的数据,共耗时约 16.82 秒

  • 相关阅读:
    《骆驼祥子》
    《基督山伯爵》
    JDeveloper 假死问题
    JSP代码执行顺序
    Ajax简单案例(实现提交值到服务器值不刷新页面)
    JS和JSP之间值传递
    如何更好的编码
    Telnet 便捷执行脚本
    MyBatis 缓存
    MyBatis 中传递多个参数的 4 种方式
  • 原文地址:https://www.cnblogs.com/liuzhanghao/p/12701202.html
Copyright © 2011-2022 走看看