Skip to content

Instantly share code, notes, and snippets.

@mikeda
Created June 30, 2013 09:06
Show Gist options
  • Select an option

  • Save mikeda/5894451 to your computer and use it in GitHub Desktop.

Select an option

Save mikeda/5894451 to your computer and use it in GitHub Desktop.
FluentdとRedisで簡単なアクセスランキングとアイテム相関レコメンド
module Fluent
class RecommendRedis < Output
Fluent::Plugin.register_output('recommend_and_ranking', self)
require 'redis'
REDIS = Redis.new(:host => "192.168.1.126", :port => 6379)
VIEW_PREFIX = 'view_' # ユーザごとの閲覧履歴のリスト
RELATION_PREFIX = 'relation_' # アイテム間の相関値
ACCESS_COUNTER = 'access_counter' # アイテムごとのアクセスカウンタ
def configure(conf)
super
end
def start
super
end
def shutdown
super
end
def emit(tag, es, chain)
es.each do |time, record|
uid = record['uid']
item_code = record['item_code']
view_key = VIEW_PREFIX + record['uid']
# 閲覧履歴リストとアイテム相関値の更新
if REDIS.lrange(view_key, 0, 0) != [item_code] then
REDIS.lpush(view_key, item_code)
view_history = REDIS.lrange(view_key, 0, 3).find_all{|r| r != item_code}.uniq.reverse.take(2).reverse
view_history.each do |old_item_code|
score = REDIS.zincrby(RELATION_PREFIX + old_item_code, 1, item_code)
score = REDIS.zincrby(RELATION_PREFIX + item_code, 1, old_item_code)
end
end
# ラインキング用アクセスカウンタのインクリメント
REDIS.zincrby(ACCESS_COUNTER, 1, item_code)
end
chain.next
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment