# 需要导入模块: 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