閱讀254 返回首頁    go 阿裏雲 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 步驟四:配置推薦引擎__快速入門_推薦引擎-阿裏雲