Insert multiple documents in Elasticsearch - bulk doc formatter

后端 未结 3 1135
梦毁少年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:20

    The problem is that the alerts JSON dump is all on a single line, so it's not gonna work as it is. You need to iterate on all the alerts (I suspect whatever is in the alerts.features array) and do it all in one shot without going through an intermediary file, like this:

    n = noaa.NOAA()
    alerts = n.alerts()
    f = open('nhc_alerts.json', 'w')
    for alert in alerts['features']:
      f.write('%s\n' % json.dumps({'index': {}}));
      f.write('%s\n' % json.dumps(alert, indent=0).replace('\n', ''))
    f.write('\n')
    

提交回复
热议问题