Simple python client to load data into ES
Elastic search supports many languages for accessing ES in your code like below . It worth trying one of them to see how we can use it in our code .
- Java REST Client
- Java API
- JavaScript API
- Groovy API [2.4]
- .NET API [6.x]
- PHP API [6.0]
- Perl API
- Python API
- Ruby API
Considering python is a language having most versatile features for data science activities , it worth checking it out with ES.
below is the simple client code which will read from CSV file and createa Index in elastc search .
( Python is very indentation sensitive , so while copy pasting below code , make sure you indent it right before you run , better use some IDE for python which can help to indent it properly like spyder , canopy etc )
import csv
from collections import deque
import elasticsearch
from elasticsearch import helpers
def readRatings():
csvfile = open('emp.csv' , r)
reader = csv.DictReader( csvfile )
for line in reader:
staff = {}
player['id'] = int(line['id'])
player['fname'] = line['fname']
player['lname'] = line['lname']
yield staff
es = elasticsearch.Elasticsearch()
es.indices.delete(index="emp",ignore=404)
deque(helpers.parallel_bulk(es,readRatings(),index="emp",doc_type="staff"), maxlen=0)
es.indices.refresh()
==================================================================
Data it is using ( emp.csv)
id,fname,lname
1,Amol,VISHWARUPE
2,Sachin, TENDULKAR
,RAhul,DRAVID
==================================================================
put both the files in same folder for convenience ,
prerequisites
Elastic service is running
Kibana service is running ( not mandatory )
go to path where above filesa are kept and open CMD prompt there .
and run
C:\Home\amol\ : python3 emp.py
you can check your INDEX is created or not by simple command
curl -XGET 127.0.0.1:9200/emp/_Serach?pretty
or just check into KIBANA whether you got new INDEX or NOT