Last active
November 18, 2016 18:36
-
-
Save simonw/99d2c60f0218b680d59b4622bc5f9d37 to your computer and use it in GitHub Desktop.
Download and dump contents of a remote Elasticsearch index to a local newline-separated JSON file using Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from elasticsearch import Elasticsearch | |
| from elasticsearch.helpers import scan | |
| import json | |
| REMOTE_URL = 'https://username:password@elasticsearch.example.com:9200/' | |
| LOCAL_FILE = '/tmp/elasticsearch-dump.txt' | |
| INDEX = 'my-index' | |
| DOC_TYPE = 'document' | |
| client = Elasticsearch([REMOTE_URL]) | |
| fp = open(LOCAL_FILE, 'w') | |
| for row in scan(client, query={}, index=INDEX, doc_type=DOC_TYPE): | |
| json.dump(row, fp) | |
| fp.write("\n") | |
| fp.flush() # So you can tail -f the file | |
| fp.close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment