【问题标题】:Elasticsearch bulk/batch indexing with python requests module使用 python 请求模块进行 Elasticsearch 批量/批量索引
【发布时间】:2012-12-09 10:02:28
【问题描述】:

我有一个小的 (~50,00) json 字典数组,我想在 ES 中存储/索引。我的偏好是使用 python,因为我要索引的数据来自 csv 文件,通过 python 加载并转换为 json。或者,我想跳过转换为 json 的步骤,而只需使用我拥有的 python 字典数组。无论如何,快速搜索揭示了 ES 的批量索引功能。我想做这样的事情:

post_url = 'http://localhost:9202/_bulk'
request.post(post_url, data = acc )    # acc a python array of dictionaries

post_url = 'http://localhost:9202/_bulk'
request.post(post_url, params = acc )    # acc a python array of dictionaries

两个请求都给出 [HTTP 500 错误]

【问题讨论】:

    标签: indexing elasticsearch python-requests


    【解决方案1】:

    我的理解是每行必须有一个“命令”(索引、创建、删除...),然后其中一些(如索引)在下一行获取一行数据,就像这样

    {'index': ''}\n
    {'your': 'data'}\n
    {'index': ''}\n
    {'other': 'data'}\n
    

    注意换行符,即使在最后一行。

    如果你 POST 到 ../index/type/_bulk 或者你需要指定索引和类型我认为没有尝试过,那么像上面这样的空索引对象有效。

    【讨论】:

      【解决方案2】:

      您可以使用以下功能:

      def post_request(self, endpoint, data):
         endpoint = 'localhost:9200/_bulk'
         response = requests.post(endpoint, data=data, headers={'content-type':'application/json', 'charset':'UTF-8'})
      
         return response
      

      作为数据,您需要传递这样的字符串:

      { "index" : { "_index" : "test-index", "_type" : "_doc", "_id" : "1681", "routing" : 0 }}
      { "field1" : ... , ..., "fieldN" : ... }
      { "index" : { "_index" : "test-index", "_type" : "_doc", "_id" : "1684", "routing" : 1 }}
      { "field1" : ... , ..., "fieldN" : ... }
      

      确保在每行末尾添加“\n”。

      【讨论】:

      • 你的数据的数据类型是什么,不应该是字典列表吗?
      【解决方案3】:

      我对 Python 了解不多,但你看过Pyes 吗? Pyes 支持批量处理。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-16
        • 2015-09-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-16
        相关资源
        最近更新 更多