Home > other >  Load JSON file to Elasticsearch
Load JSON file to Elasticsearch

Time:10-01

I have a json file and i want load them to elasticsearch, I have tried several ways but no success, can you help me? thank you.

CodePudding user response:

if you use Python programming you can try my method https://github.com/trannguyenhan/tiki-data-analysis/blob/master/load_json_to_elasticsearch.ipynb

pip install Elasticsearch

import requests, json, os
from elasticsearch import Elasticsearch

directory = '/home/trannguyenhan/json-tiki-data'
res = requests.get('http://localhost:9200')
print (res.content)
es = Elasticsearch([{'host': 'localhost', 'port': '9200'}])


for filename in os.listdir(directory):
    if filename.endswith(".json"):
        f = open(directory   "/"   filename)
        list_data = f.read().replace("}\n", "}||").split("||")
        
        # Send the data into es
        for data in list_data : 
            if "{" in data and "}" in data or data != "":
                try : 
                    es.index(index='tiki-data-json', ignore=400, doc_type='data', id=i, body=json.loads(data))
                except: 
                    continue
                i = i   1

Ref: https://github.com/trannguyenhan

  • Related