單機Kafka安裝

wget http://mirrors.tuna.tsinghua.edu.cn/apache/kafka/2.2.0/kafka_2.12-2.2.0.tgz 


ps -ef|grep zookeeper|grep -v grep|awk '{print $2}' | xargs kill -9
ps -ef|grep kafka|grep -v grep|awk '{print $2}' | xargs kill -9



 nohup  ./bin/zookeeper-server-start.sh  ./config/zookeeper.properties  > nohup_zookeeper.log  2>&1 &
 nohup ./bin/kafka-server-start.sh  ./config/server.properties >nohup_kafka.log 2>&1 &
 
 

./bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test


./bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test

#低版本
./bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic test --from-beginning
#高版本
./bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning


./bin/kafka-topics.sh  --list --zookeeper localhost:2181




#測試

[root@server test]# more testKafka_p.py 
import json
from kafka import KafkaProducer

producer = KafkaProducer(bootstrap_servers='localhost:9092')

msg_dict = {
    "testk1": 10,
    "testk2": "msg",
    "msg": "Hello World"
}
msg = json.dumps(msg_dict)

for i in range(2):
  producer.send('test_rhj', msg, partition=0)
producer.close()



[root@server test]# more testKafka_c.py 
from kafka import KafkaConsumer

consumer = KafkaConsumer('test_rhj', bootstrap_servers=['localhost:9092'])
for msg in consumer:
    recv = "%s:%d:%d: key=%s value=%s" % (msg.topic, msg.partition, msg.offset, msg.key, msg.value)
    print recv
[root@server test]# more testKafka_c_poll.py 
from kafka import KafkaConsumer
import time

consumer = KafkaConsumer( bootstrap_servers=['localhost:9092'])
consumer.subscribe(topics=('test_rhj',))
index = 0
while True:
    msg = consumer.poll(timeout_ms=5)
    print msg
    time.sleep(2)
    index += 1
    print '--------poll index is %s----------' % index
[root@server test]#

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章