ElasticSearch using python



我正在使用python将文件导入ElasticSearch。简单的数据,我可以导入,但面临的问题,当有字母和数字的组合,以及特殊字符。

我使用下面的脚本:

from datetime import datetime
from elasticsearch import Elasticsearch
import os
es = Elasticsearch([{'host': 'localhost', 'port': 9200}])
f = open("E:\ElasticSearch\test.txt",'r')
fulldata = f.readlines()
f.close()
del fulldata[0]
for line in fulldata:
  array = line.split(",")
  guid = array[0]
  senderid = array[1]
  campaign = array[2]
  json_body = "{"guid" : ""+ guid+"", "senderid" : ""+ senderid+"", "campaign" : "+ str(campaign)+"}}"
  print json_body
  res = es.index(index="mytest", doc_type="msg", id=guid, body=json_body)

test.txt文件包含如下数据

guid    senderid campaign
26fac319-604b-11e5-b1fe,003001,Weekday_EGV_21Sept_4pm_Round2_Tier1_Part1,

我得到类似

的错误
elasticsearch.exceptions.RequestError: TransportError<400, u"MapperParsingException [failed to parse ]; nested: JsonParseException [Unrecognized token 'Weekday_EGV_21Sept_4pm_Round2_Tired1_Part1' : was excepting ('true', 'false' or 'null')n at [Source: [B@b5685ce; line: 1, column:95}}; ")

看看你的代码,似乎有2个错误与你的JSON。在它的原始for中,它产生以下字符串:

{"guid" : "26fac319-604b-11e5-b1fe", "senderid" : "003001", "campaign" : Weekday_EGV_21Sept_4pm_Round2_Tier1_Part1}}

您在campaign的最终字段值周围缺少引号,并且您在末尾有一个额外的}。如果您做了以下更改:

json_body = "{"guid" : ""+ guid+"", "senderid" : ""+ senderid+"", "campaign" : ""+ str(campaign)+""}"

它应该解决JsonParseException错误

最新更新