• 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 秒

  • 相关阅读:
    Java线程九:线程的调度-让步
    Java线程八:线程的调度-优先级
    Java线程七:线程的调度-休眠
    Java线程六:线程的交互
    丸の内の霊 5
    丸の内の霊 4
    丸の内の例 3
    丸の内の例 2
    幽霊物件 1
    質問力 D
  • 原文地址:https://www.cnblogs.com/liuzhanghao/p/12701202.html
Copyright © 2020-2023  润新知