kafka 生产者消费者脚本
时间: 2023-08-14 21:11:25 浏览: 230
你可以使用Python编写Kafka生产者和消费者的脚本。下面是一个简单的示例:
Kafka生产者脚本:
```python
from kafka import KafkaProducer
# 创建Kafka生产者
producer = KafkaProducer(bootstrap_servers='localhost:9092')
# 发送消息到指定的主题
topic = 'your_topic'
message = 'your_message'
# 发送消息
producer.send(topic, value=message.encode('utf-8'))
# 关闭生产者
producer.close()
```
Kafka消费者脚本:
```python
from kafka import KafkaConsumer
# 创建Kafka消费者
consumer = KafkaConsumer('your_topic', bootstrap_servers='localhost:9092')
# 持续消费消息
for message in consumer:
print(message.value.decode('utf-8'))
# 关闭消费者
consumer.close()
```
请注意,上述示例中的"localhost:9092"是Kafka服务器的地址和端口。你需要根据你的实际配置进行更改。另外,你还需要安装`kafka-python`库来运行这些脚本。你可以使用以下命令来安装该库:
```
pip install kafka-python
```
希望对你有所帮助!如果还有其他问题,请随时提问。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)