安装脚本
#!/bin/bash
# auto install kafka
echo "========= Start to install kafka =============="
function install_kafka(){
if [ -f "./kafka_2.12-2.0.0.gz" ];then
echo "Start installing kafka"
isopen9=$(firewall-cmd --query-port=9092/tcp)
isopen2=$(firewall-cmd --query-port=2181/tcp)
if [ 'no' == $isopen9 ];then
echo "open 9092"
addresult=$(firewall-cmd --add-port=9092/tcp --permanent)
reloadresult=$(firewall-cmd --reload)
else
echo "port 9092 already opened"
fi
if [ 'no' == $isopen2 ];then
echo "open 2181"
addresult=$(firewall-cmd --add-port=2181/tcp --permanent)
reloadresult=$(firewall-cmd --reload)
else
echo "port 2181 already opened"
fi
mkdir -p /opt/ops/kafka/logs
kafkahome=/opt/ops/kafka
tar -zxf kafka_2.12-2.0.0.gz -C $kafkahome
localip=$(/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:")
#sed -i "s#host.name=#host.name=$localip#g" $kafkahome/kafka_2.12-2.0.0/config/server.properties
sed -i "s@#advertised.*@advertised.listeners = PLAINTEXT://$localip:9092@g" $kafkahome/kafka_2.12-2.0.0/config/server.properties
sed -i "s@log.dirs=/tmp/kafka-logs@log.dirs=$kafkahome/logs@g" $kafkahome/kafka_2.12-2.0.0/config/server.properties
sed -i "s@num.partitions=1@num.partitions=10@g" $kafkahome/kafka_2.12-2.0.0/config/server.properties
sed -i "s@zookeeper.connect=localhost:2181@zookeeper.connect=$localip:2181@g" $kafkahome/kafka_2.12-2.0.0/config/server.properties
else
echo "Failed to install kafka"
echo "Missing 'kafka_2.12-2.0.0.gz' file in statistical catalogue"
fi
}
if [ -d "/opt/ops/kafka" ];then
echo "Kafka has been installed"
else
install_kafka
fi
echo "========= End installation of kafka ==========="
创建topic脚本
#!/bin/bash
# create topic
topic='vehiclepass'
cd /opt/ops/kafka/kafka_2.12-2.0.0/bin
zk_ip=$(cat /opt/ops/kafka/kafka_2.12-2.0.0/config/server.properties | grep zookeeper.connect= | awk -F'=' '{ print $2 }' | sed s/[[:space:]]//g)
partitions_num=$(cat /opt/ops/kafka/kafka_2.12-2.0.0/config/server.properties | grep num.partitions= | awk -F'=' '{ print $2 }' | sed s/[[:space:]]//g)
./kafka-topics.sh --create --zookeeper $zk_ip --topic $topic --partitions $partitions_num --replication-factor 1