Exemplo n.º 1
0
# 轉換msgKey或msgValue成為utf-8的字串
def try_decode_utf8(data):
    if data:
        return data.decode('utf-8')
    else:
        return None


if __name__ == "__main__":
    ip = "35.194.224.128:9092"
    topic = "test1"

    con = Consumer()
    try:
        con.setIP(ip)
        print("IP:", con.ipAddr)
        con.setTopic(topic)
        print("topic:", con.topicName)
        con.getConsumer()
        count = 0
        while True:
            # 請求Kafka把新的訊息吐出來
            records = con.aConsumer.consume(num_messages=500,
                                            timeout=1.0)  # 批次讀取
            if records is None:
                continue

            for record in records:
                # 檢查是否有錯誤
                if record is None: