-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpcb_es.py
69 lines (68 loc) · 1.65 KB
/
pcb_es.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
60
61
62
63
64
65
66
67
68
69
import json
import sys
import uuid
import getopt
from elasticsearch import Elasticsearch
es=Elasticsearch()
indexname=''
ifile=''
ofile=''
try:
i,j = getopt.getopt(sys.argv[1:],'i:f:o:',["index=","input=","output="])
except getopt.GetoptError:
print('Usage: pcb_to_es1.py --index=<index_name> --input=<json_file> --output=<formatted_json>')
sys.exit(2)
if len(sys.argv) <= 1:
print('Usage: pcb_to_es1.py --index=<index_name> --input=<json_file> --output=<formatted_json>')
sys.exit(2)
for opt,arg in i:
if opt in ['-i','--index']:
indexname=arg
elif opt in ['-f','--input']:
ifile=arg
elif opt in ['-o','--output']:
ofile=arg
#
# print(ifile)
# print(ofile)
headers={'Content-Type': 'application/json'}
f=open(ifile,'r')
f1=open(ofile,'w')
final_data=[]
for i in f:
x=json.loads(i) # returns a dictionary
labels_json=json.dumps(x["labels"])
new_labels1=json.loads(labels_json)
dict1={}
for i in new_labels1.split('|,'):
d1={}
j=i.replace("|",'')
k=j.split(':')
d1[k[0]]=k[1]
dict1.update(d1)
x.pop('labels')
x.update(dict1)
data=json.dumps(x)
data1=json.loads(data)
final_data.append(data)
r = es.index(index=indexname,doc_type='perfmetrics',id=uuid.uuid4(),body=data)
print(r)
#print(r.content)
for i in final_data:
f1.write(i)
f1.write("\n")