V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · Sep 24, 2020 · 2125 views
    This topic created in 2053 days ago, the information mentioned may be changed or developed.

    我想要读 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 replies    2020-09-24 11:12:31 +08:00
    levelworm
        1
    levelworm  
    OP
       Sep 24, 2020
    我又试了下,可以看到 offset,诡异啊。。。照理说能看到 offset 不就应该能够看到消息吗?还是我理解错了。
    levelworm
        2
    levelworm  
    OP
       Sep 24, 2020
    我搞定了我,原来 seek_to_end()之后他给我的是最后一个 offset+1,那我的 Kafka 此时并没有接受新的消息,那肯定是不会显示任何东西了,我只需要把 offset-1 然后再 seek 即可。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2963 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 14:58 · PVG 22:58 · LAX 07:58 · JFK 10:58
    ♥ Do have faith in what you're doing.