【问题标题】:Upload json file to elastic search using python使用python将json文件上传到弹性搜索
【发布时间】:2017-11-09 19:39:22
【问题描述】:

我尝试将带有 2 个文档的 JSON 文件上传到 ES。 我收到以下错误

ValueError: Invalid control character at: line 1 column 24 (char 23)    

我正在使用这个 python 代码:

import json
import os, sys

from elasticsearch import Elasticsearch

ES_CLUSTER = 'http://localhost:9200/' # Need PW and User name
ES_INDEX = 'test'
ES_TYPE = 'doc'

es = Elasticsearch(
    ['localhost'],
    http_auth=('elastic', 'changeme'),
    port=9200
)
es = Elasticsearch(ES_CLUSTER)
with open("C:\Users\office\Desktop\Elasticsearch data\E-commerce.json") as json_file:
    json_docs = json.load(json_file)
es.bulk(ES_INDEX, ES_TYPE, json_docs)

【问题讨论】:

    标签: python json elasticsearch


    【解决方案1】:

    尝试使用json.loads 而不是json.load 可能会起作用

    【讨论】:

      【解决方案2】:

      问题解决了,这是我使用的代码

      import json
      from pprint import pprint
      from elasticsearch import Elasticsearch
      es = Elasticsearch(
          ['localhost'],
          http_auth=('elastic', 'changeme'),
          port=9200
      
      )
      
      MyFile= file("C:\Users\office\Desktop\Elasticsearch data\E-commerce2.json",'r').read()
      ClearData = MyFile.splitlines(True)
      i=0
      json_str=""
      docs ={}
      for line in ClearData:
          line = ''.join(line.split())
          if line != "},":
              json_str = json_str+line
          else:
              docs[i]=json_str+"}"
              json_str=""
              print docs[i]
              es.index(index='test', doc_type='Blog', id=i, body=docs[i])
              i=i+1
      

      【讨论】:

        猜你喜欢
        • 2021-12-14
        • 2017-07-26
        • 2011-10-30
        • 2020-08-05
        • 2017-07-07
        • 2015-10-08
        • 2019-10-31
        • 2021-07-13
        • 1970-01-01
        相关资源
        最近更新 更多