-
Notifications
You must be signed in to change notification settings - Fork 2
/
mapping_upd.sh
117 lines (99 loc) · 2.08 KB
/
mapping_upd.sh
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
#!/bin/bash
# Author: Maxim Vasilev <[email protected]>
# Description: Script to update elasticsearch mappings
# Raise an error in case of unbound var
set -u
myname=`basename $0`
###
# Options
###
es_url_base=${ES_URL-"http://elasticsearch:9200"}
es_url_mapping="_mappings/_doc"
es_url_open="_open"
es_url_close="_close"
es_url_settings="_settings"
es_indices=(
"stores"
"products"
)
es_mapping_dir="/kafka-elastic-sink-connector/mapping"
# Path to log file
log_path="/var/log/$myname.log"
log_applications=false
###
# Globs
###
# Error codes
E_MISC=20
E_BAD_ARGS=21
###
# Functions
###
logEvent() {
timestamp=`date -R`
log_msg="$@"
echo "[$timestamp] $log_msg" >> $log_path
}
indexExist() {
index=$1
curl -sif "${es_url_base%%/}/${index}"
}
indexCreate() {
index=$1
mapping=`cat ${es_mapping_dir%%/}/${index}.json`
index_json="{
\"mappings\": {
\"_doc\": $mapping
}
}"
curl -si \
-X PUT \
-H 'Content-Type: application/json' \
"${es_url_base%%/}/${index}" \
-d "$index_json"
}
mappingUpdate() {
index=$1
curl -si \
-X PUT \
-H 'Content-Type: application/json' \
-d @${es_mapping_dir%%/}/${index}.json \
"${es_url_base%%/}/${index%%/}/${es_url_mapping%%/}"
}
settingsUpdate() {
index=$1
curl -si \
-X POST \
-H 'Content-Type: application/json' \
"${es_url_base%%/}/${index%%/}/${es_url_close%%/}"
curl -si \
-X PUT \
-H 'Content-Type: application/json' \
-d @${es_mapping_dir%%/}/${index}-settings.json \
"${es_url_base%%/}/${index%%/}/${es_url_settings%%/}"
curl -si \
-X POST \
-H 'Content-Type: application/json' \
"${es_url_base%%/}/${index%%/}/${es_url_open%%/}"
}
###
# main()
###
# Redirect output
if [ "$log_applications" = "true" ]
then
exec >> "$log_path"
exec 2>> "$log_path"
fi
for index in ${es_indices[@]}
do
indexExist $index
if [[ $? = 0 ]]
then
settingsUpdate $index
mappingUpdate $index
else
indexCreate $index
fi
done
exit 0