Unable to send json data to elastic search

I'm able to read data from mysql and print in json format but unable to send json data to elastic search.
Below is my piece of code in python

def exportTable(table_name):
query = "SELECT * FROM "+table_name
data = run(query)
jsondata = (json.dumps(data, indent=4, sort_keys=True, default=str))
print jsondata
print("Exporting MySQL table - "+table_name+" : "+str(len(data))+" rows")
for row in jsondata:
#print row
res = es.bulk(index = "test8", body = row, refresh = True)
print (res)

I get below as output:

elasticsearch.exceptions.RequestError: TransportError(400, u'illegal_argument_exception', u'Malformed action/metadata line [1], expected START_OBJECT but found [START_ARRAY]')

What is in a row? Could you share one example ?

Thanks for your reply. My json output is like below. I'm trying to insert each data set into elastic search.

[
{
"action": "enter",
"example": "app~textbox~$username_editbox~enter~lbcleader1",
"idactions": 1,
"info": "It enters value on the web control like editbox description etc"
},
{
"action": "set",
"example": "app~textbox~$username_editbox~set~lbcleader1",
"idactions": 2,
"info": "It sets value on the web control like editbox description etc"
}
]

This is not a valid JSON for elasticsearch for the BULK API.
See https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html

Thank you for reply. I'm new to concepts of elastic search and hence was trying with bulk.

Is there any best way to push data from mysql to elastic search using python. Can you please suggest.

Bulk API is the right way to do it. Just adapt your code to generate what the Bulk API is expecting.

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.