-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerator
executable file
·70 lines (56 loc) · 1.63 KB
/
generator
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
#!/usr/bin/python
import json
import sys
import os
def write_bulk(documents, header, filepath):
write_file = open(filepath, 'w+')
try:
for document in documents:
write_file.write(header + '\n')
write_file.write(json.dumps(document) + '\n')
except:
print("Something went wrong during document parsing.")
write_file.close()
exit - 3
finally:
write_file.close()
def assert_file_exists(file_path):
if not os.path.exists(file_path):
print("The file {} does not exit.".format(file_path))
exit - 2
if len(sys.argv) < 2:
print(
"Elastic Search Bulk API data formatter.\nUsage:\ngenerator source [destination] [index] [type]\n\nThe source should contain JSON objects in an array.\nCheckout http://www.json-generator.com/ to quickly generate data.\nIt only supports insert action."
)
exit(0)
try:
outputPath = sys.argv[2]
except IndexError:
outputPath = None
try:
indexName = sys.argv[3]
except IndexError:
indexName = None
try:
typeName = sys.argv[4]
except IndexError:
typeName = None
path = sys.argv[1]
assert_file_exists(path)
header = {"index": {}}
if indexName:
header['index']['_index'] = indexName
if typeName:
header['index']['_type'] = typeName
operationHeaderJson = json.dumps(header)
loadFile = open(path)
data = json.load(loadFile)
loadFile.close()
if outputPath is not None:
write_bulk(data, operationHeaderJson, outputPath)
exit(1)
else:
name = os.path.basename(path)
fileName = './{filename}.bulk'.format(filename=name)
write_bulk(data, operationHeaderJson, fileName)
exit(0)