V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
levelworm
V2EX  ›  Python

求问如何用 Python 读取 Kafka topic 最新一条数据的 timestamp

  •  
  •   levelworm · 2020-09-24 10:46:30 +08:00 · 1636 次点击
    这是一个创建于 1303 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我想要读 Kafka topic 最新一条数据的时间戳,然后用这个时间戳在 vertica 里头做为 partition 的依据。不过现在的问题是,使用seek_to_end()之后,就陷入到死循环了,一条数据也没放出来,我觉得应该能放出最后一条消息的啊?请问我哪里弄错了?

    这是我的代码:

    
    topic: str = 'mytopic'
    broker: str = 'myserver'
    
    consumer = KafkaConsumer(
        bootstrap_servers=[broker],
        enable_auto_commit=True
    )
    
    tp = TopicPartition(topic, 0)
    consumer.assign([tp])
    consumer.poll()
    consumer.seek_to_end()
    
    for message in consumer:
        print(message.timestamp)
        print(message)
    
    consumer.close()
    
    
    2 条回复    2020-09-24 11:12:31 +08:00
    levelworm
        1
    levelworm  
    OP
       2020-09-24 10:55:05 +08:00
    我又试了下,可以看到 offset,诡异啊。。。照理说能看到 offset 不就应该能够看到消息吗?还是我理解错了。
    levelworm
        2
    levelworm  
    OP
       2020-09-24 11:12:31 +08:00
    我搞定了我,原来 seek_to_end()之后他给我的是最后一个 offset+1,那我的 Kafka 此时并没有接受新的消息,那肯定是不会显示任何东西了,我只需要把 offset-1 然后再 seek 即可。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5183 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 08:26 · PVG 16:26 · LAX 01:26 · JFK 04:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.