当前位置: 首页 > 知识库问答 >
问题:

Python Kafka Producer and Consumer in Cloudera Cluster

卓嘉良
2023-03-14

我有一个Cloudera集群,在3台不同的机器上有3个经纪人。我正在从集群内的第四个开发。

我创建了我的主题如下:创建主题 /usr/bin/kafka-topics --zookeeper host:2181,host2:2181,hosts3:2181/kafka --create --partitions 10 --replication-factor 2 --topic topicname

我在动物园管理员中的根目录不是根,它是 /kafka

这是我的制作人代码:

class Kafkaproducer(object):
    def __init__(self, **kwargs):
        if kwargs:
            try:
                self.producer = KafkaProducer(**kwargs)
            except Exception as ex:
                print "unable to create Producer Object " + str(ex)
            self.iw = Imageworker()
            log = Logger()
            self.logs = log.logger('Producer')


    def set_topic(self, topic):
        """
        Set Topic for Producer
        :param self:
        :param topic: Topic String for Kafka
        :return: no value
        """
        self.topic = topic
        print self.producer.partitions_for(topic )


    def send_message(self, file):
        """
        send a single message to kafka broker
        :param self:
        :param file: absolute filepath from file to send to broker
        :return: no value
        """
        print self.topic
        try:
            print "create json message .. "
            message = self.iw.read_image_file(file)
        except Exception as ex:
            print "unable to read file" + str(ex)
        try:
            print "send message"+ self.iw.get_imagename(file)
            self.producer.send(self.topic, message)
        except Exception as Ex:
            print "unable to send kafka message " + str(ex)

    def _handle_fetch_response(self):
        print "error"

    def send_message_synchron(self, file ):
        """

        :param data:
        :return:
        """
        try:
            print "create json message .. "
            message = self.iw.read_image_file(file)

        except Exception as ex:
            print "unable to read file" + str(ex)
        try:
            #print "send message "+ self.iw.get_imagename(file)
            future = self.producer.send(self.topic, message)
            future.error_on_callbacks=True
            #result = future.get(timeout=1000)
            result = future.succeeded()

            print future.is_done
            if result:
                print future.value
                print result
                print "success!!!"
                meta = future.get(timeout=100)
        except Exception as ex:
            print "unable to send kafka message " + str(ex)
        try:
            if future.is_done:
                print "Message send successful "
        except KafkaError:
            log.exception()
            print "Error in Kafka"
            pass


    def flush_producer(self):
        self.producer.flush()

我可以使用send_messages函数异步发送消息。此外,我还从使用的主题中获取分区的数量。问题是,信息消失了。

我用我的python消费者和下面的语句检查了两次:

/opt/cloudera/包裹/KAFKA-2.0-1.2.2.0.p0.68/lib/KAFKA/bin/KAFKA-run-class。sh kafka工具。GetOffsetShell--代理列表myhosts--主题topic_name

此外,我想使用我的同步函数发送消息以获取未来的结果。在这里,我无法获得未来的结果。行结果=future.get(超时=1000)失败。

希望有人在这种情况下有主意。非常感谢,

约恩

共有1个答案

纪畅
2023-03-14

发现了问题,但不知道如何修复它。我从属性文件中读取了生产者配置

bootstrap_servers=['h1:9092' ,'h2:9092','h3:9092']
api_version=(0,10)
value_serializer=str.encode
buffer_memory=200000000
retries=5
max_block_ms=10000

producer = Kafkaproducer(**dic)  # do not work
roducer = Kafkaproducer(bootstrap_servers=['h1:9092' ,'h2:9092','h3:9092'],api_version=(0,10)...   # works well

在消费者网站上,我能够与消费者 = Kafkaconsumer(**dic) 一起工作

在修复了制作人Call之后,同步错误大战也消失了。但为什么我不能用字典给制片人打电话?

-

谢谢你

 类似资料:

相关问答

相关文章

相关阅读