-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetJDBCSourceConnector.sh
More file actions
executable file
·214 lines (191 loc) · 4.77 KB
/
Copy pathsetJDBCSourceConnector.sh
File metadata and controls
executable file
·214 lines (191 loc) · 4.77 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/bin/bash
# This script configures a JDBC Source Connector to a MYSQL Database in the
# My-TRAC Platform.
function print_usage {
echo " Usage: setJDBCConnector.sh <Options>"
echo " Options: "
echo " -k|--kafka-connect <kafka connect host> (required)"
echo " -m|--mysql <mysql host> (required)"
echo " -d|--database <the database to connecto to> (required)"
echo " -u|--user <the user> (required)"
echo " -p|--password <the password> (required)"
echo " -c|--connector-name <the name of the connector> (required)"
echo " -i|--incrementing-column <the incrementing column primary key> (required)"
echo " -t|--timestamp-column <the timestamp column> (required)"
echo " -n|--names <comma separated list with the topic names to source> (Default :\"\" //all )"
echo " -f|--prefix <the prefix to prepend to the toic name> (Default:\"\")"
echo " -kp|--kafka-connect-port <kafka-connect port> (Default: 28083)"
echo " -mp|--mysql-port <MySQL port> (Default: 3306)"
}
KAFKA_CONNECT_HOST_=
MYSQL_HOST_=
DATABASE_=
USER_=
PASSWORD_=
CONNECTOR_NAME_=
INCREMENTING_NAME_=
TIMESTAMP_NAME_=
TOPIC_NAMES_=""
TOPIC_PREFIX_=""
MYSQL_PORT_=3306
KAFKA_CONNECT_PORT_=28083
HELP_=
#Capture TOPICS
arguments=$@
if [[ $arguments == *"-n"* ]]; then
index1=$(echo $arguments | grep -b -o "\-n" |cut -d: -f1)
index1=$(($index1 + 3))
index2=$(echo ${arguments:$index1} | grep -b -o "\-" | head -n1 |cut -d: -f1)
index2=$(($index2 - 1))
TOPIC_NAMES_=${arguments:$index1:$index2}
else
TOPIC_NAMES_=""
fi
# options parsing
while [[ $# > 0 ]]
do
key="$1"
case $key in
-h|--help)
HELP_=YES
;;
-k|--kafka-connect)
KAFKA_CONNECT_HOST_="$2"
shift # past argument
;;
-m|--mysql)
MYSQL_HOST_="$2"
shift # past argument
;;
-d|--database)
DATABASE_="$2"
shift # past argument
;;
-u|--user)
USER_="$2"
shift # past argument
;;
-p|--password)
PASSWORD_="$2"
shift # past argument
;;
-c|--connector-name)
CONNECTOR_NAME_="$2"
shift # past argument
;;
-i|--incrementing-column)
INCREMENTING_COLUMN_="$2"
shift # past argument
;;
-t|--timestamp-column)
TIMESTAMP_COLUMN_="$2"
shift # past argument
;;
-n|--names)
#TOPIC_NAMES_="$2"
shift # past argument
;;
-f|--prefix)
TOPIC_PREFIX_="$2"
shift # past argument
;;
-kp|--kafka-connect-port)
KAFKA_CONNECT_PORT_="$2"
shift #past argument
;;
-mp|--mysql-port)
MYSQL_PORT_"$2"
shift #past argument
;;
esac
shift
done
if [[ ! -z $HELP ]]
then
print_usage
exit 1
fi
if [[ -z $KAFKA_CONNECT_HOST_ ]]; then
echo "--kafka-connect not set"
print_usage
exit 1
fi
if [[ -z $MYSQL_HOST_ ]]; then
echo "--mysql not set"
print_usage
exit 1
fi
if [[ -z $DATABASE_ ]]; then
echo "--database not set"
print_usage
exit 1
fi
if [[ -z $USER_ ]]; then
echo "--user not set"
print_usage
exit 1
fi
if [[ -z $PASSWORD_ ]]; then
echo "--password not set"
print_usage
exit 1
fi
if [[ -z $CONNECTOR_NAME_ ]]; then
echo "--connector-name not set"
print_usage
exit 1
fi
if [[ -z $INCREMENTING_COLUMN_ ]]; then
echo "--incrementing-column not set"
print_usage
exit 1
fi
if [[ -z $TIMESTAMP_COLUMN_ ]]; then
echo "--timestamp-column not set"
print_usage
exit 1
fi
#if [[ -z $TOPIC_NAMES_ ]]; then
# echo "--names not set"
# print_usage
# exit 1
#fi
echo ""
echo "Adding JDBC SOURCE connector with options:"
echo "kafka-connect = $KAFKA_CONNECT_HOST_"
echo "mysql = $MYSQL_HOST_"
echo "database = $DATABASE_"
echo "user = $USER_"
echo "password = $PASSWORD_"
echo "connector-name = $CONNECTOR_NAME_"
echo "incrementing-column = $INCREMENTING_COLUMN_"
echo "timestamp-column = $TIMESTAMP_COLUMN_"
echo "names = $TOPIC_NAMES_"
echo "prefix = $TOPIC_PREFIX_"
echo ""
echo ""
CONNECTOR="{
\"name\": \"$CONNECTOR_NAME_\",
\"config\": {
\"connector.class\": \"io.confluent.connect.jdbc.JdbcSourceConnector\",
\"tasks.max\": 1,
\"connection.url\":\"jdbc:mysql://$MYSQL_HOST_:$MYSQL_PORT_/$DATABASE_?user=$USER_&password=$PASSWORD_\",
\"mode\": \"timestamp+incrementing\",
\"incrementing.column.name\": \"$INCREMENTING_COLUMN_\",
\"timestamp.column.name\": \"$TIMESTAMP_COLUMN_\",
\"topic.prefix\": \"$TOPIC_PREFIX_\",
\"poll.interval.ms\": 1000,
\"table.whitelist\":\"$TOPIC_NAMES_\",
\"transforms\":\"createKey,extractLong\",
\"transforms.createKey.type\":\"org.apache.kafka.connect.transforms.ValueToKey\",
\"transforms.createKey.fields\":\"$INCREMENTING_COLUMN_\",
\"transforms.extractLong.type\":\"org.apache.kafka.connect.transforms.ExtractField"'$Key'"\",
\"transforms.extractLong.field\":\"$INCREMENTING_COLUMN_\"}
}"
echo $CONNECTOR | curl -X POST \
-H "Content-Type: application/json" \
--data @- \
$KAFKA_CONNECT_HOST_:$KAFKA_CONNECT_PORT_/connectors
echo ""
echo ""
exit 0