python kafkaproducer_Python kafka.SimpleProducer方法代码示例

# 需要导入模块: import kafka [as 别名]

# 或者: from kafka import SimpleProducer [as 别名]

def __init__(self, config):

app = Flask(__name__)

app.secret_key = 'i love to search full text in real time'

# attach a redis connection pool

app.pool = redis.ConnectionPool(host="localhost", port=6379)

# user -> channels mapping

app.user_channels = {}

# how to handle messages that enter the stream from redis pub sub

def redis_message_handler(msg):

redis_connection = redis.Redis(connection_pool=app.pool)

# get channel and content of incoming message

channel = msg['channel']

data = msg['data']

# word highlighting -- TODO: this would be better to do in the search engine!

query = redis_connection.get(channel)

words = list(set(query.split(" ")))

for w in words:

data=data.lower().replace(w.lower(), highlight(w.lower()))

# find users subscribed to this channel

if app.user_channels.get(channel) is not None:

for user in app.user_channels.get(channel):

redis_connection.lpush(user, data)

else:

# no more users for this channel, unsubscribe from it

redis_connection.unsubscribe(channel)

# Add Redis query subscriber to app

app.disp = []

app.subscriber = QuerySubscriber("localhost", 6379, redis_message_handler)

# setup kafka producer in the app

kafka = KafkaClient("{0}:{1}".format(config["zookeeper_host"], 9092))

app.producer = SimpleProducer(kafka)

# add the app

self.app = app

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值