当前位置: 首页>>代码示例>>Python>>正文


Python KeyedProducer.stop方法代码示例

本文整理汇总了Python中kafka.producer.KeyedProducer.stop方法的典型用法代码示例。如果您正苦于以下问题:Python KeyedProducer.stop方法的具体用法?Python KeyedProducer.stop怎么用?Python KeyedProducer.stop使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在kafka.producer.KeyedProducer的用法示例。


在下文中一共展示了KeyedProducer.stop方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: KafkaLoggingHandler

# 需要导入模块: from kafka.producer import KeyedProducer [as 别名]
# 或者: from kafka.producer.KeyedProducer import stop [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: KafkaLoggingHandler

# 需要导入模块: from kafka.producer import KeyedProducer [as 别名]
# 或者: from kafka.producer.KeyedProducer import stop [as 别名]
class KafkaLoggingHandler(logging.Handler):
    def __init__(self, hosts="", topic="", partition=0):
        logging.Handler.__init__(self)
        self.kafkaClient = KafkaClient(hosts)
        self.topic = topic
        self.partition = partition
        self.producer = KeyedProducer(
            self.kafkaClient, async=False, req_acks=KeyedProducer.ACK_AFTER_LOCAL_WRITE, ack_timeout=200
        )

    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
            self.producer.send_messages(self.topic + record.name, self.partition, msg)
        except:
            import traceback

            ei = sys.exc_info()
            traceback.print_exception(ei[0], ei[1], ei[2], None, sys.stderr)
            del ei

    def close(self):
        self.producer.stop()
        logging.Handler.close(self)
开发者ID:TravelGene-ios,项目名称:travelgene,代码行数:31,代码来源:kafka_log_handler.py

示例3: offsetCommit

# 需要导入模块: from kafka.producer import KeyedProducer [as 别名]
# 或者: from kafka.producer.KeyedProducer import stop [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

示例4: Producer

# 需要导入模块: from kafka.producer import KeyedProducer [as 别名]
# 或者: from kafka.producer.KeyedProducer import stop [as 别名]
class Producer(object):

    def __init__(self, addr):
        self.client = KafkaClient(addr)
        self.producer = KeyedProducer(self.client)

    def open_save(self, fileName):
        log_file = open(fileName, "w")
        log_file.close()
        return log_file

    def create_topic(self, topic):
        script = "/usr/local/kafka/bin/kafka-topics.sh"
        os.system("{} --create --zookeeper localhost:2181 --topic {} --partitions {} --replication-factor 2".format(script, topic, "4"))
        return "topic {} created".format(topic)

    def produce_msgs(self, source_symbol, topic):
        server_topics = self.client.topic_partitions
        if topic not in server_topics:
          self.create_topic(topic)
        price_field = random.randint(800,1400)
        cities = ["Barcelona", "Philadelphia", "Honolulu",
                  "Atlanta", "Miami", "Chicago", "SF", "LA", "NYC",
                  "Houston", "Paris", "London", "Tokyo"]
        msg_cnt = 0
        log_file = open("input1/{}.csv".format(topic), "a")
        while True:
            time_field = datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')
            location_field = random.choice(cities)
            price_field += random.randint(-10, 10)/10.0
            str_fmt = "{},{},{},{}"
            message_info = str_fmt.format(source_symbol,
                                          time_field,
                                          location_field,
                                          price_field)
            print message_info
            log_file.write("{}\n".format(message_info))
            self.producer.send_messages(topic, source_symbol, message_info)
            msg_cnt += 1
            if msg_cnt > 200000:
                log_file.close()
                self.producer.stop()
                break
开发者ID:katychuang,项目名称:insight-demo,代码行数:45,代码来源:kafka_producer1.py

示例5: KafkaLoggingHandler

# 需要导入模块: from kafka.producer import KeyedProducer [as 别名]
# 或者: from kafka.producer.KeyedProducer import stop [as 别名]
class KafkaLoggingHandler(logging.Handler):

    def __init__(self, hosts_list, topic, **kwargs):
        logging.Handler.__init__(self)

        self.kafka_client = SimpleClient(hosts_list)
        self.key = kwargs.get("key", None)
        self.kafka_topic_name = topic

        if not self.key:
            self.producer = SimpleProducer(self.kafka_client, **kwargs)
        else:
            self.producer = KeyedProducer(self.kafka_client, **kwargs)

    def emit(self, record):
        # drop kafka logging to avoid infinite recursion
        if record.name == 'kafka':
            return
        try:
            # use default formatting
            msg = self.format(record)
            if isinstance(msg, unicode):
                msg = msg.encode("utf-8")

            # produce message
            if not self.key:
                self.producer.send_messages(self.kafka_topic_name, msg)
            else:
                self.producer.send_messages(self.kafka_topic_name, self.key,
                                            msg)
        except (KeyboardInterrupt, SystemExit):
            raise
        except Exception:
            self.handleError(record)

    def close(self):
        if self.producer is not None:
            self.producer.stop()
        logging.Handler.close(self)
开发者ID:avihoo,项目名称:python-kafka-logging,代码行数:41,代码来源:KafkaHandler.py


注:本文中的kafka.producer.KeyedProducer.stop方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。