简体   繁体   中英

ElasticSearch using python

I am using python to import file into 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

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. 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. If you make the following change:

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

it should resolve the JsonParseException error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM