Last active
June 8, 2017 03:03
-
-
Save andrisasuke/2db82f1eb84e10b9dc7a53a408da4672 to your computer and use it in GitHub Desktop.
Python read csv and put to elasticsearch
This file contains 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 csv | |
import requests | |
f = open('station.csv', 'rb') | |
reader = csv.reader(f) | |
def _generate_header(): | |
return {'Content-type': 'application/json'} | |
def put_to_elasticsearch(json_body): | |
try: | |
url = 'http://localhost:9200/cari_parkir_poi/poi_location/' + str(json_body['id']) | |
r = requests.post(url, json=json_body, headers=_generate_header()) | |
if r.status_code == 201: | |
return r.json() | |
else: | |
return None | |
except Exception as e: | |
print 'getting error put to ES ', e.message | |
return None | |
for row in reader: | |
json = {'id': int(row[0]), 'name': row[1], 'type': row[2], 'location': {'lat': float(row[3]), 'lon': float(row[4])}} | |
result = put_to_elasticsearch(json_body=json) | |
print 'put result ', result | |
f.close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment