-
Notifications
You must be signed in to change notification settings - Fork 2
/
elastic_upload.py
37 lines (29 loc) · 1.06 KB
/
elastic_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
from elasticsearch import Elasticsearch
import argparse
import json
from sys import stdin
parser = argparse.ArgumentParser('Takes a JSON object from input stream \
and uploads the object to an Elastic server')
parser.add_argument('--address',
type=str,
help='IP address of the server running the database',
required=True)
parser.add_argument('--port',
type=int,
help='Port used by the database',
required=True)
parser.add_argument('--index',
type=str,
help='Index that data is uploaded to',
required=True)
args = parser.parse_args()
elastic = Elasticsearch(['{}:{}'.format(args.address, args.port)])
upload_data = json.loads(stdin.read())
bulk_body = ''
for entry in upload_data:
bulk_body += '{"create": {}}\n'
bulk_body += json.dumps(entry) + '\n'
response = elastic.bulk(bulk_body, args.index, 'entry')
errors = response.items()[1][1]
if errors:
exit(1)