python kafka producer向主题发送0消息

时间:2019-06-24 07:06:32

标签: python apache-kafka kafka-producer-api

我们有一个卡夫卡生产商来提供mqtt数据。以下是我的python脚本:

import paho.mqtt.client as mqtt
import os
import ssl
import argparse
import threading, logging, time
import multiprocessing
from kafka import KafkaProducer, 
from location_pb2 import Locations

def on_connect(mqttc, obj, flags, rc):
    print("rc: " + str(rc) + " MQTT Connected")

def on_message(mqttc, obj, msg):
    #print(msg.topic + " " + str(msg.qos) + " " + str(msg.payload))
    send_message_to_kafka(msg.payload)

def send_message_to_kafka(message):
    producer = KafkaProducer(bootstrap_servers='xxxx07:9092,xxxx04:9092, xxxx05:9092', api_version='(0,10)',security_protocol='SASL_PLAINTEXT', sasl_plain_username='user', sasl_plain_password='pass', sasl_mechanism='PLAIN' )
    producer.send('my-topic',message)
    print("sending message to kafka: %s" % message)
    producer.flush()
    #producer.close
    metrics = producer.metrics()
    print(metrics)  

当我尝试在发送给生产者之前打印消息时

print(msg.topic + " " + str(msg.qos) + " " + str(msg.payload))

数据如下所示

1.0.0/LOC/SPOT_GOB/xxxx-xxxx/GPB_LOCR 0 b'\n+\n\txxxx-xxxx\x12\x0c6CC7ECA59000\x1d\xb8~\xe0c%\x008\x98C(\x020\xb0\xe5\xac\xe8x05'

但是当我尝试发送给生产者函数时: send_message_to_kafka(msg.payload)

0条消息正在写卡夫卡主题。

0 个答案:

没有答案