Skip to content

Instantly share code, notes, and snippets.

@OscarGalindo
Created April 24, 2015 10:36
Show Gist options
  • Select an option

  • Save OscarGalindo/be3d9d78faed6bdd46ca to your computer and use it in GitHub Desktop.

Select an option

Save OscarGalindo/be3d9d78faed6bdd46ca to your computer and use it in GitHub Desktop.
CSV to Elasticsearch Bulk Data
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