I have a log file with a bunch of JSON records. Each line is a JSON record and I’m trying to send each line one at a time to Elasticsearch using the elastic post api. I’m using Elasticvue to view those logs.
I’ve tried the below code and it says that all the JSON records were imported when I ran the code. I see the name of the index that was created in Elasticvue, but I don’t see any records or logs when I open the index.
<code>import json
import requests
#Elasticsearch connection details
ELASTICSEARCH_HOST = ""
ELASTICSEARCH_USER = ""
ELASTICSEARCH_PASSWORD = “”
ELASTICSEARCH_DATA_STREAM = ""
def import_data(filename=""):
"""Reads JSON records from a file and imports them into Elasticsearch."""
with open(filename, "r") as f:
for line in f:
try:
data = json.loads(line)
url = f"{ELASTICSEARCH_HOST}/{ELASTICSEARCH_DATA_STREAM}/_doc"
headers = {"Content-Type": "application/json"}
response = requests.post(
url,
auth=(ELASTICSEARCH_USER, ELASTICSEARCH_PASSWORD),
headers=headers,
json=data
)
if response.status_code == 201:
print("Data imported successfully.")
else:
print(f"Error importing data: {response.text}")
except json.JSONDecodeError:
print(f"Skipping invalid JSON line: {line}")
if __name__ == "__main__":
import_data()
</code>
<code>import json
import requests
#Elasticsearch connection details
ELASTICSEARCH_HOST = ""
ELASTICSEARCH_USER = ""
ELASTICSEARCH_PASSWORD = “”
ELASTICSEARCH_DATA_STREAM = ""
def import_data(filename=""):
"""Reads JSON records from a file and imports them into Elasticsearch."""
with open(filename, "r") as f:
for line in f:
try:
data = json.loads(line)
url = f"{ELASTICSEARCH_HOST}/{ELASTICSEARCH_DATA_STREAM}/_doc"
headers = {"Content-Type": "application/json"}
response = requests.post(
url,
auth=(ELASTICSEARCH_USER, ELASTICSEARCH_PASSWORD),
headers=headers,
json=data
)
if response.status_code == 201:
print("Data imported successfully.")
else:
print(f"Error importing data: {response.text}")
except json.JSONDecodeError:
print(f"Skipping invalid JSON line: {line}")
if __name__ == "__main__":
import_data()
</code>
import json
import requests
#Elasticsearch connection details
ELASTICSEARCH_HOST = ""
ELASTICSEARCH_USER = ""
ELASTICSEARCH_PASSWORD = “”
ELASTICSEARCH_DATA_STREAM = ""
def import_data(filename=""):
"""Reads JSON records from a file and imports them into Elasticsearch."""
with open(filename, "r") as f:
for line in f:
try:
data = json.loads(line)
url = f"{ELASTICSEARCH_HOST}/{ELASTICSEARCH_DATA_STREAM}/_doc"
headers = {"Content-Type": "application/json"}
response = requests.post(
url,
auth=(ELASTICSEARCH_USER, ELASTICSEARCH_PASSWORD),
headers=headers,
json=data
)
if response.status_code == 201:
print("Data imported successfully.")
else:
print(f"Error importing data: {response.text}")
except json.JSONDecodeError:
print(f"Skipping invalid JSON line: {line}")
if __name__ == "__main__":
import_data()
New contributor
Asritha Korrapati is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.