使用Python将JSON文件上传到Elasticsearch

4

我尝试将包含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)
2个回答

0

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

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

0

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接