This repository has been archived by the owner on Jul 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
kafka_publish.sh
executable file
·53 lines (46 loc) · 1.96 KB
/
kafka_publish.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
#!/bin/bash
##############################################################################
# Copyright 2017 IBM Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##############################################################################
source local.env
#############################################################################
# Posts a message to a designated topic
# ARGS:
# 1 - Topic name
# 2 - Payload (must be encoded in base64)
# RETURNS:
# Nothing
#############################################################################
post()
{
local TOPIC=$1
local DATA='{"records":[{"value":"'${2}'"}]}'
# See details here: http://docs.confluent.io/1.0/kafka-rest/docs/intro.html
# Produce a message using PAYLOAD to the kafka topic TOPIC
curl -X POST -H "Content-Type: application/vnd.kafka.binary.v1+json" \
-H "X-Auth-Token: $API_KEY" \
--data "$DATA" \
"$KAFKA_REST_URL/topics/$TOPIC"
}
#############################################################################
# MAIN
#############################################################################
echo "Encoding payload from file 'events.json'..."
PAYLOAD=$( base64 events.json | tr -d '\n' | tr -d '\r' ) # Note that it is important to disable line wrapping
NUM_MSGS=2
for ((i=0; i<$NUM_MSGS; i++)); do
echo -e "\nPosting a message #$i into the topic '$SRC_TOPIC'"
post $SRC_TOPIC "$PAYLOAD" # Note that PAYLOAD has spaces in in, so need to pass it in quotes
done
echo -e "\nAll done!"