-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelastic_importer.py
More file actions
92 lines (87 loc) · 2.44 KB
/
elastic_importer.py
File metadata and controls
92 lines (87 loc) · 2.44 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from elasticsearch import helpers, Elasticsearch
import csv
es = Elasticsearch()
with open('./cas.csv') as f:
index_name = 'cas_neto'
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": {
"dobcine": {
"type": "text"
},
"2005": {
"type": "float",
},
"2006": {
"type": "float",
},
"2007": {
"type": "float",
},
"2008": {
"type": "float",
},
"2009": {
"type": "float",
},
"2010": {
"type": "float",
},
"2011": {
"type": "float",
},
"2012": {
"type": "float",
},
"2013": {
"type": "float",
},
"2014": {
"type": "float",
},
"2015": {
"type": "float",
},
"2016": {
"type": "float",
},
"2017": {
"type": "float",
},
"2018": {
"type": "float",
}
}
}
}
)
for row in reader:
try:
if(index == 0):
headers = row
else:
obj = {}
for i, val in enumerate(row):
if(i > 1):
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:
print('error: ' + str(e) + ' in' + str(index))
index = index + 1
f.close()