Insert multiple documents in Elasticsearch - bulk doc formatter

后端 未结 3 1127
梦毁少年i
梦毁少年i 2020-12-06 23:42

TLDR; How can I bulk format my JSON file for ingestion to Elasticsearch?

I am attempting to ingest some NOAA data into Elasticsearch and have been utilizing NOAA Py

3条回答
  •  既然无缘
    2020-12-07 00:06

    How about utilizing the bulk method of the official python client?

    import json
    
    from noaa_sdk import noaa
    from elasticsearch import Elasticsearch
    from elasticsearch.helpers import bulk
    
    
    noaa_client = noaa.NOAA()
    alerts = noaa_client.alerts()['features']
    
    es = Elasticsearch()
    
    
    def save_alerts():
        with open('nhc_alerts.json', 'w') as f:
            f.write(json.dumps(alerts))
    
    
    def bulk_sync():
        actions = [
            {
                "_index": "my_noaa_index",
                "_source": alert
            } for alert in alerts
        ]
    
        bulk(es, actions)
    
    
    save_alerts()
    bulk_sync()
    
    

提交回复
热议问题