-
Notifications
You must be signed in to change notification settings - Fork 0
/
elastic_importer2.py
59 lines (55 loc) · 1.53 KB
/
elastic_importer2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from elasticsearch import helpers, Elasticsearch
import csv
es = Elasticsearch()
with open('./neki.csv') as f:
index_name = 'vse_dejavnosti_prav5'
doctype = 'placa'
reader = csv.reader(f)
headers = []
index = 0
es.indices.delete(index=index_name, ignore=[400, 404])
es.indices.create(index=index_name, ignore=400)
es.indices.put_mapping(
index=index_name,
doc_type=doctype,
ignore=400,
body={
doctype: {
"properties": {
"DEJAVNOST": {
"type": "text"
},
"MESEC": {
"type": "text",
},
"BRUTO_PLACA": {
"type": "float",
},
"NETO_PLACA": {
"type": "float",
}
}
}
}
)
for row in reader:
try:
if(index == 0):
headers = row
else:
obj = {}
for i, val in enumerate(row):
if(i == 2 or i == 3):
obj[headers[i]] = float(val)
#print(i, val)
else:
obj[headers[i]] = val
#print(i, val)
#put document into elastic search
es.index(index=index_name, doc_type=doctype, body=obj)
#print(obj)
except Exception as e:
a=0
#print('error: ' + str(e) + ' in' + str(index))
index = index + 1
f.close()