-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-es-index-schema.py
164 lines (126 loc) · 4.75 KB
/
generate-es-index-schema.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# -*- coding: utf-8 -*-
#
# Copyright (C) 2020 CHAOSS
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Authors:
# Animesh Kumar <animuz111@gmail.com>
# Venu Vardhan Reddy Tekula <venuvardhanreddytekula8@gmail.com>
#
import logging
import json
import warnings
import sys
import pandas as pd
import argparse
from argparse import RawTextHelpFormatter
from elasticsearch import Elasticsearch, RequestsHttpConnection, ConnectionError
LOG_FORMAT = "[%(asctime)s] - %(message)s"
DEBUG_LOG_FORMAT = "[%(asctime)s - %(name)s - %(levelname)s] - %(message)s"
HEAD_COLUMN = ['name', 'type', 'aggregatable', 'description']
DICT_INDEX_FIELDS = {}
def configure_logging(debug):
"""
Configure logging.
This function sets basic attributes for logging.
:param debug: set the debug mode
"""
if not debug:
logging.basicConfig(level=logging.INFO,
format=LOG_FORMAT)
else:
logging.basicConfig(level=logging.DEBUG,
format=DEBUG_LOG_FORMAT)
def parse_args():
"""
Setup command line argument parsing with argparse.
"""
parser = argparse.ArgumentParser(
description="generate es index schema script argument parser",
formatter_class=RawTextHelpFormatter
)
parser.add_argument('index_name', help="es index name")
parser.add_argument("-f", "--file",
default="schema.csv",
help="schema file\n\n")
parser.add_argument("-d", "--debug",
action='store_true',
help="set debug mode for logging\n\n")
return parser.parse_args()
def get_mapping(index_name):
mapping_items = None
try:
# es = Elasticsearch()
logging.debug("connecting to elasticsearch using client")
es = Elasticsearch("https://admin:admin@localhost:9200",
verify_certs=False,
connection_class=RequestsHttpConnection)
mapping = es.indices.get_mapping(index_name)
mapping_items = mapping[index_name]['mappings']['items']
except ConnectionError:
logging.error("there is some problem connecting to es")
return mapping_items
def generate_schema(items, prefix):
fields = items['properties']
names = list(fields.keys())
for name in names:
if 'type' in fields[name].keys():
if prefix != '':
line = [prefix + "." + name]
else:
line = [name]
line.extend([fields[name]['type'], "true", "'NA'"])
DICT_INDEX_FIELDS[prefix + "." + name] = line
elif 'properties' in fields[name].keys():
generate_schema(fields[name], str(name))
def create_schema_file(source_file):
# convert the dictionary to a dataframe and sort base on 'name'
df = pd.DataFrame(columns=HEAD_COLUMN, data=list(DICT_INDEX_FIELDS.values()))
df.sort_values('name')
# convert the dataframe to a csv
df.to_csv(source_file, sep=',', index=False)
def main():
"""
A script for generating schema template of an index.
The script can be run via the command line:
$ python3 generate-es-index-schema.py index_name
"""
args = parse_args()
index_name = str(args.index_name)
source_file = str(args.file) if args.file else "schema.csv"
configure_logging(args.debug)
logging.info("start")
logging.info("fetching mapping for " + index_name)
mapping_items = get_mapping(index_name)
logging.info("mapping fetched")
if mapping_items and mapping_items is not None:
pass
else:
logging.error("mapping items is empty, please check the es connection")
sys.exit(0)
logging.info("generating schema for " + index_name)
generate_schema(mapping_items, '')
logging.info("schema generated")
logging.info("generating schema file, " + source_file)
create_schema_file(source_file)
logging.info("schema file generated")
if __name__ == '__main__':
try:
warnings.filterwarnings("ignore")
main()
logging.info("done!")
except KeyboardInterrupt:
sys.stderr.write("\n\nReceived Ctrl-C or other break signal. Exiting.\n")
sys.exit(0)