python将kafka数据写入memcache

import json

import memcache
from kafka import KafkaConsumer


class KafkaToMemcache:

    def __init__(self, topic, kafka_host):
        self.memcache = memcache.Client(['127.0.0.1:11211'])
        self.con = KafkaConsumer(topic, bootstrap_servers=kafka_host)

    def write_cache(self, key, body):
        """

        所属单元:监控信息缓存单元

        此方法根据批次号将原始监控数据写入缓存
        :param body: kafka中接收到的数据
        :param key: 编号
        :return:
        """
        try:
            body = body.decode('utf-8')
            self.memcache.set(key, body)
        except UnicodeError:
            print("数据解码出错")

    def consumer(self):
        """
        此方法消费kafka数据,并将之写入缓存中
        :return:
        """
        key = 1
        for msg in self.con:
            key += 1
            data = msg.value
            # print(data)
            self.write_cache(str(key), data)

    def do(self):
        self.consumer()


if __name__ == '__main__':
    topic = "feng"
    kafka_host = "localhost:9092"
    kafka_to_memcache = KafkaToMemcache(topic, kafka_host)
    kafka_to_memcache.do()

猜你喜欢

转载自blog.csdn.net/mrliqifeng/article/details/80106994