[英]ElasticSearch using python
I am using python to import file into ElasticSearch
. 我正在使用python将文件导入
ElasticSearch
。 Simple data I can Import but facing issue when there is combination of letters and numbers as well special characters. 我可以导入简单数据,但是当字母和数字以及特殊字符组合时会遇到问题。
I am using below script: 我正在使用以下脚本:
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 file contain data like test.txt文件包含如下数据
guid senderid campaign
26fac319-604b-11e5-b1fe,003001,Weekday_EGV_21Sept_4pm_Round2_Tier1_Part1,
I am getting error like 我收到类似的错误
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}}; ")
Looking at your code it appears there are 2 errors with your JSON. 查看您的代码,您的JSON似乎有2个错误。 In it's original for it produces the follwoing string:
在它的原始版本中,它产生以下字符串:
{"guid" : "26fac319-604b-11e5-b1fe", "senderid" : "003001", "campaign" : Weekday_EGV_21Sept_4pm_Round2_Tier1_Part1}}
You are missing quotes around that final field value of campaign
and you have an extra }
at the end. 您在
campaign
最终字段值附近缺少引号,并且结尾处有一个额外的}
。 If you make the following change: 如果进行以下更改:
json_body = "{\"guid\" : \""+ guid+"\", \"senderid\" : \""+ senderid+"\", \"campaign\" : \""+ str(campaign)+"\"}"
it should resolve the JsonParseException error. 它应该解决JsonParseException错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.