[英]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}}; ")
查看您的代碼,您的JSON似乎有2個錯誤。 在它的原始版本中,它產生以下字符串:
{"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錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.