本文整理汇总了Python中kafka.KafkaConsumer.offsets_for_times方法的典型用法代码示例。如果您正苦于以下问题:Python KafkaConsumer.offsets_for_times方法的具体用法?Python KafkaConsumer.offsets_for_times怎么用?Python KafkaConsumer.offsets_for_times使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类kafka.KafkaConsumer
的用法示例。
在下文中一共展示了KafkaConsumer.offsets_for_times方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __consume_topic_with_func
# 需要导入模块: from kafka import KafkaConsumer [as 别名]
# 或者: from kafka.KafkaConsumer import offsets_for_times [as 别名]
def __consume_topic_with_func(self, topic, func):
consumer = KafkaConsumer(topic,
client_id='fooltrader',
group_id=self.trader_id,
value_deserializer=lambda m: json.loads(m.decode('utf8')),
bootstrap_servers=[KAFKA_HOST])
topic_partition = TopicPartition(topic=topic, partition=0)
start_timestamp = int(self.start_date.timestamp())
partition_map_offset_and_timestamp = consumer.offsets_for_times({topic_partition: start_timestamp})
if partition_map_offset_and_timestamp:
offset_and_timestamp = partition_map_offset_and_timestamp[topic_partition]
if offset_and_timestamp:
# partition assigned after poll, and we could seek
consumer.poll(5, 1)
consumer.seek(topic_partition, offset_and_timestamp.offset)
end_offset = consumer.end_offsets([topic_partition])[topic_partition]
consuming_time = self.current_time
for message in consumer:
message_time = pd.Timestamp(message.value['timestamp'])
# 设定了结束日期的话,时间到了或者kafka没数据了就结束
if self.end_date and (message_time > self.end_date or message.offset + 1 == end_offset):
consumer.close()
break
# 收到的时间戳与消费了的时间戳比较
time_delta = message_time.date() - consuming_time.date()
# 为了准确计算当天收盘账户,必须等各级别都把当天的行情撸完了
if time_delta.days >= 1:
self.barrier.wait()
self.account_service.save_account(self.current_time, trading_close=True)
getattr(self, func)(message.value)
consuming_time = message_time
# 时间以最小级别为准
if self.level_step.get(func) == self.step:
self.current_time = message_time
else:
consumer.poll(5, 1)
consumer.seek(topic_partition, consumer.end_offsets([topic_partition])[topic_partition] - 1)
message = consumer.poll(5000, 1)
kafka_start_date = datetime.fromtimestamp(message[topic_partition][0].timestamp).strftime(
TIME_FORMAT_DAY)
self.logger.warn("start:{} is after the last record:{}".format(self.start_date, kafka_start_date))