當前位置: 首頁>>代碼示例>>Python>>正文


Python KeyedProducer.send方法代碼示例

本文整理匯總了Python中kafka.producer.KeyedProducer.send方法的典型用法代碼示例。如果您正苦於以下問題:Python KeyedProducer.send方法的具體用法?Python KeyedProducer.send怎麽用?Python KeyedProducer.send使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在kafka.producer.KeyedProducer的用法示例。


在下文中一共展示了KeyedProducer.send方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: KafkaLoggingHandler

# 需要導入模塊: from kafka.producer import KeyedProducer [as 別名]
# 或者: from kafka.producer.KeyedProducer import send [as 別名]
class KafkaLoggingHandler(logging.Handler):

    def __init__(self, hosts_list, topic, key=None):
        logging.Handler.__init__(self)
        self.kafka_client = KafkaClient(hosts_list)
        self.key = key
        self.kafka_topic_name = topic
        if not key:
            self.producer = SimpleProducer(self.kafka_client)
        else:
            self.producer = KeyedProducer(self.kafka_client)

    def emit(self, record):
        # drop kafka logging to avoid infinite recursion
        if record.name == 'kafka':
            return
        try:
            # use default formatting
            msg = self.format(record)
            # produce message
            if not self.key:
                self.producer.send_messages(self.kafka_topic_name, msg)
            else:
                self.producer.send(self.kafka_topic_name, self.key, msg)
        except (KeyboardInterrupt, SystemExit):
            raise
        except:
            self.handleError(record)

    def close(self):
        self.producer.stop()
        logging.Handler.close(self)
開發者ID:nivm,項目名稱:python-kafka-logging,代碼行數:34,代碼來源:KafkaHandler.py

示例2: offsetCommit

# 需要導入模塊: from kafka.producer import KeyedProducer [as 別名]
# 或者: from kafka.producer.KeyedProducer import send [as 別名]
def offsetCommit():
    global users
    checkUserPartitionMapping()
    kafkaClient = KafkaClient(kafkaHost, timeout=None)
    producer = KeyedProducer(kafkaClient, async=False,
                      req_acks=UserProducer.ACK_AFTER_LOCAL_WRITE,
                      ack_timeout=200)
    for partition in partitions:
        encodedMessage = simplejson.dumps({'turtleName':turtleName,
                                           'user':'',
                                           'operation':'offsetCommit'})
        print producer.send(kafkaTopic, partition, encodedMessage)
    producer.stop(1)
    kafkaClient.close()
開發者ID:TravelGene-ios,項目名稱:travelgene,代碼行數:16,代碼來源:mouthOpen2.py


注:本文中的kafka.producer.KeyedProducer.send方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。