Created
April 24, 2015 10:36
-
-
Save OscarGalindo/be3d9d78faed6bdd46ca to your computer and use it in GitHub Desktop.
CSV to Elasticsearch Bulk Data
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
| import pyelasticsearch as pyes | |
| import csv | |
| URL_ES = 'http://localhost:9200/' | |
| CSV_FILE = 'example.csv' | |
| INDEX = 'Index' | |
| TYPE = 'Type' | |
| ID_FIELD = 'id' | |
| f = open(CSV_FILE) | |
| rows = csv.DictReader(f) | |
| print('Connecting to elastic...', end="") | |
| es = pyes.ElasticSearch(URL_ES) | |
| print('Done!') | |
| actions = [] | |
| for row in rows: | |
| actions.append(row) | |
| while len(actions) > 10000 and len(actions) % 10000 == 0: | |
| print('Inserting bulk data...', end="") | |
| es.bulk_index(INDEX, TYPE, actions, id_field=ID_FIELD) | |
| print('done!') | |
| del actions[0:len(actions)] | |
| break | |
| if len(actions) > 0: | |
| print('Inserting bulk data...', end="") | |
| es.bulk_index(INDEX, TYPE, actions, id_field=ID_FIELD) | |
| print('done!') | |
| del actions[0:len(actions)] | |
| print('Finished.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment