步驟三:實驗數據上傳和加工__快速入門_推薦引擎-阿裏雲
操作步驟如下:
- 數據上傳
- 了解推薦引擎基礎數據模型
- 創建推薦引擎基礎表
- 數據格式轉換和加載
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-阿裏雲