阅读254 返回首页    go 微信


步骤三:实验数据上传和加工__快速入门_推荐引擎-阿里云

操作步骤如下:

  1. 数据上传
  2. 了解推荐引擎基础数据模型
  3. 创建推荐引擎基础表
  4. 数据格式转换和加载

1. 数据上传

(1) 在odps的项目中,按照2.2中的表结构说明,分别创建表;

  1. -- 创建用户表(users)
  2. create table users(user_id bigint,
  3. age bigint,
  4. gender string,
  5. occupation string,
  6. zip_cod string)
  7. ;
  8. -- 创建电影表
  9. create table movies(movie_id bigint, movie_title string,
  10. release_date string, video_release_date string,
  11. IMDb_URL string, Unknown bigint,
  12. Action bigint, Adventure bigint,
  13. Animation bigint, Childrens bigint,
  14. Comedy bigint, Crime bigint,
  15. Documentary bigint, Drama bigint,
  16. Fantasy bigint, Film_Noir bigint,
  17. Horror bigint, Musical bigint,
  18. Mystery bigint, Romance bigint,
  19. Sci_Fi bigint, Thriller bigint,
  20. War bigint, Western bigint
  21. )
  22. ;
  23. -- 创建用户评分表(ratings)
  24. create table ratings(user_id bigint,
  25. movie_id bigint,
  26. rating bigint,
  27. timestamps bigint
  28. )
  29. ;

(2) 使用odps客户端(odpscmd)中的tunnel命令上传数据:

注意文件路径要输入正确,可以使用绝对路径:

  1. tunnel upload ./ml-100k/u.user users -fd "|" -rd "n";
  2. tunnel upload ./ml-100k/u.item movies -fd "|" -rd "n";
  3. tunnel upload ./ml-100k/u.data ratings -fd "t" -rd "n";

(3) 查看导入后的结果:

  1. read users 10;
  2. read movies 10;
  3. read ratings 20;

2. 了解推荐引擎基础数据模型

本章节内容为基础知识介绍,是附加阅读内容,可以帮助理解推荐引擎所需数据的内容和格式,便于做数据转换。已经了解的或者不想了解太多技术细节的同学可以跳过,不影响后续试验操作。

推荐引擎自带了一套数据模型,主要包括几类数据:用户数据、物品数据以及行为数据,具体格式规范请参考数据格式规范

推荐引擎会基于基础数据进行推荐分析,得到最终推荐结果。我们需要做的是:

1):在我们ODPS的项目空间中创建这些表;

2):按照推荐引擎数据模型的要求,使用ODPS SQL等将我们已有的业务数据插入到推荐引擎所需要的表中去。

3. 创建推荐引擎基础表

使用ODPS客户端工具odpscmd,登录执行以下建表脚本:

  1. -- 创建用户信息表(movielens_user_meta)
  2. create table movielens_user_meta (
  3. user_id string,
  4. tags string
  5. ) partitioned by (ds string)
  6. ;
  7. --创建用户属性维度表(movielens_user_meta_config)
  8. create table movielens_user_meta_config (
  9. config_name string,
  10. config_value string
  11. ) partitioned by (ds string)
  12. ;
  13. -- 创建物品信息表(movielens_item_meta)
  14. create table movielens_item_meta (
  15. item_id string,
  16. category string,
  17. keywords string,
  18. description string,
  19. properties string,
  20. bizinfo string
  21. ) partitioned by (ds string)
  22. ;
  23. --创建物品属性维度表(movielens_item_meta_config)
  24. create table movielens_item_meta_config (
  25. config_name string,
  26. config_value string
  27. ) partitioned by (ds string);
  28. --创建用户行为表(movielens_user_behavior)
  29. create table movielens_user_behavior (
  30. user_id string,
  31. item_id string,
  32. bhv_type string,
  33. bhv_amt double,
  34. bhv_cnt double,
  35. bhv_datetime datetime,
  36. content string,
  37. media_type string,
  38. pos_type string,
  39. position string,
  40. env string,
  41. trace_id string
  42. ) partitioned by (ds string)
  43. ;
  44. --创建可推荐物品表(movielens_rec_item_info)
  45. create table movielens_rec_item_info (
  46. item_id string,
  47. item_info string
  48. ) partitioned by (ds string)
  49. ;

4. 数据格式转换和加载

对已上传的业务数据进行加工处理,插入到推荐引擎的基础数据表中去,即把数据从 uses、movies以及 user_behavior中通过ODPS SQL的方式,转换成满足推荐引擎基础数据要求的格式,插入到对应的六张表中。

首先,将users信息插入到 movielens_user_meta 和 movielens_user_meta_config 中去:

  1. --创建 DUAL 表,用于单条记录插入
  2. create table dual (x string);
  3. insert overwrite table dual select 'x' from users limit 1;
  4. --插入用户信息表:
  5. insert overwrite table movielens_user_meta partition (ds='20160401')
  6. select user_id,concat('age03',age,'02gender03',gender)
  7. from users
  8. ;
  9. --插入用户属性维度表
  10. insert overwrite table movielens_user_meta_config partition (ds='20160401')
  11. select 'age','sv_num'
  12. from dual
  13. ;
  14. insert into table movielens_user_meta_config partition (ds='20160401')
  15. select 'gender','sv_enum'
  16. from dual

操作执行完成后,执行下面的命令可查看数据表内容:

  1. read movielens_user_meta partition (ds='20160401') 10

接着,将用户行为数据插入到用户行为表 movielens_user_behavior 中:

  1. insert overwrite table movielens_user_behavior partition (ds='20160401')
  2. select user_id, movie_id, 'grade',rating, 1.0,
  3. FROM_UNIXTIME(timestamps),null, null, null, null, null, null
  4. from ratings
  5. ;

然后,把 movies 信息插入到 movielens_item_meta 和 movielens_item_meta_config 中去:

  1. insert overwrite table movielens_item_meta partition (ds='20160401')
  2. select movie_id, category,
  3. case when substr(keyword,-1)='02' then substr(keyword,1,length(keyword)-1)
  4. else keyword end,
  5. description, properties, bizinfo
  6. from (select movie_id, null as category,
  7. concat(coalesce(concat(case when unknown=1 then 'unknown' end,'02'),''),
  8. coalesce(concat(case when action=1 then 'action' end,'02'),''),
  9. coalesce(concat(case when adventure=1 then 'adventure' end,'02'),''),
  10. coalesce(concat(case when animation=1 then 'animation' end,'02'),''),
  11. coalesce(concat(case when childrens=1 then 'childrens' end,'02'),''),
  12. coalesce(concat(case when comedy=1 then 'comedy' end,'02'),''),
  13. coalesce(concat(case when crime=1 then 'crime' end,'02'),''),
  14. coalesce(concat(case when documentary=1 then 'documentary'
  15. end,'02'),''),
  16. coalesce(concat(case when drama=1 then 'drama' end,'02'),''),
  17. coalesce(concat(case when fantasy=1 then 'fantasy' end,'02'),''),
  18. coalesce(concat(case when film_noir=1 then 'film_noir' end,'02'),''),
  19. coalesce(concat(case when horror=1 then 'horror' end,'02'),''),
  20. coalesce(concat(case when musical=1 then 'musical' end,'02'),''),
  21. coalesce(concat(case when mystery=1 then 'mystery' end,'02'),''),
  22. coalesce(concat(case when romance=1 then 'romance' end,'02'),''),
  23. coalesce(concat(case when sci_fi=1 then 'sci_fi' end,'02'),''),
  24. coalesce(concat(case when thriller=1 then 'thriller' end,'02'),''),
  25. coalesce(concat(case when war=1 then 'war' end,'02'),''),
  26. coalesce(case when western=1 then 'western' end,'')) as keyword,
  27. movie_title as description , null as properties,null as bizinfo
  28. from movies) t
  29. ;

由于properties均为空,所以 movielens_item_meta_config 没有记录要插入,我们可以不进行操作,如果需要建立对应的分区,语句如下:

  1. alter table movielens_item_meta_config
  2. add if not exists partition (ds='20160401');

如果表movielens_item_meta中所有的电影都可以作为推荐结果返回给用户,并且在返回给用户的时候,没有需要显示给用户的附加信息,则不需要创建该表,并且在推荐引擎控制台页面中“可推荐物品表”无需输入表名。在我们的例子中不需要创建该表。

如果其中某些电影不希望作为推荐结果返回给用户,或者想增加一些附加信息,则可以把可以推荐的电影插入到该表中,并且可以在字段config_value中指定返回给客户时的附加信息。我们当前的场景是所有电影均可以推荐,并且将该电影的名字和它在IMDB网站上的链接作为附加信息回显给客户,用户可以根据这个链接去了解电影的详情以及评价:

  1. insert overwrite table movielens_rec_item_info partition(ds='20160401')
  2. select movie_id,concat('Title:',movie_title,'|| IMDB Url:', imdb_url)
  3. from movies
  4. ;

至此,所有的数据转换和加载的工作就已经完成了,这部分工作要求使用者有一定的ODPS SQL的经验并且了解自己的业务数据,可以把业务数据按照推荐引擎基础数据表的要求进行简单处理。

最后更新:2016-11-23 16:04:12

  上一篇:go 步骤二:准备实验数据__快速入门_推荐引擎-阿里云
  下一篇:go 步骤四:配置推荐引擎__快速入门_推荐引擎-阿里云