新发布商品实时相关推荐
实时处理新商品元数据,基于内容相似度推荐,解决商品冷启动问题
应用场景:
1新发布商品被用户点击后在列表页推荐相似商品
2.新发布商品详情页相关推荐栏
核心组件:
flume+kafka+spark+hbase+sparkstreaming+redis
1.0
启动zookeeper:master,slave1,slave2
启动kafka集群
./bin/kafka-server-start.sh config/server.properties
创建kafka topic:topic_user_watch
./bin/kafka-topics.sh --create --zookeeper localhost:2181 -replication-factor 3 --partitions 5 -topic topic_item_meta
1.1
Flume配置:item元数据分别发送至HDFS和kafka
a1.sources = r1
a1.sinks = k1 k2
a1.channels = c1 c2
# Describe/configure the source
## exec表示flume回去调用给的命令,然后从给的命令的结果中去拿数据
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /usr/local/lc_codes/jizhi/log/item_meta.log
a1.sources.r1.channels = c1 c2
# Describe the sink
## 表示下沉到hdfs,类型决定了下面的参数
a1.sinks.k1.type = hdfs
a1.sinks.k1.channel = c1
## 下面的配置告诉用hdfs去写文件的时候写到什么位置,下面的表示不是写死的,而是可以动态的变化的。表示输出的目录名称是可变的
a1.sinks.k1.hdfs.path = /flume/item_meta/%y-%m-%d/%H%M/
## 使用本地时间戳
a1.sinks.k1.hdfs.useLocalTimeStamp = true
#生成的文件类型,默认是Sequencefile,可用DataStream:为普通文本
a1.sinks.k1.hdfs.fileType = DataStream
# Describe the sink
a1.sinks.k2.type = org.apache.flume.sink.kafka.KafkaSink
a1.sinks.k2.brokerList = master:9092
a1.sinks.k2.topic = topic_item_meta
# Use a channel which buffers events in memory
##使用内存的方式
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
a1.channels.c2.type = memory
a1.channels.c2.capacity = 1000
a1.channels.c2.transactionCapacity = 100
# Bind the source and sink to the channel
a1.sources.r1.channels = c1 c2
a1.sinks.k1.channel = c1
a1.sinks.k2.channel = c2
启动命令:
./bin/flume-ng agent --conf conf --conf-file /usr/local/lc_codes/jizhi/flume_kafka_hdfs.properties --name a1 -Dflume.root.logger=INFO,console
1.2
Spark对 Hdfs目录下商品元数据处理,并存入Hbase
(1) 提取关键词,计算itemA->token1:score1,token2:score2;
(2) 建立倒排表(token1->itemB:score3,itemC:score4;token2->itemE:score5)
(3) 存入Hbase
存入HBase代码:
invertSortHbase.py
from pyspark import SparkContext, SparkConf
import jieba
import jieba.posseg
import jieba.analyse
# 名称、描述、标签预设分数权重
RATIO_FOR_NAME = 0.9
RATIO_FOR_DESC = 0.1
RATIO_FOR_TAGS = 0.05
def split_