cmunka
(cmunka)
August 7, 2021, 6:17pm
1
I am trying to upload a jsonl file to elastic search via _bulk api using Python. Below is my code.!
Error:
Data is having multiple json object:
##code ##
# -*- coding: utf-8 -*-
import requests
import json
url = "http://localhost:9200/tweets/_doc/_bulk"
data=[]
with open('bulk.json', 'rb') as json_file:
for jsonObj in json_file:
studentDict = json.loads(jsonObj)
data.append(studentDict)
headers = {
'Content-Type': 'application/x-ndjson', 'data':'binary'
}
response = requests.post( url, headers=headers, json=data)
print(response.text)
Please suggest me the solution for this. Thanks!
leandrojmp
(Leandro Pereira)
August 8, 2021, 3:27pm
2
You are not trying to upload json
, your data variable is a list, and you are trying to send this list to elasticsearch, which is not supported.
This is what you are trying to send:
[{json_object}, {json_object}, {json_object}, {json_object}, {json_object}]
You need to send in this format:
{json_object}
{json_object}
{json_object}
{json_object}
You will need to change your code.
The bellow line will convert a list wiht json elements into a string delimited by new lines:
bulk_data = '\n'.join(json.dumps(d) for d in data) + "\n"
I did not test it, but you could try to send this request:
response = requests.post( url, headers=headers, data=bulk_data)
But your problem is with your code, it is not related to elasticsearch, there are better places to get coding help.
warkolm
(Mark Walkom)
August 8, 2021, 11:19pm
3
Welcome to our community!
Please don't post pictures of text or code. They are difficult to read, impossible to search and replicate (if it's code), and some people may not be even able to see them
system
(system)
Closed
September 5, 2021, 11:19pm
4
This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.