您好,登錄后才能下訂單哦!
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。