254
微信
步骤三:实验数据上传和加工__快速入门_推荐引擎-阿里云
操作步骤如下:
- 数据上传
- 了解推荐引擎基础数据模型
- 创建推荐引擎基础表
- 数据格式转换和加载
1. 数据上传
(1) 在odps的项目中,按照2.2中的表结构说明,分别创建表;
-- 创建用户表(users)
create table users(user_id bigint,
age bigint,
gender string,
occupation string,
zip_cod string)
;
-- 创建电影表
create table movies(movie_id bigint, movie_title string,
release_date string, video_release_date string,
IMDb_URL string, Unknown bigint,
Action bigint, Adventure bigint,
Animation bigint, Childrens bigint,
Comedy bigint, Crime bigint,
Documentary bigint, Drama bigint,
Fantasy bigint, Film_Noir bigint,
Horror bigint, Musical bigint,
Mystery bigint, Romance bigint,
Sci_Fi bigint, Thriller bigint,
War bigint, Western bigint
)
;
-- 创建用户评分表(ratings)
create table ratings(user_id bigint,
movie_id bigint,
rating bigint,
timestamps bigint
)
;
(2) 使用odps客户端(odpscmd)中的tunnel命令上传数据:
注意文件路径要输入正确,可以使用绝对路径:
tunnel upload ./ml-100k/u.user users -fd "|" -rd "n";
tunnel upload ./ml-100k/u.item movies -fd "|" -rd "n";
tunnel upload ./ml-100k/u.data ratings -fd "t" -rd "n";
(3) 查看导入后的结果:
read users 10;
read movies 10;
read ratings 20;
2. 了解推荐引擎基础数据模型
本章节内容为基础知识介绍,是附加阅读内容,可以帮助理解推荐引擎所需数据的内容和格式,便于做数据转换。已经了解的或者不想了解太多技术细节的同学可以跳过,不影响后续试验操作。
推荐引擎自带了一套数据模型,主要包括几类数据:用户数据、物品数据以及行为数据,具体格式规范请参考数据格式规范
推荐引擎会基于基础数据进行推荐分析,得到最终推荐结果。我们需要做的是:
1):在我们ODPS的项目空间中创建这些表;
2):按照推荐引擎数据模型的要求,使用ODPS SQL等将我们已有的业务数据插入到推荐引擎所需要的表中去。
3. 创建推荐引擎基础表
使用ODPS客户端工具odpscmd,登录执行以下建表脚本:
-- 创建用户信息表(movielens_user_meta)
create table movielens_user_meta (
user_id string,
tags string
) partitioned by (ds string)
;
--创建用户属性维度表(movielens_user_meta_config)
create table movielens_user_meta_config (
config_name string,
config_value string
) partitioned by (ds string)
;
-- 创建物品信息表(movielens_item_meta)
create table movielens_item_meta (
item_id string,
category string,
keywords string,
description string,
properties string,
bizinfo string
) partitioned by (ds string)
;
--创建物品属性维度表(movielens_item_meta_config)
create table movielens_item_meta_config (
config_name string,
config_value string
) partitioned by (ds string);
--创建用户行为表(movielens_user_behavior)
create table movielens_user_behavior (
user_id string,
item_id string,
bhv_type string,
bhv_amt double,
bhv_cnt double,
bhv_datetime datetime,
content string,
media_type string,
pos_type string,
position string,
env string,
trace_id string
) partitioned by (ds string)
;
--创建可推荐物品表(movielens_rec_item_info)
create table movielens_rec_item_info (
item_id string,
item_info string
) partitioned by (ds string)
;
4. 数据格式转换和加载
对已上传的业务数据进行加工处理,插入到推荐引擎的基础数据表中去,即把数据从 uses、movies以及 user_behavior中通过ODPS SQL的方式,转换成满足推荐引擎基础数据要求的格式,插入到对应的六张表中。
首先,将users信息插入到 movielens_user_meta 和 movielens_user_meta_config 中去:
--创建 DUAL 表,用于单条记录插入
create table dual (x string);
insert overwrite table dual select 'x' from users limit 1;
--插入用户信息表:
insert overwrite table movielens_user_meta partition (ds='20160401')
select user_id,concat('age 03',age,' 02gender 03',gender)
from users
;
--插入用户属性维度表
insert overwrite table movielens_user_meta_config partition (ds='20160401')
select 'age','sv_num'
from dual
;
insert into table movielens_user_meta_config partition (ds='20160401')
select 'gender','sv_enum'
from dual
操作执行完成后,执行下面的命令可查看数据表内容:
read movielens_user_meta partition (ds='20160401') 10
接着,将用户行为数据插入到用户行为表 movielens_user_behavior 中:
insert overwrite table movielens_user_behavior partition (ds='20160401')
select user_id, movie_id, 'grade',rating, 1.0,
FROM_UNIXTIME(timestamps),null, null, null, null, null, null
from ratings
;
然后,把 movies 信息插入到 movielens_item_meta 和 movielens_item_meta_config 中去:
insert overwrite table movielens_item_meta partition (ds='20160401')
select movie_id, category,
case when substr(keyword,-1)=' 02' then substr(keyword,1,length(keyword)-1)
else keyword end,
description, properties, bizinfo
from (select movie_id, null as category,
concat(coalesce(concat(case when unknown=1 then 'unknown' end,' 02'),''),
coalesce(concat(case when action=1 then 'action' end,' 02'),''),
coalesce(concat(case when adventure=1 then 'adventure' end,' 02'),''),
coalesce(concat(case when animation=1 then 'animation' end,' 02'),''),
coalesce(concat(case when childrens=1 then 'childrens' end,' 02'),''),
coalesce(concat(case when comedy=1 then 'comedy' end,' 02'),''),
coalesce(concat(case when crime=1 then 'crime' end,' 02'),''),
coalesce(concat(case when documentary=1 then 'documentary'
end,' 02'),''),
coalesce(concat(case when drama=1 then 'drama' end,' 02'),''),
coalesce(concat(case when fantasy=1 then 'fantasy' end,' 02'),''),
coalesce(concat(case when film_noir=1 then 'film_noir' end,' 02'),''),
coalesce(concat(case when horror=1 then 'horror' end,' 02'),''),
coalesce(concat(case when musical=1 then 'musical' end,' 02'),''),
coalesce(concat(case when mystery=1 then 'mystery' end,' 02'),''),
coalesce(concat(case when romance=1 then 'romance' end,' 02'),''),
coalesce(concat(case when sci_fi=1 then 'sci_fi' end,' 02'),''),
coalesce(concat(case when thriller=1 then 'thriller' end,' 02'),''),
coalesce(concat(case when war=1 then 'war' end,' 02'),''),
coalesce(case when western=1 then 'western' end,'')) as keyword,
movie_title as description , null as properties,null as bizinfo
from movies) t
;
由于properties均为空,所以 movielens_item_meta_config 没有记录要插入,我们可以不进行操作,如果需要建立对应的分区,语句如下:
alter table movielens_item_meta_config
add if not exists partition (ds='20160401');
如果表movielens_item_meta中所有的电影都可以作为推荐结果返回给用户,并且在返回给用户的时候,没有需要显示给用户的附加信息,则不需要创建该表,并且在推荐引擎控制台页面中“可推荐物品表”无需输入表名。在我们的例子中不需要创建该表。
如果其中某些电影不希望作为推荐结果返回给用户,或者想增加一些附加信息,则可以把可以推荐的电影插入到该表中,并且可以在字段config_value中指定返回给客户时的附加信息。我们当前的场景是所有电影均可以推荐,并且将该电影的名字和它在IMDB网站上的链接作为附加信息回显给客户,用户可以根据这个链接去了解电影的详情以及评价:
insert overwrite table movielens_rec_item_info partition(ds='20160401')
select movie_id,concat('Title:',movie_title,'|| IMDB Url:', imdb_url)
from movies
;
至此,所有的数据转换和加载的工作就已经完成了,这部分工作要求使用者有一定的ODPS SQL的经验并且了解自己的业务数据,可以把业务数据按照推荐引擎基础数据表的要求进行简单处理。
最后更新:2016-11-23 16:04:12
上一篇:
步骤二:准备实验数据__快速入门_推荐引擎-阿里云
下一篇:
步骤四:配置推荐引擎__快速入门_推荐引擎-阿里云
更新类目__媒体类目接口_API使用手册_视频点播-阿里云
手动扩展系统盘__扩容磁盘概览_磁盘_用户指南_云服务器 ECS-阿里云
RAM中可对专有网络资源进行授权的Action__借助RAM实现子账号对主账号的VPC资源访问_API参考_专有网络 VPC-阿里云
控制台更新记录__更新记录_产品简介_E-MapReduce-阿里云
集群列表页__集群_用户指南_E-MapReduce-阿里云
ECS Windows系统抓包工具Wireshark的安装使用__网络连接问题_网络问题_Windows操作运维问题_云服务器 ECS-阿里云
RAM 中可对 ECS 资源进行授权的 Action__借助 RAM 实现子账号对主账号的 ECS 资源访问_API 参考_云服务器 ECS-阿里云
FreeBSD系统盘扩容、分区、挂载__扩容磁盘概览_磁盘_用户指南_云服务器 ECS-阿里云
设备接入协议__IoT Hub_产品组件_阿里云物联网套件-阿里云
查询水印模板__水印模板接口_API使用手册_媒体转码-阿里云
相关内容
常见错误说明__附录_大数据计算服务-阿里云
发送短信接口__API使用手册_短信服务-阿里云
接口文档__Android_安全组件教程_移动安全-阿里云
运营商错误码(联通)__常见问题_短信服务-阿里云
设置短信模板__使用手册_短信服务-阿里云
OSS 权限问题及排查__常见错误及排除_最佳实践_对象存储 OSS-阿里云
消息通知__操作指南_批量计算-阿里云
设备端快速接入(MQTT)__快速开始_阿里云物联网套件-阿里云
查询API调用流量数据__API管理相关接口_API_API 网关-阿里云
使用STS访问__JavaScript-SDK_SDK 参考_对象存储 OSS-阿里云