-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathES_Upload.py
66 lines (52 loc) · 1.62 KB
/
ES_Upload.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
from opensearchpy import OpenSearch
import json
import sys
filename = sys.argv[1]
def payload_constructor(data, action):
action_string = json.dumps(action) + "\n"
payload_string = ""
count = 0
for line in data:
payload_string += action_string
this_line = json.dumps(line) + "\n"
payload_string += this_line
count += 1
if count == 100:
client.bulk(body=payload_string, index="ocm-api-data")
count = 0
payload_string = ""
return payload_string
# Create the client with SSL/TLS enabled, but hostname verification disabled.
client = OpenSearch(
hosts=[{'host': 'search-perfscale-dev-chmf5l4sh66lvxbnadi4bznl3a.us-west-2.es.amazonaws.com',
'port': 443}],
http_compress=True, # enables gzip compression for request bodies
# http_auth = ('<username>', '<password>'),
use_ssl=True,
verify_certs=False,
timeout=60
# max_retries=10,
# retry_on_timeout=True
)
# To check if connected to server
if not client.ping():
raise ValueError("Connection failed")
with open(filename) as f:
for line in f:
data = json.loads(line)
# Created index
client.indices.create(index="ocm-api-data", ignore=400)
# Document appended to the json file,for bulk uploading to the ES server.
action = {
"index": {
"_index": "ocm-api-data"
}
}
# For Bulk Upload
payload = payload_constructor(data, action)
# To check if all the data is uploaded
if not payload:
print("Successful")
else:
response = client.bulk(body=payload_constructor(data, action),
index="ocm-api-data")