1.软件需求
CentOS 7.2 X64
jdk-8u121-linux-x64.rpm
zookeeper-3.4.9.tar.gz
kafka_2.11-0.10.1.0.tgz
setuptools-33.1.1.zip
kafka-python-1.3.2.tar.gz
2.安装zookeeper、kafka
1).zookeeper
# tar -zxf zookeeper-3.4.9.tar.gz
# mv zookeeper-3.4.9 /usr/local/zookeeper
# mkdir -p /var/lib/zookeeper
# cat > /usr/local/zookeeper/conf/zoo.cfg << EOF
tickTime=2000
dataDir=/var/lib/zookeeper
clientPort=2181
EOF
# /usr/local/zookeeper/bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
#
2).kafka
# tar -zxf kafka_2.11-0.10.1.0.tgz
# mv kafka_2.11-0.10.1.0 /usr/local/kafka
# mkdir /tmp/kafka-logs
# /usr/local/kafka/bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic telematics
fCreated topic "telematics".
#
3.create topics
# /usr/local/kafka/bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic telematics
4.topic操作
1).list topic
# /usr/local/kafka/bin/kafka-topics.sh --zookeeper localhost:2181 --list
telematics
2).describe topic
# /usr/local/kafka/bin/kafka-topics.sh --zookeeper localhost:2181 --describe --topic telematics
Topic:telematics PartitionCount:1 ReplicationFactor:1 Configs:
Topic: telematics Partition: 0 Leader: 0 Replicas: 0 Isr: 0
#
3).delete topic
#/usr/local/kafka/bin/kafka-topics.sh --zookeeper localhost:2181 --delete --topic telematics
5.添加选项执行删除命令时生效,否则只是标记为删除
/usr/local/kafka/config/server.properties
delete.topic.enable=true
6.测试脚本
1). producer.py
#!/usr/bin/python
from kafka import KafkaProducer
producer = KafkaProducer(bootstrap_servers='192.168.1.201:9092')
producer = KafkaProducer(key_serializer=str.encode, value_serializer=str.encode)
producer.send('telematics', key='ping', value=b'1234')
#
2). consumer.py
from kafka import KafkaConsumer
# To consume latest messages and auto-commit offsets
consumer = KafkaConsumer('telematics',bootstrap_servers=['192.168.1.201:9092'])
for message in consumer:
# message value and key are raw bytes -- decode if necessary!
# e.g., for unicode: `message.value.decode('utf-8')`
print ("%s:%d:%d: key=%s value=%s" % (message.topic, message.partition,
message.offset, message.key,
message.value))
#
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。