用SparkSQL構建用戶畫像

SparkSQL構建用戶畫像

2、  前言

大數據時代已經到來,企業迫切但願從已經積累的數據中分析出有價值的東西,而用戶行爲的分析尤其重要。java

利用大數據來分析用戶的行爲與消費習慣,能夠預測商品的發展的趨勢,提升產品質量,同時提升用戶滿意度。node

3、  初識用戶畫像

 

 

 

 

右邊是一我的的基本屬性,經過一我的的基本屬性咱們能夠了解到這我的的基本信息,左邊上圖是經過消費購物信息來描述一我的特徵,左邊下圖是經過交際圈信息來描述一我的特徵,經過不一樣的維度,去描述一我的,認識一我的,瞭解一我的。這就是咱們今天所要講到的用戶畫像。mysql

用戶畫像:也叫用戶信息標籤化、客戶標籤;根據用戶社會屬性、生活習慣和消費行爲等信息而抽象出的一個標籤化的用戶模型。從電商的角度看,根據你在電商網站上所填的信息和你的行爲,能夠用一些標籤把你描繪出來,描述你的標籤就是用戶畫像。構建用戶畫像的核心工做便是給用戶貼「標籤」,而標籤是經過對用戶信息分析而來的高度精煉的特徵標識。android

4、  構建電商用戶畫像的重大意義

羅振宇在《時間的朋友》跨年演講舉了這樣一個例子:當一個壞商家掌握了你的購買數據,他就能夠根據你平時購買商品的偏好來決定是給你發正品仍是假貨以此來提升利潤,且不說是否存在這種狀況,但這也說明了利用用戶畫像能夠作到「精準營銷」,固然這是極其錯誤的用法。ios

其做用大致不離如下幾個方面:web

u  1、精準營銷,分析產品潛在用戶,針對特定羣體利用短信郵件等方式進行營銷; 算法

u  2、用戶統計,好比中國大學購買書籍人數 TOP10spring

u  3、數據挖掘,構建智能推薦系統,利用關聯規則計算,喜歡紅酒的人一般喜歡什麼運動品牌,利用聚類算法分析,喜歡紅酒的人年齡段分佈狀況; sql

u  4、進行效果評估,完善產品運營,提高服務質量,其實這也就至關於市場調研、用戶調研,迅速下定位服務羣體,提供高水平的服務; 數據庫

u  5、對服務或產品進行私人定製,即個性化的服務某類羣體甚至每一位用戶(我的認爲這是目前的發展趨勢,將來的消費主流)。好比,某公司想推出一款面向510歲兒童的玩具,經過用戶畫像進行分析,發現形象=「喜羊羊」、價格區間=「中等」的偏比如重最大,那麼就給新產品提供了很是客觀有效的決策依據。

u  6、業務經營分析以及競爭分析,影響企業發展戰略

5、  如何構建電商用戶畫像

5.1 構建電商用戶畫像技術和流程

 

 

 

構建一個用戶畫像,包括數據源端數據收集、數據預處理、行爲建模、構建用戶畫像

有些標籤是能夠直接獲取到的,有些標籤須要經過數據挖掘分析到!

5.2 源數據分析

 

 

 

 

用戶數據分爲2類:動態信息數據、靜態信息數據

靜態信息數據來源:

  • 用戶填寫的我的資料,或者由此經過必定的算法,計算出來的數據
  • 若是有不肯定的,能夠創建模型來判斷,好比用戶的性別註冊沒有填寫,能夠創建模型,根據用戶的行爲來判斷用戶性別是什麼,或者它的機率

動態信息數據來源:

  • 用戶行爲產生的數據:註冊、遊覽、點擊、購買、簽收、評價、收藏等等。
  • 用戶比較重要的行爲數據:遊覽商品,收藏商品、加入購物車、關注商品

根據這些行爲特性能夠計算出:用戶註冊時間、首單時間、潮媽族、糾結商品、最大消費、訂單數量、退貨數量、敗家指數、品牌偏好等等。

5.3 目標分析

用戶畫像的目標是經過分析用戶行爲,最終爲每一個用戶打上標籤,以及該標籤的權重。

如,紅酒 0.8、李寧 0.6

標籤:表現了內容,用戶對該內容有興趣、偏好、需求等等。

權重:表現了指數,用戶的興趣、偏好指數,也可能表現用戶的需求度,能夠簡單的理解爲可信度,機率。

5.4 用戶畫像建模

5.4.1 用戶基本屬性表

根據用戶所填寫的屬性標籤和推算出來的標籤。用於瞭解用戶的人口屬性的基本狀況和按不一樣屬性維度統計。

做用:按人口屬性營銷、好比營銷80後,對金牛座的優惠,生日營銷。

主要數據來源:用戶表、用戶調查表、孕婦模型表、馬甲模型表。

用戶表:記錄用戶最基本的屬性特性。

用戶調查表:補充用戶的其餘基本信息。

 

用戶所填寫的基本信息:用戶ID、用戶名、密碼、性別、手機號、郵箱、年齡、戶籍省份、身份證編號、註冊時間、收貨地址等

用戶所填信息計算獲得的指標:

生日、星座、城市等級、手機前幾位、手機運營商、郵件運營商

用戶調查表獲得:學歷、收入、職業、婚姻、是否有小孩、是否有車有房、使用手機品牌。

根據算法獲得:

身高、體重、性別模型、孩子性別機率、潛在汽車用戶機率、是否孕婦、孩子年齡機率、手機品牌、更換手機頻率、是否有小孩,是否有車,使用手機檔次,疑似馬甲標準、疑似馬甲帳號數、用戶忠誠度、用戶購物類型。

模型算法---性別模型

  • 用戶本身也填寫了性別,但仍然要用算法算一次性別

用戶性別

10-1未識別

一、商品性別得分

二、用戶購買上述商品計算用戶性別等得分

三、最優化算法訓練閥值,根據閥值判斷

孩子性別

0 僅有男孩

1僅有女孩

2男女都有

3沒法識別

一、選擇男孩女孩商品

二、肯定用戶購買商品的男女性別比例

三、訓練閥值,判斷孩子性別,同用戶性別相似

  • 性別驗證方法

隨機抽樣幾千條數據讓客戶打電話確認。

與用戶本身填的性別作對比,確認百分比。

模型算法---用戶汽車模型

用戶是否有車

10 沒有

-1 未識別

根據用戶購買車相關產品

判斷用戶是否有車

潛在汽車用戶

10 沒有

-1 未識別

用戶遊覽或者搜索汽車

用戶數據判斷

 

 

模型算法---用戶忠誠度模型

  • 忠誠度越高的用戶越多,對網站的發展越有利

用戶忠誠度

1忠誠型用戶

2偶爾型用戶

3投資型用戶

4遊覽型用戶

-1未識別

整體規則是判斷+聚類算法

1、遊覽用戶型:只遊覽不購買的

2、購買天數大於必定天數的爲忠誠用戶

3、購買天數小於必定天數,大部分是有優惠才購買的

4、其餘類型根據購買天數,購買最後一次距今時間,購買金額進行聚類

 

模型算法---用戶身高尺碼模型

男性用戶身高尺碼

xxx-xxx身高段,-1未識別  

用戶購買服裝鞋帽等用戶判斷

男性身材

1偏瘦、2標準、3偏胖4肥胖、-1未識別

用戶購買服裝鞋帽等用戶判斷

女性用戶身高尺碼

xxx-xxx身高段,-1未識別

用戶購買服裝鞋帽等用戶判斷

女性身材

1偏瘦、2標準、3偏胖4肥胖、-1未識別

用戶購買服裝鞋帽等用戶判斷

 

模型算法---用戶馬甲標誌模型

  • 馬甲是指一個用戶註冊多個帳號
  • 屢次訪問地址相同的用戶帳號是同一我的全部
  • 同一臺手機登錄屢次的用戶是同一我的全部
  • 收貨手機號相同的帳號同一我的全部

 

模型算法---手機相關標籤模型

  • 對於手機營銷參考意義比較大
  • 使用手機品牌: 最經常使用手機直接獲得
  • 使用手機品牌檔次:根據檔次維表
  • 使用多少種不一樣的手機:手機登錄狀況
  • 更換手機頻率(月份):按時間段看手機登錄狀況

 

5.4.2 客戶消費訂單表

根據客戶消費的狀況提取的客戶標籤,用於瞭解用戶的消費整體狀況,

最終的目的根據用戶消費習慣與消費能力作營銷。

 

主要數據來源:訂單表、退貨表、用戶表、購物車表

訂單表能夠獲得相關標籤:

第一次消費時間、

最近一次消費時間、

首單距今時間、

尾單距今時間------分析用戶何時來購買商品以及多久沒有購買了。

最小消費金額、

最大消費金額、

累計消費次數(不含退拒)

累計消費金額(不含退拒)、

累計使用代金券金額、

累計使用代金券次數。-----分析用戶整體消費狀況。

     客單價(含退拒)、

     近60天客單價(含退拒)-----分析用戶消費水平。

     經常使用收貨地址、

     經常使用支付方式----分析用戶經常使用的消費屬性,方便作定向營銷。

 

 

退貨表能夠獲得相關標籤:

30天購買次數(不含退拒)

30天購買金額(不含退拒)

30天購買次數(含退拒)

30天購買金額(含退拒)----分析用戶最近的消費能力。

退貨商品數量、

退貨商品金額、

拒收商品數量、

拒收商品金額、

最近一次退貨時間-----分析用戶拒收和退貨習慣。

 

 

購物車表能夠獲得相關標籤:

最近30天購物車次數、

最近30天購物車商品件數、

最近30天購物車提交商品件數、

最近30天購物車放棄件數、

最近30天購物車成功率------分析用戶購物車使用習慣

 

訂單表和用戶表能夠獲得相關標籤:

學校下單總數、

單位下單總數、

家裏下單總數、

上午下單總數、

下午下單總數、

晚上下單總數----分析用戶購物時間與地點習慣。

 

5.4.3 客戶購買類目表

 

根據客戶購買類目的狀況提取客戶標籤,用於瞭解類目的購買人羣狀況和針對某一類目的營銷等。

主要數據來源:訂單表、購物車表、類目維表

類目維表能夠獲得相關標籤:

一級分類ID

一級分類名稱、

二級分類ID

二級分類名稱、

三級分類ID

三級分類名稱-----分析用戶都購買了哪些類目。

 

電商的三級類目:

京東商城:

 

 

 

淘寶:

 

 

 

訂單表和類目維表能夠獲得相關標籤:

30天購買類目次數、

30天購買類目金額、

90天購買類目次數、

90天購買類目金額、

180天購買類目次數、

180天購買類目金額、

累計購買類目次數、

累計購買類目金額----分析用戶最近都購買了哪些類目。

最近一次購買類目時間、

最後一次購買類目距今天數----分析用戶多久沒有購買這個類目。

 

購物車表和類目維表能夠獲得相關標籤:

30天購物車類目次數、

30天購物車類目金額、

90天購物車類目次數、

90天購物車類目金額----分析用戶最近都挑中哪些類目。

 

 

5.4.4 用戶訪問信息表

根據客戶訪問的狀況提取相關客戶標籤。

用於瞭解用戶的訪問整體狀況,方便根據客戶遊覽習慣作營銷

主要數據來源:點擊流日誌行爲表(PC/APP端)

點擊流日誌行爲表能夠獲得相關標籤:

最近一次APP/PC端訪問日期、

最近一次APP/PC端訪問使用操做系統、

最近一次APP/PC端訪問使用遊覽器、

最近一次訪問IP地址、

最近一次訪問城市、

最近一次訪問的省份-----分析用戶最近一次訪問狀況。

 

第一次APP/PC端訪問日期、

第一次APP/PC端訪問使用操做系統、

第一次APP/PC端訪問使用遊覽器、

第一次訪問IP地址、

第一次訪問城市、

第一次訪問的省份-----分析用戶第一次訪問狀況。

 

7APP/PC端訪問次數、

30APP/PC訪問次數、

60APP/PC端訪問次數、

90APP/PC端訪問次數、

180APP/PC端訪問次數、

365APP/PC端訪問次數----分析用戶APP/PC端訪問次數。

 

30PC/APP端訪問天數、

30PC/APP端訪問併購買次數、

30PC/APP端訪問PV

30PC/APP端訪問平均PV

30PC/APP端最經常使用的遊覽器、

30PC/APP端不一樣IP數、

30PC/APP端最經常使用IP-----分析用戶訪問詳情。

 

300-5點訪問的次數、

306-7點訪問的次數、

308-9點訪問的次數、

3010-12點訪問的次數、

3013-14點訪問的次數、

3015-17點訪問的次數、

3018-19點訪問的次數、

3020-21點訪問的次數、

3022-23點訪問的次數----分析用戶喜歡在哪一個時間上網訪問。

 

 

6、  電商用戶畫像環境搭建

衆所周知,Hive的執行任務是將hql語句轉化爲MapReduce來計算的,Hive的總體解決方案很不錯,可是從查詢提交到結果返回須要至關長的時間,查詢耗時太長。這個主要緣由就是因爲Hive原生是基於MapReduce的,那麼若是咱們不生成MapReduce Job,而是生成Spark Job,就能夠充分利用Spark的快速執行能力來縮短HiveHQL的響應時間。

本項目採用SparkSql與hive進行整合(spark on hive),經過SparkSql讀取hive中表的元數據,把HiveHQL底層採用MapReduce來處理任務,致使性能慢的特色,改成更增強大的Spark引擎來進行相應的分析處理,快速的爲用戶打上標籤構建用戶畫像。

6.1  環境準備

  • 1、搭建hadoop集羣
  • 2、安裝hive構建數據倉庫
  • 3、安裝spark集羣
  • 4sparksql 整合hive

 

 

6.2  sparksql整合hive

   Spark SQL主要目的是使得用戶能夠在Spark上使用SQL,其數據源既能夠是RDD,也能夠是外部的數據源(好比文本、HiveJson等)。Spark SQL的其中一個分支就是Spark on Hive,也就是使用HiveHQL的解析、邏輯執行計劃翻譯、執行計劃優化等邏輯,能夠近似認爲僅將物理執行計劃從MR做業替換成了Spark做業。SparkSql整合hive就是獲取hive表中的元數據信息,而後經過SparkSql來操做數據

 

整合步驟:

① 須要將hive-site.xml文件拷貝到Sparkconf目錄下,這樣就能夠經過這個配置文件找到Hive的元數據以及數據存放位置。

② 若是Hive的元數據存放在Mysql中,咱們還須要準備好Mysql相關驅動,好比:mysql-connector-java-5.1.35.jar

6.3  測試sparksql整合hive是否成功

先啓動hadoop集羣,在啓動spark集羣,確保啓動成功以後執行命令:

/var/local/spark/bin/spark-sql  --master spark://itcast01:7077  --executor-memory 1g  --total-executor-cores 4

指明master地址、每個executor的內存大小、一共所須要的核數、

mysql數據庫鏈接驅動。

執行成功後的界面:進入到spark-sql 客戶端命令行界面

 

 

接下來就能夠經過sql語句來操做數據庫表:

查看當前有哪些數據庫 ---show databases;

 

 

 

看到以上結果,說明sparksql整合hive成功!

日誌太多,咱們能夠修改spark的日誌輸出級別(conf/log4j.properties)

 

前方高能:

spark2.0版本後因爲出現了sparkSession,在初始化sqlContext的時候,會設置默認的spark.sql.warehouse.dir=spark-warehouse,

此時將hivesparksql整合完成以後,在經過spark-sql腳本啓動的時候,仍是會在哪裏啓動spark-sql腳本,就會在當前目錄下建立一個spark.sql.warehouse.dirspark-warehouse的目錄,存放由spark-sql建立數據庫和建立表的數據信息,與以前hive的數據信息不是放在同一個路徑下(能夠互相訪問)。可是此時spark-sql中表的數據在本地,不利於操做,也不安全。

 

全部在啓動的時候須要加上這樣一個參數:

--conf  spark.sql.warehouse.dir=hdfs://node1:9000/user/hive/warehouse

保證spark-sql啓動時不在產生新的存放數據的目錄,sparksqlhive最終使用的是hive同一存放數據的目錄。

若是使用的是spark2.0以前的版本,因爲沒有sparkSession,不會有spark.sql.warehouse.dir配置項,不會出現上述問題。

 

最後的執行腳本;

spark-sql \

--master spark://node1:7077 \

--executor-memory 1g \

--total-executor-cores 2 \

--conf  spark.sql.warehouse.dir=hdfs://node1:9000/user/hive/warehouse

 

 

 

 

 

 

 

7、  電商用戶畫像數據倉庫創建

7.1  數據倉庫準備工做

爲何要對數據倉庫分層?星型模型 雪花模型

User----->web界面展現指標表

l    用空間換時間,經過大量的預處理來提高應用系統的用戶體驗(效率),所以數據倉庫會存在大量冗餘的數據;

l     若是不分層的話,若是源業務系統的業務規則發生變化將會影響整個數據清洗過程,工做量巨大

l     經過數據分層管理能夠簡化數據清洗的過程,由於把原來一步的工做分到了多個步驟去完成,至關於把一個複雜的工做拆成了多個簡單的工做,把一個大的黑盒變成了一個白盒,每一層的處理邏輯都相對簡單和容易理解,這樣咱們比較容易保證每個步驟的正確性,當數據發生錯誤的時候,每每咱們只須要局部調整某個步驟便可。

數據倉庫標準上能夠分爲四層:ODS(臨時存儲層)、PDW(數據倉庫層)、MID(數據集市層)、APP(應用層)

ODS層:

爲臨時存儲層,是接口數據的臨時存儲區域,爲後一步的數據處理作準備。通常來講ODS層的數據和源系統的數據是同構的,主要目的是簡化後續數據加工處理的工做。從數據粒度上來講ODS層的數據粒度是最細的。ODS層的表一般包括兩類,一個用於存儲當前須要加載的數據,一個用於存儲處理完後的歷史數據。歷史數據通常保存3-6個月後須要清除,以節省空間。但不一樣的項目要區別對待,若是源系統的數據量不大,能夠保留更長的時間,甚至全量保存;

PDW層:

爲數據倉庫層,PDW層的數據應該是一致的、準確的、乾淨的數據,即對源系統數據進行了清洗(去除了雜質)後的數據。這一層的數據通常是遵循數據庫第三範式的,其數據粒度一般和ODS的粒度相同。在PDW層會保存BI系統中全部的歷史數據,例如保存10年的數據

MID層:

爲數據集市層,這層數據是面向主題來組織數據的,一般是星形或雪花結構的數據。從數據粒度來講,這層的數據是輕度彙總級的數據,已經不存在明細數據了。從數據的時間跨度來講,一般是PDW層的一部分,主要的目的是爲了知足用戶分析的需求,而從分析的角度來講,用戶一般只須要分析近幾年(如近三年的數據)的便可。從數據的廣度來講,仍然覆蓋了全部業務數據。

APP層:

爲應用層,這層數據是徹底爲了知足具體的分析需求而構建的數據,也是星形或雪花結構的數據。從數據粒度來講是高度彙總的數據。從數據的廣度來講,則並不必定會覆蓋全部業務數據,而是MID層數據的一個真子集,從某種意義上來講是MID層數據的一個重複。從極端狀況來講,能夠爲每一張報表在APP層構建一個模型來支持,達到以空間換時間的目的數據倉庫的標準分層只是一個建議性質的標準,實際實施時須要根據實際狀況肯定數據倉庫的分層,不一樣類型的數據也可能採起不一樣的分層方法。

 

這裏咱們採用的是京東的數據倉庫分層模式,是根據標準的模型演化而來。

數據倉庫分層:

BDM:緩衝數據,源數據的直接映像

FDM:基礎數據層,數據拉鍊處理、分區處理

GDM:通用聚合

ADM:高度聚合

 

先把數據從源數據庫中抽取加載到BDM層中,

而後FDM層根據BDM層的數據按天分區

 

7.2  數據倉庫基本表介紹

 

 

 

 

 

 

BDM層數據表

(貼源緩存層)

 

 

訂單表

 

 

itcast_bdm_order

 

訂單明細表

 

 

itcast_bdm_order_desc

 

訂單商品表

 

 

itcast_bdm_order_goods

 

 用戶表

 

 

itcast_bdm_user

 

 購物車表

 

 

itcast_bdm_order_cart

 

用戶上網記錄表

 

itcast_bdm_user_pc_click_log

itcast_bdm_user_app_click_log

 

 

 

 

 

 

 

 

 

FDM層數據表

(拉鍊表、分區表)

 

 

用戶寬表

 

itcast_fdm_user_wide

 

購物車表

 

itcast_fdm_order_cart

 

訂單表

 

itcast_fdm_order

 

訂單代表細表

 

itcast_fdm_order_desc

 

用戶appview

 

itcast_fdm_user_app_pageview

 

用戶pcview

 

itcast_fdm_user_pc_pageview

 

 

 

 

GDM層數據表

(通用數據模型層)

 

 

客戶基本屬性表

 

itcast_gdm_user_basic

      

客戶消費訂單表

 

itcast_gdm_user_consume_order

 

訂單模型表

 

itcast_gdm_order

 

客戶購買類目表

 

itcast_gdm_user_buy_category

      

客戶訪問信息表

 

itcast_gdm_user_visit

 

8、  電商用戶畫像開發

8.1用戶畫像--數據開發的步驟

u 數據開發前置依賴

     -需求肯定    pv uv  topn

     -建模肯定表結構  create table t1(pv int,uv int,topn string)

     -實現方案肯定

u 數據開發過程

 -表落地

 -sql語句實現業務邏輯

 -部署代碼

 -數據測試

 -試運行與上線

 

在接下來的客戶基本屬性表開發中演示開發的流程。

 

8.2 用戶畫像開發--客戶基本屬性表

--用戶畫像-客戶基本屬性模型表

create database if not exists gdm;

create table if not exists gdm.itcast_gdm_user_basic(

user_id string       ,--用戶ID

user_name string ,--用戶登錄名

user_sex  string ,--用戶性別

user_birthday string       ,--用戶生日

user_age  bigint ,--用戶年齡

constellation string       ,--用戶星座

province string ,--省份

city string             ,--城市

city_level string ,--城市等級

hex_mail string ,--郵箱

op_mail string ,--郵箱運營商

hex_phone string ,--手機號

fore_phone string ,--手機前3

op_phone string ,--手機運營商

add_time timestamp ,--註冊時間

login_ip string ,--登錄ip地址

login_source string ,--登錄來源

request_user string ,--邀請人

total_mark bigint ,--會員積分

used_mark bigint ,--已使用積分

level_name string ,--會員等級名稱

blacklist bigint       ,--用戶黑名單

is_married bigint ,--婚姻情況

education string ,--學歷

monthly_money double ,--收入

profession string ,--職業

sex_model bigint ,--性別模型

is_pregnant_woman bigint       ,--是否孕婦

is_have_children bigint ,--是否有小孩

children_sex_rate double       ,--孩子性別機率

children_age_rate double       ,--孩子年齡機率

is_have_car bigint ,--是否有車

potential_car_user_rate double     ,--潛在汽車用戶機率

phone_brand string ,--使用手機品牌

phone_brand_level string       ,--使用手機品牌檔次

phone_cnt bigint ,--使用多少種不一樣的手機

change_phone_rate bigint       ,--更換手機頻率

majia_flag string ,--馬甲標誌

majie_account_cnt bigint       ,--馬甲帳號數量

loyal_model bigint ,--用戶忠誠度

shopping_type_model bigint       ,--用戶購物類型

figure_model bigint ,--身材

stature_model bigint       ,--身高

dw_date timestamp

) partitioned by (dt string);

 

 

該模型表其基本信息主要來源於用戶表、用戶調查表。有靜態信息和動態信息、後面的一些是數據挖掘模型(數據挖掘模型比較多,邏輯比較複雜,在機器學習課程中給你們介紹)。

#***************************

--客戶基本屬性模型表BDM

create database if not exists bdm;

create external table if not exists bdm.itcast_bdm_user(

user_id string     ,--用戶ID

user_name string     ,--用戶登錄名

user_sex  string     ,--用戶性別

user_birthday string     ,--用戶生日

user_age  bigint     ,--用戶年齡

constellation string     ,--用戶星座

province string     ,--省份

city string     ,--城市

city_level string     ,--城市等級

hex_mail string     ,--郵箱

op_mail string     ,--郵箱運營商

hex_phone string     ,--手機號

fore_phone string     ,--手機前3

op_phone string     ,--手機運營商

add_time string     ,--註冊時間

login_ip string     ,--登錄ip地址

login_source string     ,--登錄來源

request_user string     ,--邀請人

total_mark bigint     ,--會員積分

used_mark bigint     ,--已使用積分

level_name string     ,--會員等級名稱

blacklist bigint     ,--用戶黑名單

is_married bigint     ,--婚姻情況

education string     ,--學歷

monthly_money double     ,--收入

profession string           --職業

) partitioned by (dt string)

row format delimited fields terminated by ',';

alter table itcast_bdm_user add partition (dt='2017-01-01') location '/business/itcast_bdm_user/2017-01-01';

 

--客戶基本屬性表FDM

create database if not exists fdm;

create table if not exists fdm.itcast_fdm_user_wide(

user_id string     ,--用戶ID

user_name string     ,--用戶登錄名

user_sex  string     ,--用戶性別

user_birthday string     ,--用戶生日

user_age  bigint     ,--用戶年齡

constellation string     ,--用戶星座

province string     ,--省份

city string     ,--城市

city_level string     ,--城市等級

hex_mail string     ,--郵箱

op_mail string     ,--郵箱運營商

hex_phone string     ,--手機號

fore_phone string     ,--手機前3

op_phone string     ,--手機運營商

add_time string     ,--註冊時間

login_ip string     ,--登錄ip地址

login_source string     ,--登錄來源

request_user string     ,--邀請人

total_mark bigint     ,--會員積分

used_mark bigint     ,--已使用積分

level_name string     ,--會員等級名稱

blacklist bigint     ,--用戶黑名單

is_married bigint     ,--婚姻情況

education string     ,--學歷

monthly_money double     ,--收入

profession string     ,--職業

dw_date  timestamp

) partitioned by (dt string);

 

--加載數據

insert overwrite table fdm.itcast_fdm_user_wide partition(dt='2017-01-01')

select

t.user_id,

t.user_name,

t.user_sex,

t.user_birthday,

t.user_age,

t.constellation,

t.province,

t.city,

t.city_level,

t.hex_mail,

t.op_mail,

t.hex_phone,

t.fore_phone,

t.op_phone,

t.add_time,

t.login_ip,

t.login_source,

t.request_user,

t.total_mark,

t.used_mark,

t.level_name,

t.blacklist,

t.is_married,

t.education,

t.monthly_money,

t.profession,

from_unixtime(unix_timestamp())  dw_date

from bdm.itcast_bdm_user t where dt='2017-01-01';

 

--用戶畫像-客戶基本屬性模型表GDM

create database if not exists gdm;

create  table if not exists gdm.itcast_gdm_user_basic(

user_id string       ,--用戶ID

user_name string ,--用戶登錄名

user_sex  string ,--用戶性別

user_birthday string       ,--用戶生日

user_age  bigint ,--用戶年齡

constellation string       ,--用戶星座

province string ,--省份

city string       ,--城市

city_level string ,--城市等級

hex_mail string ,--郵箱

op_mail string ,--郵箱運營商

hex_phone string ,--手機號

fore_phone string ,--手機前3

op_phone string ,--手機運營商

add_time string ,--註冊時間

login_ip string ,--登錄ip地址

login_source string ,--登錄來源

request_user string ,--邀請人

total_mark bigint ,--會員積分

used_mark bigint ,--已使用積分

level_name string ,--會員等級名稱

blacklist bigint       ,--用戶黑名單

is_married bigint ,--婚姻情況

education string ,--學歷

monthly_money double ,--收入

profession string ,--職業

sex_model bigint ,--性別模型

is_pregnant_woman bigint       ,--是否孕婦

is_have_children bigint ,--是否有小孩

children_sex_rate double       ,--孩子性別機率

children_age_rate double       ,--孩子年齡機率

is_have_car bigint ,--是否有車

potential_car_user_rate double     ,--潛在汽車用戶機率

phone_brand string ,--使用手機品牌

phone_brand_level string       ,--使用手機品牌檔次

phone_cnt bigint ,--使用多少種不一樣的手機

change_phone_rate bigint       ,--更換手機頻率

majia_flag string ,--馬甲標誌

majie_account_cnt bigint       ,--馬甲帳號數量

loyal_model bigint ,--用戶忠誠度

shopping_type_model bigint       ,--用戶購物類型

figure_model bigint ,--身材

stature_model bigint       ,--身高

dw_date timestamp

) partitioned by (dt string);

 

--加載數據

insert overwrite table  gdm.itcast_gdm_user_basic partition(dt='2017-01-01')

select

t.user_id,

t.user_name,

t.user_sex,

t.user_birthday,

t.user_age,

t.constellation,

t.province,

t.city,

t.city_level,

t.hex_mail,

t.op_mail,

t.hex_phone,

t.fore_phone,

t.op_phone,

t.add_time,

t.login_ip,

t.login_source,

t.request_user,

t.total_mark,

t.used_mark,

t.level_name,

t.blacklist,

t.is_married,

t.education,

t.monthly_money,

t.profession,

null sex_model,--數據挖掘模型-開始

null is_pregnant_woman,

null is_have_children,

null children_sex_rate,

null children_age_rate,

null is_have_car,

null potential_car_user_rate,

null phone_brand,

null phone_brand_level,

null phone_cnt,

null change_phone_rate,

null majia_flag,

null majie_account_cnt,

null loyal_model,

null shopping_type_model,

null figure_model,

null stature_model,--數據挖掘模型-結束

from_unixtime(unix_timestamp())  dw_date

from (select * from fdm.itcast_fdm_user_wide where dt='2017-01-01') t;

 

演示模型表開發腳本:

######################

#名稱:客戶基本屬性模型表

# itcast_gdm_user_basic.sh

######################

#!/bin/sh

yesterday=`date -d '-1 day' "+%Y-%m-%d"`

if [ $1 ];then

yesterday=$1

fi

 

SPARK_SUBMIT_INFO="/export/servers/spark/bin/spark-sql --master spark://node1:7077 --executor-memory 1g --total-executor-cores 2 --conf spark.sql.warehouse.dir=hdfs://node1:9000/user/hive/warehouse"

 

SOURCE_DATA="/root/source_data"

 

SQL_BDM="create database if not exists bdm;

create external table if not exists bdm.itcast_bdm_user(

user_id string     ,--用戶ID

user_name string ,--用戶登錄名

user_sex  string ,--用戶性別

user_birthday string ,--用戶生日

user_age  bigint ,--用戶年齡

constellation string ,--用戶星座

province string     ,--省份

city string ,--城市

city_level string ,--城市等級

hex_mail string ,--郵箱

op_mail string ,--郵箱運營商

hex_phone string ,--手機號

fore_phone string ,--手機前3

op_phone string ,--手機運營商

add_time string ,--註冊時間

login_ip string ,--登錄ip地址

login_source string ,--登錄來源

request_user string ,--邀請人

total_mark bigint ,--會員積分

used_mark bigint ,--已使用積分

level_name string ,--會員等級名稱

blacklist bigint ,--用戶黑名單

is_married bigint ,--婚姻情況

education string ,--學歷

monthly_money double ,--收入

profession string --職業

) partitioned by (dt string)

row format delimited fields terminated by ','

location '/business/bdm/itcast_bdm_user' ;

alter table bdm.itcast_bdm_user add partition (dt='$yesterday');"

 

 

SQL_FDM="create database if not exists fdm;

create table if not exists fdm.itcast_fdm_user_wide(

user_id string     ,--用戶ID

user_name string ,--用戶登錄名

user_sex  string ,--用戶性別

user_birthday string ,--用戶生日

user_age  bigint ,--用戶年齡

constellation string ,--用戶星座

province string     ,--省份

city string ,--城市

city_level string ,--城市等級

hex_mail string ,--郵箱

op_mail string ,--郵箱運營商

hex_phone string ,--手機號

fore_phone string ,--手機前3

op_phone string ,--手機運營商

add_time string ,--註冊時間

login_ip string ,--登錄ip地址

login_source string ,--登錄來源

request_user string ,--邀請人

total_mark bigint ,--會員積分

used_mark bigint ,--已使用積分

level_name string ,--會員等級名稱

blacklist bigint ,--用戶黑名單

is_married bigint ,--婚姻情況

education string ,--學歷

monthly_money double ,--收入

profession string ,--職業

dw_date  timestamp

) partitioned by (dt string);"

 

##加載數據

LOAD_FDM="

insert overwrite table fdm.itcast_fdm_user_wide partition(dt='$yesterday')

select

t.user_id,

t.user_name,

t.user_sex,

t.user_birthday,

t.user_age,

t.constellation,

t.province,

t.city,

t.city_level,

t.hex_mail,

t.op_mail,

t.hex_phone,

t.fore_phone,

t.op_phone,

t.add_time,

t.login_ip,

t.login_source,

t.request_user,

t.total_mark,

t.used_mark,

t.level_name,

t.blacklist,

t.is_married,

t.education,

t.monthly_money,

t.profession,

from_unixtime(unix_timestamp())  dw_date

from bdm.itcast_bdm_user t where dt='$yesterday';"

 

SQL_GDM="create database if not exists gdm;

create  table if not exists gdm.itcast_gdm_user_basic(

user_id string     ,--用戶ID

user_name string ,--用戶登錄名

user_sex  string ,--用戶性別

user_birthday string ,--用戶生日

user_age  bigint ,--用戶年齡

constellation string ,--用戶星座

province string     ,--省份

city string ,--城市

city_level string ,--城市等級

hex_mail string ,--郵箱

op_mail string ,--郵箱運營商

hex_phone string ,--手機號

fore_phone string ,--手機前3

op_phone string ,--手機運營商

add_time string ,--註冊時間

login_ip string ,--登錄ip地址

login_source string ,--登錄來源

request_user string ,--邀請人

total_mark bigint ,--會員積分

used_mark bigint ,--已使用積分

level_name string ,--會員等級名稱

blacklist bigint ,--用戶黑名單

is_married bigint ,--婚姻情況

education string ,--學歷

monthly_money double ,--收入

profession string ,--職業

sex_model bigint ,--性別模型

is_pregnant_woman bigint ,--是否孕婦

is_have_children bigint ,--是否有小孩

children_sex_rate double ,--孩子性別機率

children_age_rate double ,--孩子年齡機率

is_have_car bigint ,--是否有車

potential_car_user_rate double,--潛在汽車用戶機率

phone_brand string ,--使用手機品牌

phone_brand_level string ,--使用手機品牌檔次

phone_cnt bigint ,--使用多少種不一樣的手機

change_phone_rate bigint ,--更換手機頻率

majia_flag string ,--馬甲標誌

majie_account_cnt bigint ,--馬甲帳號數量

loyal_model bigint ,--用戶忠誠度

shopping_type_model bigint ,--用戶購物類型

figure_model bigint ,--身材

stature_model bigint ,--身高

dw_date timestamp

) partitioned by (dt string);"

 

 

##加載數據到GDM

LOAD_GDM="insert overwrite table  gdm.itcast_gdm_user_basic partition(dt='$yesterday')

select

t.user_id,

t.user_name,

t.user_sex,

t.user_birthday,

t.user_age,

t.constellation,

t.province,

t.city,

t.city_level,

t.hex_mail,

t.op_mail,

t.hex_phone,

t.fore_phone,

t.op_phone,

t.add_time,

t.login_ip,

t.login_source,

t.request_user,

t.total_mark,

t.used_mark,

t.level_name,

t.blacklist,

t.is_married,

t.education,

t.monthly_money,

t.profession,

null sex_model,--數據挖掘模型-開始

null is_pregnant_woman,

null is_have_children,

null children_sex_rate,

null children_age_rate,

null is_have_car,

null potential_car_user_rate,

null phone_brand,

null phone_brand_level,

null phone_cnt,

null change_phone_rate,

null majia_flag,

null majie_account_cnt,

null loyal_model,

null shopping_type_model,

null figure_model,

null stature_model,--數據挖掘模型-結束

from_unixtime(unix_timestamp())  dw_date

from (select * from fdm.itcast_fdm_user_wide where dt='$yesterday') t;"

 

 

##建立BDM層表

echo "${SQL_BDM}"

$SPARK_SUBMIT_INFO -e "${SQL_BDM}"

 

##添加數據到BDM

hdfs dfs -put $SOURCE_DATA/itcast_bdm_user.txt /business/bdm/itcast_bdm_user/"dt=$yesterday"

 

##建立FDM層表

echo "${SQL_FDM}"

$SPARK_SUBMIT_INFO -e "${SQL_FDM}"

 

##導入數據到FDM

echo "${LOAD_FDM}"

$SPARK_SUBMIT_INFO -e "${LOAD_FDM}"

 

##建立GDM層表

echo "${SQL_GDM}"

$SPARK_SUBMIT_INFO -e "${SQL_GDM}"

 

##導入GDM數據

echo "${LOAD_GDM}"

$SPARK_SUBMIT_INFO -e "${LOAD_GDM}"

 

 

8.3 用戶畫像開發--訂單表寬表

--訂單寬表模型

create database if not exists gdm;

create  table if not exists gdm.itcast_gdm_order(

order_id   string,--訂單ID

order_no   string,--訂單號

order_date string,--訂單日期

user_id string,--用戶ID

user_name string,--登陸名

order_money double,--訂單金額

order_type string,--訂單類型

order_status       string,--訂單狀態

pay_status string,--支付狀態

pay_type string,--支付方式  1、在線支付,2、貨到付款

order_source       string,--訂單來源

consignee string,--收貨人姓名

area_id string,--收貨人地址ID

area_name string,--地址ID對應的地址段(粒度到縣)

address string,--收貨人地址(手工填寫的地址)

mobile string,--收貨人手機號

telphone string,--收貨人電話

coupon_id bigint,--使用代金券ID

coupon_money double,--使用代金券金額

carriage_money double,--運費

create_time timestamp,--建立時間

update_time timestamp,--更新時間

dw_date timestamp

) partitioned by (dt string);

 

--訂單主要信息表BDM

create database if not exists bdm;

create external table if not exists bdm.itcast_bdm_order(

order_id string, --訂單ID

order_no string, --訂單號

order_date string, --訂單日期

user_id  string, --用戶ID

user_name string, --登陸名

order_money double, --訂單金額

order_type string, --訂單類型

order_status string,       --訂單狀態

pay_status string, --支付狀態

pay_type string, --支付方式  1、在線支付,2、貨到付款

order_source string,       --訂單來源

update_time timestamp, --訂單更新時間

dw_date timestamp

) partitioned by (dt string)

row format delimited fields terminated by ','

lines terminated by '\n';

alter table bdm.itcast_bdm_order add partition (dt='2017-01-01') location '/business/itcast_bdm_order/2017-01-01';

hdfs dfs -put fdm_order.txt /business/itcast_bdm_order/2017-01-01

 

--訂單主要信息表FDM

create database if not exists fdm;

create  table if not exists fdm.itcast_fdm_order(

order_id string, --訂單ID

order_no string, --訂單號

order_date string, --訂單日期

user_id  string, --用戶ID

user_name string, --登陸名

order_money double, --訂單金額

order_type string, --訂單類型

order_status string,       --訂單狀態

pay_status string, --支付狀態

pay_type string, --支付方式  1、在線支付,2、貨到付款

order_source string,       --訂單來源

update_time timestamp, --訂單更新時間

dw_date timestamp

) partitioned by (dt string);

 

--加載數據

insert overwrite table fdm.itcast_fdm_order partition(dt='2017-01-01')

select

t.order_id, --訂單ID

t.order_no, --訂單號

t.order_date,       --訂單日期

t.user_id, --用戶ID

t.user_name,       --登陸名

t.order_money, --訂單金額

t.order_type,       --訂單類型

t.order_status,       --訂單狀態

t.pay_status,       --支付狀態

t.pay_type, --支付方式

t.order_source,       --訂單來源

t.update_time timestamp,--訂單更新時間

from_unixtime(unix_timestamp())  dw_date

from bdm.itcast_bdm_order t where dt='2017-01-01';

 

-------訂單詳細信息表BDM----------------

create database if not exists bdm;

create external table if not exists bdm.itcast_bdm_order_desc(

order_id string, --訂單ID

order_no string, --訂單號

consignee string, --收貨人姓名

area_id string, --收貨人地址ID

area_name string, --地址ID對應的地址段

address string, --收貨人地址

mobile string, --收貨人手機號

telphone string, --收貨人電話

coupon_id bigint, --使用代金券ID

coupon_money double, --使用代金券金額

carriage_money double, --運費

create_time timestamp, --建立時間

update_time timestamp, --更新時間

dw_date timestamp

)partitioned by (dt string)

row format delimited fields terminated by ',';

alter table bdm.itcast_bdm_order_desc add partition (dt='2017-01-01') location '/business/itcast_bdm_order_desc/2017-01-01';

hdfs dfs -put itcast_bdm_order_desc.txt /business/itcast_bdm_order_desc/2017-01-01

 

-----訂單主要信息表FDM

create database if not exists fdm;

create table if not exists fdm.itcast_fdm_order_desc(

order_id string, --訂單ID

order_no string, --訂單號

consignee string, --收貨人姓名

area_id string, --收貨人地址ID

area_name string, --地址ID對應的地址段

address string, --收貨人地址

mobile string, --收貨人手機號

telphone string, --收貨人電話

coupon_id bigint, --使用代金券ID

coupon_money double, --使用代金券金額

carriage_money double, --運費

create_time timestamp, --建立時間

update_time timestamp, --更新時間

dw_date timestamp

) partitioned by (dt string);

 

------加載數據

insert overwrite table fdm.itcast_fdm_order_desc partition(dt='2017-01-01')

select

t.order_id, --訂單ID

t.order_no, --訂單號

t.consignee,       --收貨人姓名

t.area_id, --收貨人地址ID

t.area_name,       --地址ID對應的地址段

t.address, --收貨人地址

t.mobile, --收貨人手機號

t.telphone, --收貨人電話

t.coupon_id,       --使用代金券ID

t.coupon_money, --使用代金券金額

t.carriage_money,       --運費

t.create_time,       --建立時間

t.update_time,       --更新時間

from_unixtime(unix_timestamp())  dw_date

from bdm.itcast_bdm_order_desc t where dt='2017-01-01';

 

 

--------訂單寬表模型表GDM

create database if not exists gdm;

create external table if not exists gdm.itcast_gdm_order(

order_id string, --訂單ID

order_no string, --訂單號

order_date string, --訂單日期

user_id string, --用戶ID

user_name string, --登陸名

order_money double, --訂單金額

order_type string, --訂單類型

order_status string,--訂單狀態

pay_status string, --支付狀態

pay_type string, --支付方式  1、在線支付,2、貨到付款

order_source string,--訂單來源

consignee string, --收貨人姓名

area_id string, --收貨人地址ID

area_name string, --地址ID對應的地址段(粒度到縣)

address string, --收貨人地址(手工填寫的地址)

mobile string, --收貨人手機號

telphone string, --收貨人電話

coupon_id bigint, --使用代金券ID

coupon_money double,--使用代金券金額

carriage_money double,--運費

create_time timestamp,--建立時間

update_time timestamp,--更新時間

dw_date timestamp

) partitioned by (dt string);

 

---加載數據生成訂單寬表

insert overwrite table gdm.itcast_gdm_order partition(dt='2017-01-01')

select

a.order_id, --訂單ID

a.order_no, --訂單號

a.order_date,       --訂單日期

a.user_id, --用戶ID

a.user_name,       --用戶名

a.order_money, --訂單金額

a.order_type,       --訂單類型

a.order_status,    --訂單狀態

a.pay_status,       --支付類型

a.pay_type, --支付方式

a.order_source, --訂單來源

b.consignee,       --收貨人姓名

b.area_id, --收貨人地址ID

b.area_name,       --地址ID對應的地址段

b.address, --收貨人地址

b.mobile,          --收貨人手機號

b.telphone,         --收貨人電話

b.coupon_id,       --使用代金券ID

b.coupon_money,   --使用代金券金額

b.carriage_money,  --運費

b.create_time,     --建立時間

b.update_time,     --更新時間

from_unixtime(unix_timestamp()) dw_date

from (select *  from fdm.itcast_fdm_order where dt='2017-01-01') a

join (select * from fdm.itcast_fdm_order_desc where dt='2017-01-01') b on a.order_id=b.order_id;

 

8.4 用戶畫像開發--客戶消費訂單表

--用戶畫像 客戶消費訂單表

create database if not exists gdm;

create  table if not exists gdm.itcast_gdm_user_consume_order(

user_id string, --客戶ID

first_order_time timestamp, --第一次消費時間

last_order_time timestamp, --最近一次消費時間

first_order_ago bigint, --首單距今時間

last_order_ago bigint, --尾單距今時間

month1_hg_order_cnt bigint, --30天購買次數(不含退拒)

month1_hg_order_amt double, --30天購買金額(不含退拒)

month2_hg_order_cnt bigint, --60天購買次數(不含退拒)

month2_hg_order_amt double, --60天購買金額(不含退拒)

month3_hg_order_cnt bigint, --90天購買次數(不含退拒)

month3_hg_order_amt double, --90天購買金額(不含退拒)

month1_order_cnt bigint, --30天購買次數(含退拒)

month1_order_amt double, --30天購買金額(含退拒)

month2_order_cnt bigint, --60天購買次數(含退拒)

month2_order_amt double, --60天購買金額(含退拒)

month3_order_cnt bigint, --90天購買次數(含退拒)

month3_order_amt double, --90天購買金額(含退拒)

max_order_amt double, --最大消費金額

min_order_amt double, --最小消費金額

total_order_cnt bigint, --累計消費次數(不含退拒)

total_order_amt double, --累計消費金額(不含退拒)

user_avg_amt double, --客單價(含退拒)

month3_user_avg_amt double, --90天的客單價

common_address string, --經常使用收貨地址

common_paytype string, --經常使用支付方式

month1_cart_cnt bigint, --30天購物車的次數

month1_cart_goods_cnt bigint, --30天購物車商品件數

month1_cart_submit_cnt bigint, --30天購物車提交商品件數

month1_cart_rate double, --30天購物車成功率

month1_cart_cancle_cnt double, --30天購物車放棄件數

 

return_cnt bigint, --退貨商品數量

return_amt double, --退貨商品金額

reject_cnt bigint, --拒收商品數量

reject_amt double, --拒收商品金額

 

last_return_time timestamp, --最近一次退貨時間

school_order_cnt bigint, --學校下單總數

company_order_cnt bigint, --單位下單總數

home_order_cnt bigint, --家裏下單總數

 

forenoon_order_cnt bigint, --上午下單總數

afternoon_order_cnt bigint, --下午下單總數

night_order_cnt bigint, --晚上下單總數

morning_order_cnt bigint, --凌晨下單總數

dw_date timestamp

) partitioned by (dt string);

 

---客戶消費訂單模型表-臨時表01

drop table if exists gdm.itcast_gdm_user_consume_order_temp_01;

CREATE TABLE gdm.itcast_gdm_user_consume_order_temp_01 AS

SELECT

  t.user_id,

  MIN(order_date) first_order_time,--第一次消費時間

  MAX(order_date) last_order_time,--最近一次消費時間

  DATEDIFF(MIN(order_date), '2017-01-01') first_order_ago,--首單距今時間

  DATEDIFF(MAX(order_date), '2017-01-01') last_order_ago,--尾單距今時間

  SUM(

    CASE

      WHEN t.dat_30 = 1

      AND t.order_flag = 0

      THEN 1

    END

  ) month1_hg_order_cnt,--30天購買次數(不含退拒)

  SUM(

    CASE

      WHEN t.dat_30 = 1

      AND t.order_flag = 0

      THEN t.order_money

    END

  ) month1_hg_order_amt,--30天購買金額(不含退拒)

  SUM(

    CASE

      WHEN t.dat_60 = 1

      AND t.order_flag = 0

      THEN 1

    END

  ) month2_hg_order_cnt,--60天購買次數(不含退拒)

  SUM(

    CASE

      WHEN t.dat_60 = 1

      AND t.order_flag = 0

      THEN t.order_money

    END

  ) month2_hg_order_amt,--60天購買金額(不含退拒)

  SUM(

    CASE

      WHEN t.dat_90 = 1

      AND t.order_flag = 0

      THEN 1

    END

  ) month3_hg_order_cnt,--90天購買次數(不含退拒)

  SUM(

    CASE

      WHEN t.dat_90 = 1

      AND t.order_flag = 0

      THEN t.order_money

    END

  ) month3_hg_order_amt,--90天購買金額(不含退拒)

  SUM(dat_30) month1_order_cnt,--30天購買次數(含退拒)

  SUM(

    CASE

      WHEN t.dat_30 = 1

      THEN t.order_money

    END

  ) month1_order_amt,--30天購買金額(含退拒)

  SUM(dat_60) month2_order_cnt,--60天購買次數(含退拒)

  SUM(

    CASE

      WHEN t.dat_60 = 1

      THEN t.order_money

    END

  ) month2_order_amt,--60天購買金額(含退拒)

  SUM(dat_90) month3_order_cnt,--90天購買次數(含退拒)

 SUM(

    CASE

      WHEN t.dat_90 = 1

      THEN t.order_money

    END

  ) month3_order_amt,--90天購買金額(含退拒)

  MAX(t.order_money) max_order_amt,--最大消費金額

  MIN(t.order_money) min_order_amt,--最小消費金額

    SUM(

    CASE

      WHEN t.order_flag = 0

      THEN 1

    END

  ) total_order_cnt,--累計消費次數(不含退拒)

  SUM(

    CASE

      WHEN t.order_flag = 0

      THEN t.order_money

    END

  ) total_order_amt,--累計消費金額(不含退拒)

  SUM(coupon_money) total_coupon_amt,--累計使用代金券金額

  SUM(t.order_money) / COUNT(1) user_avg_amt,--客單價(含退拒)

  0 month3_user_avg_amt,--90天的客單價(含退拒)

  0 common_address,--經常使用收穫地址

  0 common_paytype,--經常使用支付方式

  0 month1_cart_cnt,--最近30天購物車次數

  0 month1_cart_goods_cnt,--最近30天購物車商品件數

  0 month1_cart_submit_cnt,--最近30天購物車提交商品件數

  0 month1_order_rate,--最近30天購物車成功率

  0 month1_cart_cancle_cnt,--最近30天購物車放棄件數

 SUM(

    CASE

      WHEN t.order_status = 3

      THEN t1.goods_amount

    END

  ) return_cnt,--退貨商品數量

  SUM(

    CASE

      WHEN t.order_status = 3

      THEN t.order_money

    END

  ) return_amt,--退貨商品金額

  SUM(

    CASE

      WHEN t.order_status = 4

      THEN t1.goods_amount

    END

  ) reject_cnt,--拒收商品數量

  SUM(

    CASE

      WHEN t.order_status = 4

      THEN t.order_money

    END

  ) reject_amt,--拒收商品金額

  MAX(

    CASE

      WHEN t.order_status = 3

      THEN t.order_date

    END

  ) last_return_time,--最近一次退貨時間

  SUM(

    CASE

      WHEN t2.order_addr = 1

      THEN 1

    END

  ) school_order_cnt,--學校下單總數

  SUM(

    CASE

      WHEN t2.order_addr = 2

      THEN 1

    END

  ) company_order_cnt,--單位下單總數

  SUM(

    CASE

      WHEN t2.order_addr = 3

      THEN 1

    END

  ) home_order_cnt,--家裏下單總數

  SUM(

    CASE

      WHEN t.order_hour >= 8

      AND t.order_hour <= 11

      THEN 1

    END

  ) forenoon_order_cnt,--上午下單總數

  SUM(

    CASE

      WHEN t.order_hour >= 12

      AND t.order_hour <= 18

      THEN 1

    END

  ) afternoon_order_cnt,--下午下單總數

  SUM(

    CASE

      WHEN t.order_hour >= 19

      AND t.order_hour <= 22

      THEN 1

    END

  ) night_order_cnt,--晚上下單總數

  SUM(

    CASE

      WHEN t.order_hour >= 23

      AND t.order_hour <= 7

      THEN 1

    END

  ) morning_order_cnt --凌晨下單總數

FROM

  (SELECT

    a.*,

    (

      CASE

        WHEN order_date >= DATE_SUB('2017-01-01', 29)

        AND order_date <= '2017-01-01'

        THEN 1

      END

    ) dat_30,

    (

      CASE

        WHEN order_date >= DATE_SUB('2017-01-01', 59)

        AND order_date <= '2017-01-01'

        THEN 1

      END

    ) dat_60,

    (

      CASE

        WHEN order_date >= DATE_SUB('2017-01-01', 89)

        AND order_date <= '2017-01-01'

        THEN 1

      END

    ) dat_90,

    (

      CASE

        WHEN a.order_status IN (3, 4)

        THEN 1

        ELSE 0

      END

    ) order_flag,--退貨與拒收標示

    HOUR(order_date) order_hour

  FROM

    gdm.itcast_gdm_order a

  WHERE dt = '2017-01-01') t

  LEFT JOIN

    (SELECT

      order_id,

      goods_amount

    FROM

      fdm.itcast_fdm_order_goods) t1

    ON (t.order_id = t1.order_id)

  LEFT JOIN

    (SELECT

      user_id,

      order_addr

    FROM

      gdm.itcast_user_order_addr_model) t2

    ON (t.user_id = t2.user_id)

GROUP BY t.user_id ;

 

 

---購物車臨時模型表--臨時表02

DROP TABLE IF EXISTS gdm.itcast_gdm_user_consume_order_temp_02;

CREATE TABLE gdm.itcast_gdm_user_consume_order_temp_02 AS

SELECT

  user_id,

  COUNT(1) month1_cart_cnt,--最近30天購物車次數

  SUM(goods_num) month1_cart_goods_cnt,--最近30天購物車商品件數

  SUM(

    CASE

      WHEN sumbit_time <> ''

      THEN goods_num

      ELSE 0

    END

  ) month1_cart_submit_cnt,--最近30天購物車提交商品件數

 '' month1_cart_rate,--最近30天購物車成功率

 SUM(

    CASE

      WHEN cancle_time <> ''

      THEN goods_num

      ELSE 0

    END

  ) month1_cart_cancle_cnt  --最近30天購物車放棄件數

FROM

  fdm.itcast_fdm_order_cart

WHERE dt = '2017-01-01'

  AND to_date (add_time) >= DATE_SUB('2017-01-01', 29)

  AND to_date (add_time) <= '2017-01-01'

GROUP BY user_id ;

 

 

---購物車臨時模型表---經常使用地址和經常使用支付方式-臨時表03

drop table if exists gdm.itcast_gdm_user_consume_order_temp_03;

create table gdm.gdm_user_consume_order_temp_03 as

select

  t.user_id,

  t.con,

  t.type,

  t.cnt

from

  (select

    b.user_id,

    b.con,

    b.type,

    b.cnt,

    row_number() over(distribute by b.user_id,

    b.type sort by b.cnt,

    b.type desc) rn

from

  (select

    a.user_id,concat(

      coalesce(area_name, ''),

      coalesce(address, '')) con,

    'address' type,

    count(1) cnt

  from

    gdm.itcast_gdm_order a where dt = '2017-01-01'

  group by a.user_id,

    concat(

      coalesce(area_name, ''),

      coalesce(address, '')

    )

    union

    all

    select

      a.user_id,

      a.pay_type con,

      'pay_type' type,

      count(1) cnt

    from

      gdm.itcast_gdm_order a

    where dt = '2017-01-01'

    group by a.user_id,

      a.pay_type) b) t

  where t.rn = 1 ;

 

 

 

--購物車表和訂單表整合

drop table if exists gdm.itcast_gdm_user_consume_order_temp_100;

create table gdm.gdm_user_consume_order_temp_100 as

select

  a.user_id

from

  (select

    user_id

  from

    gdm.itcast_gdm_user_consume_order_temp_01

  union

  all

  select

    user_id

  from

    gdm.itcast_gdm_user_consume_order_temp_02) a

group by a.user_id ;

 

 

--生成最終的客戶消費訂單表

INSERT overwrite TABLE gdm.itcast_gdm_user_consume_order PARTITION (dt = '2017-01-01')

SELECT

  t.user_id,  --客戶ID

  t1.first_order_time,  --經常使用地址和經常使用支付方式次消費時間

  t1.last_order_time,  --最近一次消費時間

  t1.first_order_ago,  --首單距今時間

  t1.last_order_ago,  --尾單距今時間

  t1.month1_hg_order_cnt,--30天購買次數(不含退拒)

  t1.month1_hg_order_amt,--30天購買金額(不含退拒)

  t1.month2_hg_order_cnt,--60天購買次數(不含退拒)

  t1.month2_hg_order_amt,--60天購買金額(不含退拒)

  t1.month3_hg_order_cnt,--90天購買次數(不含退拒)

  t1.month3_hg_order_amt,--90天購買金額(不含退拒)

  t1.month1_order_cnt,    --30天購買次數(含退拒)

  t1.month1_order_amt,    --30天購買金額(含退拒)

  t1.month2_order_cnt,    --60天購買次數(含退拒)

  t1.month2_order_amt,    --60天購買金額(含退拒)

  t1.month3_order_cnt,    --90天購買次數(含退拒)

  t1.month3_order_amt,    --90天購買金額(含退拒)

  t1.max_order_amt,       --最大消費金額

  t1.min_order_amt,       --最小消費金額

  t1.total_order_cnt,       --累計消費次數(不含退拒)

  t1.total_order_amt,      --累計消費金額(不含退拒)

  t1.user_avg_amt,        --客單價(含退拒)

  (

    CASE

      WHEN t1.month3_order_cnt <> 0

      THEN t1.month3_order_amt / t1.month3_order_cnt

      ELSE 0

    END

  ) month3_user_avg_amt,    --90天的客單價(含退拒)

  t3.common_address,        --經常使用收貨地址

  t3.common_paytype,        --經常使用支付方式

  t2.month1_cart_cnt,         --30天購物車的次數

  t2.month1_cart_goods_cnt,   --30天購物車商品件數

  t2.month1_cart_submit_cnt,  --30天購物車提交商品件數

  (

    CASE

      WHEN t1.month1_order_cnt <> 0

      THEN t2.month1_cart_submit_cnt / t2.month1_cart_goods_cnt

      ELSE 0

    END

  ) month1_cart_rate,  --30天購物車成功率

  t2.month1_cart_cancle_cnt,  --30天購物車放棄件數

  t1.return_cnt,                  --退貨商品數量

  t1.return_amt,                  --退貨商品金額

  t1.reject_cnt,                 --拒收商品數量

  t1.reject_amt,                 --拒收商品金額

  t1.last_return_time,            --最近一次退貨時間

  t1.school_order_cnt,           --學校下單總數

  t1.company_order_cnt,         --單位下單總數

  t1.home_order_cnt,            --家裏下單總數

  t1.forenoon_order_cnt,         --上午下單總數

  t1.afternoon_order_cnt,        --下午下單總數

  t1.night_order_cnt,            --晚上下單總數

  t1.morning_order_cnt,          --凌晨下單總數

  FROM_UNIXTIME(UNIX_TIMESTAMP())  dw_date

FROM                             

  gdm.itcast_gdm_user_consume_order_temp_100 t

  LEFT JOIN gdm.itcast_gdm_user_consume_order_temp_01 t1

    ON (t.user_id = t1.user_id)

  LEFT JOIN gdm.itcast_gdm_user_consume_order_temp_02 t2

    ON (t.user_id = t2.user_id)

  LEFT JOIN

    (SELECT

      user_id,

      MAX(

        CASE

          WHEN type = 'address'

          THEN con

        END

      ) common_address,

      MAX(

        CASE

          WHEN type = 'pay_type'

          THEN con

        END

      ) common_paytype

    FROM

      gdm.itcast_gdm_user_consume_order_temp_03

    GROUP BY user_id) t3

    ON (t.user_id = t3.user_id);

 

8.5 用戶畫像開發--客戶購買類目表

--用戶畫像 客戶購買類目表

create database if not exists gdm;

create external table if not exists gdm.itcast_gdm_user_buy_category(

user_id bigint , --客戶ID

first_cat bigint , --一級分類ID

first_cat_name       string , --一級分類名稱

second_cat bigint , --二分類ID

second_cat_name string , --二級分類名稱

third_cat bigint , --三級分類ID

third_cat_name string , --三級分類名稱

month1_cat_cnt bigint , --30天購物類目次數

month1_cat_amt string , --30天購物類目金額

month3_cat_cnt bigint , --90天購物類目次數

month3_cat_amt string , --90天購物類目金額

month6_cat_cnt bigint , --180天購物類目次數

month6_cat_amt string , --180天購物類目金額

total_cat_cnt       bigint ,--累計購物類目次數

total_cat_amt       string , --累計購物類目次數

month1_cart_cat_cnt       bigint , --30天購物車類目次數

month3_cart_cat_cnt       bigint , --90天購物車類目次數

month6_cart_cat_cnt       bigint , --180天購物車類目次數

total_cart_cat_cnt          bigint , --累計購物車類目次數

last_cat_time       timestamp , --最後一次購買類目時間

last_cat_ago       bigint , --最後一次購買類目距今天數

dw_date             timestamp

)partitioned by (dt string);

 

 

 

 

 

 

--計算訂單中客戶購買狀況

drop table if exists gdm.itcast_gdm_user_buy_category_temp;

CREATE TABLE gdm.itcast_gdm_user_buy_category_temp AS

SELECT

  a.user_id,

  '' first_category_id,

  '' first_category_name,

  '' second_category_id,

  '' second_catery_name,

  b.third_cart  third_category_id,

  b.third_cat_name  third_category_name,

  SUM(

    CASE

      WHEN a.dat_30 = 1

      THEN b.goods_amount

    END

  ) month1_category_cnt,

  SUM(

    CASE

      WHEN a.dat_30 = 1

      THEN COALESCE(b.curr_price,0)* COALESCE(b.goods_amount,0)

    END

  ) month1_category_amt,

  SUM(

    CASE

      WHEN a.dat_90 = 1

      THEN b.goods_amount

    END

  ) month3_category_cnt,

  SUM(

    CASE

      WHEN a.dat_90 = 1

      THEN COALESCE(b.curr_price, 0)* COALESCE(b.goods_amount, 0)

    END

  ) month3_category_amt,

  SUM(

    CASE

      WHEN a.dat_180 = 1

      THEN b.goods_amount

    END

  ) month6_category_cnt,

  SUM(

    CASE

      WHEN a.dat_180 = 1

      THEN COALESCE(b.curr_price, 0) * COALESCE(b.goods_amount, 0)

    END

  ) month6_category_amt,

  SUM(b.goods_amount) total_category_cnt,

  SUM(

    COALESCE(b.curr_price, 0) * COALESCE(b.goods_amount, 0)

  ) total_category_amt,

  MAX(a.order_date) last_category_time,

  DATEDIFF(MIN(a.order_date), '2017-01-01') last_category_ago,

  FROM_UNIXTIME(UNIX_TIMESTAMP()) dw_date

FROM

  (SELECT

    a.*,

    (

      CASE

        WHEN order_date >= DATE_SUB('2017-01-01', 29)

        AND order_date <= '2017-01-01'

        THEN

        1

      END

    ) dat_30,

    (

      CASE

        WHEN order_date >= DATE_SUB('2017-01-01', 89)

        AND order_date <= '2017-01-01'

        THEN

        1

      END

    ) dat_90,

    (

      CASE

        WHEN order_date >= DATE_SUB('2017-01-01', 179)

        AND order_date <= '2017-01-01'

        THEN

        1

      END

    ) dat_180

  FROM

    fdm.itcast_fdm_order a

  WHERE dt = '2017-01-01') a

  JOIN

    (SELECT

      *

    FROM

      fdm.itcast_fdm_order_goods

    WHERE dt = '2017-01-01') b

    ON (a.user_id = b.user_id)

GROUP BY a.user_id,b.third_cart,b.third_cat_name;

 

--購物車中類目狀況

--購物車中類目狀況

drop table if exists gdm.itcast_gdm_user_cart_category_temp;

create table gdm.itcast_gdm_user_cart_category_temp as

select

  a.user_id,

  b.third_cart,

  sum(

    case

      when to_date (add_time) >= date_sub('2017-01-01', 29)

      and to_date (add_time) <= '2017-01-01'

      then 1

    end

  ) month1_category_cnt,

  sum(

    case

      when to_date (add_time) >= date_sub('2017-01-01', 59)

      and to_date (add_time) <= '2017-01-01'

      then 1

    end

  ) month3_category_cnt,

  sum(

    case

      when to_date (add_time) >= date_sub('2017-01-01', 179)

      and to_date (add_time) <= '2017-01-01'

      then 1

    end

  ) month6_category_cnt,

  count(1) total_category_cnt

from

  (select

    *

  from

    fdm.itcast_fdm_order_cart

  where dt = '2017-01-01'

    and to_date (add_time) >= date_sub('2017-01-01', 179)

    and to_date (add_time) <= '2017-01-01') a

  left join

    (select

      goods_id,

      third_cart

    from

      fdm.itcast_fdm_order_goods

    where dt = '2017-01-01'

    group by goods_id,

      third_cart) b

    on (a.goods_id = b.goods_id)

group by user_id,

  b.third_cart ;

 

 

--整合

drop table if exists gdm.itcast_gdm_user_category_total;

create table gdm.itcast_gdm_user_category_total as

select

  a.user_id,

  b.first_category_id,

  b.first_category_name,

  b.second_category_id,

  b.second_catery_name,

  a.third_category_id,

  b.third_category_name

from

  (select

    user_id,

    third_category_id

  from

    gdm.itcast_gdm_user_buy_category_temp

  union

  all

  select

    user_id,

    third_cart

  from

    gdm.itcast_gdm_user_cart_category_temp) a

  left join gdm.gdm_category_code b

    on (

      a.third_category_id = b.third_category_id

    )

group by a.user_id,

  b.first_category_id,

  b.first_category_name,

  b.second_category_id,

  b.second_catery_name,

  a.third_category_id,

  b.third_category_name ;

 

--生成最終客戶購買類目表

INSERT overwrite TABLE gdm.itcast_gdm_user_buy_category PARTITION (dt = '2017-01-01')

SELECT

  t.user_id,

  t.first_category_id,

  t.first_category_name,

  t.second_category_id,

  t.second_catery_name,

  t.third_category_id,

  t.third_category_name,

  t1.month1_category_cnt,

  t1.month1_category_amt,

  t1.month3_category_cnt,

  t1.month3_category_amt,

  t1.month6_category_cnt,

  t1.month6_category_amt,

  t1.total_category_cnt,

  t1.total_category_amt,

  t2.month1_category_cnt,

  t2.month3_category_cnt,

  t2.month6_category_cnt,

  t2.total_category_cnt,

  t1.last_category_time,

  t1.last_category_ago,

  FROM_UNIXTIME(UNIX_TIMESTAMP()) dw_date

FROM

  gdm.itcast_gdm_user_category_total t

  LEFT JOIN gdm.itcast_gdm_user_buy_category_temp t1

    ON (

      t.user_id = t1.user_id

      AND t.third_category_id = t1.third_category_id

    )

  LEFT JOIN gdm.itcast_gdm_user_cart_category_temp t2

    ON (t.user_id = t2.user_id

    AND t.third_category_id = t2.third_cart) ;

8.6 用戶畫像開發--客戶訪問信息表

--用戶畫像-客戶訪問信息表

drop table if exists gdm.itcast_gdm_user_visit;

create external table gdm.itcast_gdm_user_visit(

user_id string ,--客戶ID

latest_pc_visit_date string ,--最近一次PC端訪問日期

latest_app_visit_date string ,--最近一次APP端訪問日期

latest_pc_visit_session string ,--最近一次PC端訪問的session

latest_pc_cookies string ,--最近一次PC端訪問的cookies

latest_pc_pv string ,--最近一次PC端訪問的PV

latest_pc_browser_name string ,--最近一次PC端訪問使用的遊覽器

latest_pc_visit_os string ,--最近一次PC端訪問使用的操做系統

latest_app_name string ,--最近一次APP端訪問app名稱

latest_app_visit_os string ,--最近一次APP端訪問使用的操做系統

latest_visit_ip string ,--最近一次訪問IP(不分APPPC)

latest_city string ,--最近一次訪問城市(不分APPPC)

latest_province string ,--最近一次訪問省份(不分APPPC)

first_pc_visit_date string ,--第一次PC端訪問日期

first_app_visit_date string ,--第一次APP端訪問日期

first_pc_visit_session string ,--第一次PC端訪問的session

first_pc_cookies string ,--第一次PC端訪問的cookies

first_pc_pv string ,--第一次PC端訪問的PV

first_pc_browser_name string ,--第一次PC端訪問使用的遊覽器

first_pc_visit_os string ,--第一次PC端訪問使用的操做系統

first_app_name string ,--第一次APP端訪問app名稱

first_app_visit_os string ,--第一次APP端訪問使用的操做系統

first_visit_ip string ,--第一次訪問IP(不分APPPC)

first_city string ,--第一次訪問城市(不分APPPC)

first_province string ,--第一次訪問省份(不分APPPC)

day7_app_cnt bigint ,--7APP端訪問次數

day15_app_cnt bigint ,--15APP端訪問次數

month1_app_cnt bigint ,--30APP端訪問次數

month2_app_cnt bigint ,--60APP端訪問次數

month3_app_cnt bigint ,--90APP端訪問次數

 

day7_pc_cnt bigint ,--7PC端訪問次數

day15_pc_cnt bigint ,--15PC端訪問次數

month1_pc_cnt bigint ,--30PC端訪問次數

month2_pc_cnt bigint ,--60PC端訪問次數

month3_pc_cnt bigint ,--90PC端訪問次數

 

month1_pc_days bigint ,--30PC端訪問天數

month1_pc_buy_cnt bigint ,--30PC端訪問購買次數

month1_pc_pv bigint ,--30PC端訪問PV

month1_pc_avg_pv bigint ,--30PC端訪問平均PV

month1_pc_diff_ip_cnt bigint ,--30PC端訪問不一樣ip

month1_pc_common_ip bigint ,--30PC端訪問最經常使用ip

month1_pc_diff_cookie_cnt bigint ,--30PC端訪問不一樣的cookie

month1_pc_common_cookie bigint ,--30PC端訪問最經常使用的cookie

month1_pc_common_browser_name     bigint ,--30PC端訪問最經常使用遊覽器

month1_pc_common_os bigint ,--30PC端訪問最經常使用的操做系統

 

month1_hour025_cnt        bigint ,--300-5點訪問次數(不分PCAPP)

month1_hour627_cnt bigint ,--306-7點訪問次數(不分PCAPP)

month1_hour829_cnt bigint ,--308-9點訪問次數(不分PCAPP)

month1_hour10212_cnt bigint ,--3010-12點訪問次數(不分PCAPP)

month1_hour13214_cnt bigint ,--3013-14點訪問次數(不分PCAPP)

month1_hour15217_cnt bigint ,--3015-17點訪問次數(不分PCAPP)

month1_hour18219_cnt bigint ,--3018-19點訪問次數(不分PCAPP)

month1_hour20221_cnt bigint ,--3020-21點訪問次數(不分PCAPP)

month1_hour22223_cnt bigint ,--3022-23點訪問次數(不分PCAPP)

dw_date        timestamp

) partitioned by (dt string);

 

------用戶上網軌跡表BDM--PC

DROP TABLE IF EXISTS bdm.itcast_bdm_user_pc_click_log ;

CREATE external TABLE bdm.itcast_bdm_user_pc_click_log (

  session_id STRING,  --sessionID

  cookie_id STRING,   --cookieID

  visit_time STRING,  --訪問時間

  user_id STRING,     --用戶ID

  visit_url STRING,   --訪問的URL

  visit_os STRING,    --操做系統

  browser_name STRING,--遊覽器名稱

  visit_ip STRING,    --訪問ip

  province STRING,    --省份

  city STRING,        --城市

  page_id STRING,     --頁面ID

  goods_id STRING,    --商品ID

  shop_id STRING      --商店ID

) partitioned by (dt string)

row format delimited fields terminated by ','

lines terminated by '\n';

alter table bdm.itcast_bdm_user_pc_click_log add partition (dt='2017-01-01') location '/business/itcast_bdm_user_pc_click_log/2017-01-01';

hdfs dfs -put itcast_bdm_user_pc_click_log.txt /business/itcast_bdm_user_pc_click_log/2017-01-01

 

------用戶上網軌跡表FDM--PC

DROP TABLE IF EXISTS fdm.itcast_fdm_user_pc_pageview ;

CREATE external TABLE fdm.itcast_fdm_user_pc_pageview (

  session_id STRING,  --sessionID

  cookie_id STRING,   --cookieID

  user_id STRING,     --用戶ID

  in_time STRING,     --訪問進入時間

  out_time STRING,    --訪問離開時間

  stay_time STRING,   --訪問停留時間

  pv BIGINT,          --PV

  visit_os STRING,    --操做系統

  browser_name STRING,--遊覽器名稱

  visit_ip STRING,     --訪問ip

  province STRING,    --省份

  city STRING         --城市

) partitioned BY (dt STRING);

-------------加載數據---

INSERT overwrite TABLE fdm.itcast_fdm_user_pc_pageview PARTITION (dt = '2017-01-01')

SELECT

  t.session_id,

  t.cookie_id,

  t.user_id,

  MIN(visit_time) in_time,

  MAX(visit_time) out_time,

  (

    case

      WHEN MIN(visit_time) = MAX(visit_time)

      then 5

      else unix_timestamp(MAX(visit_time)) - unix_timestamp(MIN(visit_time))

    end

  ) stay_time,

  COUNT(1) pv,

  t.visit_os,

  t.browser_name,

  t.visit_ip,

  t.province,

  t.city

FROM

  bdm.itcast_bdm_user_pc_click_log t

WHERE dt = '2017-01-01'

GROUP BY t.session_id,

  t.cookie_id,

  t.user_id,

  t.visit_os,

  t.browser_name,

  t.visit_ip,

  t.province,

  t.city ;

 

 

 

------用戶上網軌跡表BDM--APP

drop table if exists bdm.itcast_bdm_user_app_click_log;

create external table bdm.itcast_bdm_user_app_click_log(

  user_id string,        --用戶ID

  log_time string,       --訪問時間

  phone_id string,       --手機ID,惟一標識一臺設備

  visit_os string,        --操做系統 androidioswp

  os_version string,     --操做系統版本

  app_name string,     --APP的名稱

  app_version string,    --APP的版本

  device_token string,   --PUSH碼,消息推送的

  visit_ip string,        --訪問ip

  province string,       --省份

  city string            --城市

) partitioned by (dt string)

row format delimited fields terminated by ','

lines terminated by '\n';

alter table bdm.itcast_bdm_user_app_click_log add partition (dt='2017-01-01') location '/business/itcast_bdm_user_app_click_log/2017-01-01';

hdfs dfs -put itcast_bdm_user_app_click_log.txt /business/itcast_bdm_user_app_click_log/2017-01-01

--Push指運營人員經過本身的產品或第三方工具對用戶移動設備進行的主動消息推送。用戶能夠在移動設備鎖定屏幕和通知欄看到push消息通知,通知欄點擊可喚起APP並去往相應頁面。咱們平時在鎖屏上看到的微信消息等等都屬於APP消息推送行列

 

 

------用戶上網軌跡表FDM--app

DROP TABLE IF EXISTS fdm.itcast_fdm_user_app_pageview ;

CREATE external TABLE fdm.itcast_fdm_user_app_pageview (

  user_id string,        --用戶ID

  log_time string,       --訪問時間

  log_hour string,     --訪問時間的小時數

  phone_id string,       --手機ID,惟一標識一臺設備

  visit_os string,       --操做系統 androidioswp

  os_version string,     --操做系統版本

  app_name string,      --APP的名稱

  app_version string,    --APP的版本

  device_token string,   --PUSH碼,消息推送的

  visit_ip string,       --訪問ip

  province string,       --省份

  city string            --城市

) partitioned BY (dt STRING);

 

-------------加載數據---

INSERT overwrite TABLE fdm.itcast_fdm_user_app_pageview PARTITION (dt = '2017-01-01')

SELECT

  t.user_id,

  t.log_time,

  HOUR(t.log_time) log_hour,

  t.phone_id,

  t.visit_os,

  t.os_version,

  t.app_name,

  t.app_version,

  t.device_token,

  t.visit_ip,

  t.province,

  t.city

FROM

  bdm.itcast_bdm_user_app_click_log t

WHERE dt = '2017-01-01' ;

 

 

--生成臨時表-統計出用戶近30天訪問的ip/cookie/遊覽器信息

drop table if exists gdm.itcast_gdm_user_visit_temp_01;

create table gdm.itcast_gdm_user_visit_temp_01 as

select

  t.user_id,

  t.type,

  t.con,

  t.cnt,

  t.rn

from

  (select

    b.user_id,

    b.con,

    b.type,

    b.cnt,

    row_number () over (

      distribute by b.user_id,

      b.type sort by b.cnt desc

    ) rn

  from

    (select

      a.user_id,

      a.visit_ip con,

      'visit_ip' type,

      count(1) cnt

    from

      fdm.itcast_fdm_user_pc_pageview a

    where dt >= date_add('2017-01-01', -29)

    group by a.user_id,

      a.visit_ip

    union

    all

    select

      a.user_id,

      a.cookie_id con,

      'cookie_id' type,

      count(1) cnt

    from

      fdm.itcast_fdm_user_pc_pageview a

    where dt >= date_add('2017-01-01', -29)

    group by a.user_id,

      a.cookie_id

    union

    all

    select

      a.user_id,

      a.browser_name con,

      'browser_name' type,

      count(1) cnt

    from

      fdm.itcast_fdm_user_pc_pageview a

    where dt >= date_add('2017-01-01', -29)

    group by a.user_id,

      a.browser_name

    union

    all

    select

      a.user_id,

      a.visit_os con,

      'visit_os' type,

      count(1) cnt

    from

      fdm.itcast_fdm_user_pc_pageview a

    where dt >= date_add('2017-01-01', -29)

    group by a.user_id,

      a.visit_os) b) t ;

 

-------生成客戶訪問模型表

-------------加載數據---

INSERT overwrite TABLE fdm.itcast_fdm_user_app_pageview PARTITION (dt = '2017-01-01')

SELECT

  t.user_id,

  t.log_time,

  HOUR(t.log_time) log_hour,

  t.phone_id,

  t.visit_os,

  t.os_version,

  t.app_name,

  t.app_version,

  t.device_token,

  t.visit_ip,

  t.province,

  t.city

FROM

  bdm.itcast_bdm_user_app_click_log t

WHERE dt = '2017-01-01' ;

 

 

 

 

------30PC端訪問最經常使用的指標

drop table if exists gdm.itcast_gdm_user_visit_temp_01;

create table gdm.itcast_gdm_user_visit_temp_01 as

select

  t.user_id,

  t.type,

  t.con,

  t.cnt,

  t.rn

from

  (select

    b.user_id,

    b.con,

    b.type,

    b.cnt,

    row_number () over (

      distribute by b.user_id,

      b.type sort by b.cnt desc

    ) rn

  from

    (select

      a.user_id,

      a.visit_ip con,

      'visit_ip' type,

      count(1) cnt

    from

      fdm.itcast_fdm_user_pc_pageview a

    where dt >= date_add('2017-01-01', -29)

    group by a.user_id,

      a.visit_ip

    union

    all

    select

      a.user_id,

      a.cookie_id con,

      'cookie_id' type,

      count(1) cnt

    from

      fdm.itcast_fdm_user_pc_pageview a

    where dt >= date_add('2017-01-01', -29)

    group by a.user_id,

      a.cookie_id

    union

    all

    select

      a.user_id,

      a.browser_name con,

      'browser_name' type,

      count(1) cnt

    from

      fdm.itcast_fdm_user_pc_pageview a

    where dt >= date_add('2017-01-01', -29)

    group by a.user_id,

      a.browser_name

    union

    all

    select

      a.user_id,

      a.visit_os con,

      'visit_os' type,

      count(1) cnt

    from

      fdm.itcast_fdm_user_pc_pageview a

    where dt >= date_add('2017-01-01', -29)

    group by a.user_id,

      a.visit_os) b) t ;

 

 

-------生成客戶訪問模型表

 

INSERT overwrite TABLE gdm.itcast_gdm_user_visit PARTITION (dt = '2017-01-01')

SELECT

  t.user_id,--客戶ID

  MAX(

    CASE

      WHEN pc.rn_desc = 1

      THEN pc.in_time

    END

  ) latest_pc_visit_date,--最近一次PC端訪問日期

  MAX(

    CASE

      WHEN app.rn_desc = 1

      THEN app.log_time

    END

  ) latest_app_visit_date,--最近一次APP端訪問日期

  MAX(

    CASE

      WHEN pc.rn_desc = 1

      THEN pc.session_id

    END

  ) latest_pc_visit_session,--最近一次PC端訪問的session

  MAX(

    CASE

      WHEN pc.rn_desc = 1

      THEN pc.cookie_id

    END

  ) latest_pc_cookies,--最近一次PC端訪問的cookies

  MAX(

    CASE

      WHEN pc.rn_desc = 1

      THEN pc.pv

    END) latest_pc_pv,--最近一次PC端訪問的PV

  MAX(

    CASE

      WHEN pc.rn_desc = 1

      THEN pc.browser_name

    END

  ) latest_pc_browser_name,--最近一次PC端訪問使用的遊覽器

  MAX(

    CASE

      WHEN pc.rn_desc = 1

      THEN pc.visit_os

    END

  ) latest_pc_visit_os,--最近一次PC端訪問使用的操做系統

  MAX(

    CASE

      WHEN app.rn_desc = 1

      THEN app.app_name

    END

  ) latest_app_name,--最近一次APP端訪問app名稱

  MAX(

    CASE

      WHEN app.rn_desc = 1

      THEN app.visit_os

    END

  ) latest_app_visit_os,--最近一次APP端訪問使用的操做系統

  MAX(

    CASE

      WHEN pc.rn_desc = 1

      AND app.rn_desc = 1

      AND pc.in_time >= app.log_time

      THEN pc.visit_ip

      WHEN pc.rn_desc = 1

      AND app.rn_desc = 1

      AND pc.in_time < app.log_time

      THEN app.visit_ip  

    END

  ) latest_visit_ip,--最近一次訪問IP(不分APPPC)

  MAX(

    CASE

      WHEN pc.rn_desc = 1

      AND app.rn_desc = 1

      AND pc.in_time >= app.log_time

      THEN pc.city

      WHEN pc.rn_desc = 1

      AND app.rn_desc = 1

      AND pc.in_time < app.log_time

      THEN app.city

    END

  ) latest_city,--最近一次訪問城市(不分APPPC)

  MAX(

    CASE

      WHEN pc.rn_desc = 1

      AND app.rn_desc = 1

      AND pc.in_time >= app.log_time

      THEN pc.province

      WHEN pc.rn_desc = 1

      AND app.rn_desc = 1

      AND pc.in_time < app.log_time

      THEN app.province

    END

  ) latest_province,--最近一次訪問省份(不分APPPC)

  MAX(

    CASE

      WHEN pc.rn_asc = 1

      THEN pc.in_time

    END

  ) first_pc_visit_date,--第一次PC端訪問日期

  MAX(

    CASE

      WHEN app.rn_asc = 1

      THEN app.log_time

    END

  ) first_app_visit_date,--第一次APP端訪問日期

  MAX(

    CASE

      WHEN pc.rn_asc = 1

      THEN pc.session_id

    END

  ) first_pc_visit_session,--第一次PC端訪問的session

  MAX(

    CASE

      WHEN pc.rn_asc = 1

      THEN pc.cookie_id

    END

  ) first_pc_cookies,--第一次PC端訪問的cookies

  MAX(

    CASE

      WHEN pc.rn_asc = 1

      THEN pc.pv

    END) first_pc_pv,--第一次PC端訪問的PV

  MAX(

    CASE

      WHEN pc.rn_asc = 1

      THEN pc.browser_name

    END

  ) first_pc_browser_name,--第一次PC端訪問使用的遊覽器

  MAX(

    CASE

      WHEN pc.rn_asc = 1

      THEN pc.visit_os

    END

  ) first_pc_visit_os,--第一次PC端訪問使用的操做系統

  MAX(

    CASE

      WHEN app.rn_asc = 1

      THEN app.app_name

    END

  ) first_app_name,--第一次APP端訪問app名稱

  MAX(

    CASE

      WHEN app.rn_asc = 1

      THEN app.visit_os

    END

  ) first_app_visit_os,--第一次APP端訪問使用的操做系統

  MAX(

    CASE

      WHEN pc.rn_asc = 1

      AND app.rn_asc = 1

      AND pc.in_time <= app.log_time

      THEN pc.visit_ip

      WHEN pc.rn_asc = 1

      AND app.rn_asc = 1

      AND pc.in_time > app.log_time

      THEN app.visit_ip

    END

  ) first_visit_ip,--第一次訪問IP(不分APPPC)

  MAX(

    CASE

      WHEN pc.rn_asc = 1

      AND app.rn_asc = 1

      AND pc.in_time <= app.log_time

      THEN pc.city

      WHEN pc.rn_asc = 1

      AND app.rn_asc = 1

      AND pc.in_time > app.log_time

      THEN app.city

    END

  ) first_city,--第一次訪問城市(不分APPPC)

  MAX(

    CASE

      WHEN pc.rn_asc = 1

      AND app.rn_asc = 1

      AND pc.in_time <= app.log_time

      THEN pc.province

      WHEN pc.rn_asc = 1

      AND app.rn_asc = 1

      AND pc.in_time > app.log_time

      THEN app.province

    END

  ) first_province,--第一次訪問省份(不分APPPC)

  SUM(

    CASE

      WHEN app.dat_7 = 1

      THEN 1

    END) day7_app_cnt,--7APP端訪問次數

  SUM(

    CASE

      WHEN app.dat_15 = 1

      THEN 1

    END) day15_app_cnt,--15APP端訪問次數

  SUM(

    CASE

      WHEN app.dat_30 = 1

      THEN 1

    END) month1_app_cnt,--30APP端訪問次數

  SUM(

    CASE

      WHEN app.dat_60 = 1

      THEN 1

    END) month2_app_cnt,--60APP端訪問次數

  SUM(

    CASE

      WHEN app.dat_90 = 1

      THEN 1

    END) month3_app_cnt,--90APP端訪問次數

  COUNT(

    CASE

      WHEN pc.dat_7 = 1

      THEN pc.session_id

    END

  ) day7_pc_cnt,--7PC端訪問次數

  COUNT(

    CASE

      WHEN pc.dat_15 = 1

      THEN pc.session_id

    END

  ) day15_pc_cnt,--15PC端訪問次數

  COUNT(

    CASE

      WHEN pc.dat_30 = 1

      THEN pc.session_id

    END

  ) month1_pc_cnt,--30PC端訪問次數

  COUNT(

    CASE

      WHEN pc.dat_60 = 1

      THEN pc.session_id

    END

  ) month2_pc_cnt,--60PC端訪問次數

  COUNT(

    CASE

      WHEN pc.dat_90 = 1

      THEN pc.session_id

    END

  ) month3_pc_cnt,--90PC端訪問次數

  COUNT(DISTINCT substr(pc.in_time,0,10)) month1_pc_days,--30PC端訪問天數

  SUM(

    CASE

      WHEN pc.dat_30 = 1

      THEN pc.pv

    END) month1_pc_pv,--30PC端訪問PV

  SUM(

    CASE

      WHEN pc.dat_30 = 1

      THEN pc.pv

    END) / COUNT(DISTINCT substr(pc.in_time,0,10)) month1_pc_avg_pv,--30PC端訪問平均PV

  MAX(b.month1_pc_diff_ip_cnt),--30PC端訪問不一樣ip

  MAX(b.month1_pc_diff_cookie_cnt),--30PC端訪問不一樣的cookie

  MAX(b.month1_pc_common_ip),--30PC端訪問最經常使用ip

  MAX(b.month1_pc_common_cookie),--30PC端訪問最經常使用的cookie

  MAX(b.month1_pc_common_browser_name),--30PC端訪問最經常使用遊覽器

  MAX(b.month1_pc_common_os),--30PC端訪問最經常使用的操做系統

  COUNT(

    CASE

      WHEN pc.visit_hour >= 0

      AND pc.visit_hour <= 5

      THEN pc.session_id

    END

  ) month1_hour025_cnt,--30PC0-5點訪問次數

  COUNT(

    CASE

      WHEN pc.visit_hour >= 6

      AND pc.visit_hour <= 7

      THEN pc.session_id

    END

  ) month1_hour627_cnt,--30PC6-7點訪問次數

  COUNT(

    CASE

      WHEN pc.visit_hour >= 8

      AND pc.visit_hour <= 9

      THEN pc.session_id

    END

  ) month1_hour829_cnt,--30PC8-9點訪問次數

  COUNT(

    CASE

      WHEN pc.visit_hour >= 10

      AND pc.visit_hour <= 12

      THEN pc.session_id

    END

  ) month1_hour10212_cnt,--30PC10-12點訪問次數

  COUNT(

    CASE

      WHEN pc.visit_hour >= 13

      AND pc.visit_hour <= 14

      THEN pc.session_id

    END

  ) month1_hour13214_cnt,--30PC13-14點訪問次數

  COUNT(

    CASE

      WHEN pc.visit_hour >= 15

      AND pc.visit_hour <= 17

      THEN pc.session_id

    END

  ) month1_hour15217_cnt,--30PC15-17點訪問次數

  COUNT(

    CASE

      WHEN pc.visit_hour >= 18

      AND pc.visit_hour <= 19

      THEN pc.session_id

    END

  ) month1_hour18219_cnt,--30PC18-19點訪問次數

  COUNT(

    CASE

      WHEN pc.visit_hour >= 20

      AND pc.visit_hour <= 21

      THEN pc.session_id

    END

  ) month1_hour20221_cnt,--30PC20-21點訪問次數

  COUNT(

    CASE

      WHEN pc.visit_hour >= 22

      AND pc.visit_hour <= 23

      THEN pc.session_id

    END

  ) month1_hour22223_cnt,--30PC22-23點訪問次數

  FROM_UNIXTIME(UNIX_TIMESTAMP()) dw_date

FROM

  (SELECT

    user_id

  FROM

    fdm.itcast_fdm_user_wide

  WHERE dt = '2017-01-01') t

  LEFT JOIN

    (SELECT

      a.*,

      (

        CASE

          WHEN in_time >= DATE_SUB('2017-01-01', 6)

          AND in_time <= '2017-01-01'

          THEN 1

        END

      ) dat_7,

      (

        CASE

          WHEN in_time >= DATE_SUB('2017-01-01', 14)

          AND in_time <= '2017-01-01'

          THEN 1

        END

      ) dat_15,

      (

        CASE

          WHEN in_time >= DATE_SUB('2017-01-01', 29)

          AND in_time <= '2017-01-01'

          THEN 1

        END

      ) dat_30,

      (

        CASE

          WHEN in_time >= DATE_SUB('2017-01-01', 59)

          AND in_time <= '2017-01-01'

          THEN 1

        END

      ) dat_60,

      (

        CASE

          WHEN in_time >= DATE_SUB('2017-01-01', 89)

          AND in_time <= '2017-01-01'

          THEN 1

        END

      ) dat_90,

      hour(in_time) visit_hour,

      row_number () over (

        distribute BY a.user_id sort BY a.in_time DESC

      ) rn_desc,

      row_number () over (

        distribute BY a.user_id sort BY a.in_time ASC

      ) rn_asc

    FROM

      fdm.itcast_fdm_user_pc_pageview a

    WHERE in_time >= DATE_ADD('2017-01-01', -89)

      AND in_time <= '2017-01-01') pc

    ON (t.user_id = pc.user_id)

  LEFT JOIN

    (SELECT

      user_id,

      sum(

        CASE

          WHEN TYPE = 'visit_ip'

          THEN cnt

        END

      ) month1_pc_diff_ip_cnt,

      MAX(

        CASE

          WHEN TYPE = 'visit_ip' and rn= 1

          THEN con

        END

      ) month1_pc_common_ip,

      sum(

        CASE

          WHEN TYPE = 'cookie_id'

          THEN cnt

        END

      ) month1_pc_diff_cookie_cnt,

      MAX(

        CASE

          WHEN TYPE = 'cookie_id' and rn = 1

          THEN con

        END

      ) month1_pc_common_cookie,

      MAX(

        CASE

          WHEN TYPE = 'browser_name' and rn = 1

          THEN con

        END

      ) month1_pc_common_browser_name,

      MAX(

        CASE

          WHEN TYPE = 'visit_os' and rn = 1

          THEN con

        END

      ) month1_pc_common_os

    FROM

      gdm.itcast_gdm_user_visit_temp_01

    GROUP BY user_id) b

    ON (t.user_id = b.user_id)

  LEFT JOIN

    (SELECT

      a.*,

      (

        CASE

          WHEN log_time >= DATE_SUB('2017-01-01', 6)

          AND log_time <= '2017-01-01'

          THEN 1

        END

      ) dat_7,

      (

        CASE

          WHEN log_time >= DATE_SUB('2017-01-01', 14)

          AND log_time <= '2017-01-01'

          THEN 1

        END

      ) dat_15,

      (

        CASE

          WHEN log_time >= DATE_SUB('2017-01-01', 29)

          AND log_time <= '2017-01-01'

          THEN 1

        END

      ) dat_30,

      (

        CASE

          WHEN log_time >= DATE_SUB('2017-01-01', 59)

          AND log_time <= '2017-01-01'

          THEN 1

        END

      ) dat_60,

      (

        CASE

          WHEN log_time >= DATE_SUB('2017-01-01', 89)

          AND log_time <= '2017-01-01'

          THEN 1

        END

      ) dat_90,

      row_number () over (

        distribute BY a.user_id sort BY a.log_time DESC

      ) rn_desc,

      row_number () over (

        distribute BY a.user_id sort BY a.log_time ASC

      ) rn_asc

    FROM

      fdm.itcast_fdm_user_app_pageview a

 WHERE log_time >= DATE_ADD('2017-01-01', -89)

      AND log_time <= '2017-01-01') app

    ON (t.user_id = app.user_id)

GROUP BY t.user_id ;

 

8.7 用戶畫像開發--用戶寬表彙總模型表

--用戶畫像寬表模型

create database if not exists adm;

create table if not exists adm.itcast_adm_personas(

user_id string     ,--用戶ID

user_name string     ,--用戶登錄名

user_sex  string     ,--用戶性別

user_birthday string     ,--用戶生日

user_age  bigint     ,--用戶年齡

constellation string     ,--用戶星座

province string     ,--省份

city string     ,--城市

city_level string     ,--城市等級

hex_mail string ,--郵箱

op_mail string ,--郵箱運營商

hex_phone string ,--手機號

fore_phone string ,--手機前3

op_phone string ,--手機運營商

add_time timestamp ,--註冊時間

login_ip string ,--登錄ip地址

login_source string ,--登錄來源

request_user string ,--邀請人

total_mark bigint ,--會員積分

used_mark bigint ,--已使用積分

level_name string ,--會員等級名稱

blacklist bigint        ,--用戶黑名單

is_married bigint ,--婚姻情況

education string        ,--學歷

monthly_money double        ,--收入

profession string        ,--職業

sex_model bigint ,--性別模型

is_pregnant_woman bigint        ,--是否孕婦

is_have_children bigint        ,--是否有小孩

children_sex_rate double               , --孩子性別機率

children_age_rate double               ,--孩子年齡機率

is_have_car bigint ,--是否有車

potential_car_user_rate double          ,--潛在汽車用戶機率

phone_brand string ,--使用手機品牌

phone_brand_level string               ,--使用手機品牌檔次

phone_cnt bigint        ,--使用多少種不一樣的手機

change_phone_rate bigint        ,--更換手機頻率

majia_flag string        ,--馬甲標誌

majie_account_cnt bigint               ,--馬甲帳號數量

loyal_model bigint ,--用戶忠誠度

shopping_type_model bigint        ,--用戶購物類型

figure_model bigint ,--身材

stature_model bigint        ,--身高

first_order_time timestamp, --第一次消費時間

last_order_time timestamp, --最近一次消費時間

first_order_ago bigint, --首單距今時間

last_order_ago bigint, --尾單距今時間

month1_hg_order_cnt bigint, --30天購買次數(不含退拒)

month1_hg_order_amt double, --30天購買金額(不含退拒)

month2_hg_order_cnt bigint, --60天購買次數(不含退拒)

month2_hg_order_amt double, --60天購買金額(不含退拒)

month3_hg_order_cnt bigint, --90天購買次數(不含退拒)

month3_hg_order_amt double, --90天購買金額(不含退拒)

month1_order_cnt bigint,        --30天購買次數(含退拒)

month1_order_amt double, --30天購買金額(含退拒)

month2_order_cnt bigint,        --60天購買次數(含退拒)

month2_order_amt double, --60天購買金額(含退拒)

month3_order_cnt bigint,        --90天購買次數(含退拒)

month3_order_amt double, --90天購買金額(含退拒)

max_order_amt double, --最大消費金額

min_order_amt double, --最小消費金額

total_order_cnt bigint, --累計消費次數(不含退拒)

total_order_amt double, --累計消費金額(不含退拒)

user_avg_amt double, --客單價(含退拒)

month3_user_avg_amt double, --90天的客單價

common_address string, --經常使用收貨地址

common_paytype string, --經常使用支付方式

month1_cart_cnt bigint, --30天購物車的次數

month1_cart_goods_cnt bigint,        --30天購物車商品件數

month1_cart_submit_cnt bigint,        --30天購物車提交商品件數

month1_cart_rate double,        --30天購物車成功率

month1_cart_cancle_cnt double,        --30天購物車放棄件數

return_cnt bigint, --退貨商品數量

return_amt double, --退貨商品金額

reject_cnt bigint, --拒收商品數量

reject_amt double, --拒收商品金額

last_return_time timestamp, --最近一次退貨時間

school_order_cnt bigint,        --學校下單總數

company_order_cnt bigint, --單位下單總數

home_order_cnt bigint, --家裏下單總數

forenoon_order_cnt bigint, --上午下單總數

afternoon_order_cnt bigint, --下午下單總數

night_order_cnt bigint, --晚上下單總數

morning_order_cnt bigint, --凌晨下單總數

first_category_id BIGINT,             --一級分類ID

first_category_name STRING,          --一級分類名稱

second_category_id BIGINT,           --二分類ID

second_catery_name STRING,         --二級分類名稱

third_category_id BIGINT,            --三級分類ID

third_category_name STRING,        --三級分類名稱

month1_category_cnt BIGINT,        --30天購物類目次數

month1_category_amt STRING,       --30天購物類目金額

month3_category_cnt BIGINT,        --90天購物類目次數

month3_category_amt STRING,      --90天購物類目金額

month6_category_cnt BIGINT,       --180天購物類目次數

month6_category_amt STRING,      --180天購物類目金額

total_category_cnt BIGINT,         --累計購物類目次數

total_category_amt STRING,        --累計購物類目次數

month1_cart_category_cnt BIGINT,   --30天購物車類目次數

month3_cart_category_cnt BIGINT,   --90天購物車類目次數

month6_cart_category_cnt BIGINT,    --180天購物車類目次數

total_cart_category_cnt BIGINT,      --累計購物車類目次數

last_category_time TIMESTAMP,     --最後一次購買類目時間

last_category_ago BIGINT,          --最後一次購買類目距今天數

latest_pc_visit_date string,           --最近一次PC端訪問日期

latest_app_visit_date string,          --最近一次APP端訪問日期

latest_pc_visit_session string,         --最近一次PC端訪問的session

latest_pc_cookies string,             --最近一次PC端訪問的cookies

latest_pc_pv string,                 --最近一次PC端訪問的PV

latest_pc_browser_name string,       --最近一次PC端訪問使用的遊覽器

latest_pc_visit_os string,             --最近一次PC端訪問使用的操做系統

latest_app_name string,              --最近一次APP端訪問app名稱

latest_app_visit_os string,             --最近一次APP端訪問使用的操做系統

latest_visit_ip string,                 --最近一次訪問IP(不分APPPC)

latest_city string,                     --最近一次訪問城市(不分APPPC)

latest_province string,                 --最近一次訪問省份(不分APPPC)

first_pc_visit_date string,             --第一次PC端訪問日期

first_app_visit_date string,            --第一次APP端訪問日期

first_pc_visit_session string,          --第一次PC端訪問的session

first_pc_cookies string,                --第一次PC端訪問的cookies

first_pc_pv string,                     --第一次PC端訪問的PV

first_pc_browser_name string,           --第一次PC端訪問使用的遊覽器

first_pc_visit_os string,               --第一次PC端訪問使用的操做系統

first_app_name string,                  --第一次APP端訪問app名稱

first_app_visit_os string,              --第一次APP端訪問使用的操做系統

first_visit_ip string,                  --第一次訪問IP(不分APPPC)

first_city string,                      --第一次訪問城市(不分APPPC)

first_province string,                  --第一次訪問省份(不分APPPC)

day7_app_cnt bigint,                    --7APP端訪問次數

day15_app_cnt bigint,                   --15APP端訪問次數

month1_app_cnt bigint,                  --30APP端訪問次數

month2_app_cnt bigint,                  --60APP端訪問次數

month3_app_cnt bigint,                  --90APP端訪問次數

day7_pc_cnt bigint,                     --7PC端訪問次數

day15_pc_cnt bigint,                    --15PC端訪問次數

month1_pc_cnt bigint,                   --30PC端訪問次數

month2_pc_cnt bigint,                   --60PC端訪問次數

month3_pc_cnt bigint,                   --90PC端訪問次數

month1_pc_days bigint,                  --30PC端訪問天數

month1_pc_pv bigint,                    --30PC端訪問PV

month1_pc_avg_pv bigint,                --30PC端訪問平均PV

month1_pc_diff_ip_cnt bigint,           --30PC端訪問不一樣ip

month1_pc_diff_cookie_cnt bigint,       --30PC端訪問不一樣的cookie

month1_pc_common_ip string,             --30PC端訪問最經常使用ip

month1_pc_common_cookie string,         --30PC端訪問最經常使用的cookie

month1_pc_common_browser_name string,   --30PC端訪問最經常使用遊覽器

month1_pc_common_os string,             --30PC端訪問最經常使用的操做系統

month1_hour025_cnt bigint,              --30PC0-5點訪問次數

month1_hour627_cnt bigint,              --30PC6-7點訪問次數

month1_hour829_cnt bigint,              --30PC8-9點訪問次數

month1_hour10212_cnt bigint,            --30PC10-12點訪問次數

month1_hour13214_cnt bigint,            --30PC13-14點訪問次數

month1_hour15217_cnt bigint,            --30PC15-17點訪問次數

month1_hour18219_cnt bigint,            --30PC18-19點訪問次數

month1_hour20221_cnt bigint,            --30PC20-21點訪問次數

month1_hour22223_cnt bigint             --30PC22-23點訪問次數

);

 

 

-----加載數據

 

insert overwrite table adm.itcast_adm_personas

select  

a.user_id,

a.user_name,

a.user_sex,

a.user_birthday,

a.user_age,

a.constellation,

a.province,

a.city,

a.city_level,

a.hex_mail,

a.op_mail,

a.hex_phone,

a.fore_phone,

a.op_phone,

a.add_time,

a.login_ip,

a.login_source,

a.request_user,

a.total_mark,

a.used_mark,

a.level_name,

a.blacklist,

a.is_married,

a.education,

a.monthly_money,

a.profession,

a.sex_model,

a.is_pregnant_woman,

a.is_have_children,

a.children_sex_rate,

a.children_age_rate,

a.is_have_car,

a.potential_car_user_rate,

a.phone_brand,

a.phone_brand_level,

a.phone_cnt,

a.change_phone_rate,

a.majia_flag,

a.majie_account_cnt,

a.loyal_model,

a.shopping_type_model,

a.figure_model,

a.stature_model,

b.first_order_time,  

b.last_order_time,  

b.first_order_ago,  

b.last_order_ago,  

b.month1_hg_order_cnt,

b.month1_hg_order_amt,

b.month2_hg_order_cnt,

b.month2_hg_order_amt,

b.month3_hg_order_cnt,

b.month3_hg_order_amt,

b.month1_order_cnt,   

b.month1_order_amt,   

b.month2_order_cnt,   

b.month2_order_amt,   

b.month3_order_cnt,   

b.month3_order_amt,   

b.max_order_amt,      

b.min_order_amt,      

b.total_order_cnt,    

b.total_order_amt,    

b.user_avg_amt,       

b.month3_user_avg_amt,    

b.common_address,         

b.common_paytype,         

b.month1_cart_cnt,        

b.month1_cart_goods_cnt,  

b.month1_cart_submit_cnt,

b.month1_cart_rate,

b.month1_cart_cancle_cnt,

b.return_cnt,             

b.return_amt,             

b.reject_cnt,             

b.reject_amt,             

b.last_return_time,       

b.school_order_cnt,       

b.company_order_cnt,      

b.home_order_cnt,         

b.forenoon_order_cnt,     

b.afternoon_order_cnt,    

b.night_order_cnt,        

b.morning_order_cnt,      

c.first_category_id,

c.first_category_name,

c.second_category_id,

c.second_catery_name,

c.third_category_id,

c.third_category_name,

c.month1_category_cnt,

c.month1_category_amt,

c.month3_category_cnt,

c.month3_category_amt,

c.month6_category_cnt,

c.month6_category_amt,

c.total_category_cnt,

c.total_category_amt,

c.month1_category_cnt,

c.month3_category_cnt,

c.month6_category_cnt,

c.total_category_cnt,

c.last_category_time,

c.last_category_ago,

d.latest_pc_visit_date,         

d.latest_app_visit_date,        

d.latest_pc_visit_session,      

d.latest_pc_cookies,            

d.latest_pc_pv,                 

d.latest_pc_browser_name,       

d.latest_pc_visit_os,           

d.latest_app_name,              

d.latest_app_visit_os,          

d.latest_visit_ip,              

d.latest_city,                  

d.latest_province,              

d.first_pc_visit_date,          

d.first_app_visit_date,         

d.first_pc_visit_session,       

d.first_pc_cookies,             

d.first_pc_pv,                  

d.first_pc_browser_name,        

d.first_pc_visit_os,            

d.first_app_name,               

d.first_app_visit_os,           

d.first_visit_ip,               

d.first_city,                   

d.first_province,               

d.day7_app_cnt,                 

d.day15_app_cnt,                

d.month1_app_cnt,               

d.month2_app_cnt,               

d.month3_app_cnt,               

d.day7_pc_cnt,                  

d.day15_pc_cnt,                 

d.month1_pc_cnt,                

d.month2_pc_cnt,                

d.month3_pc_cnt,                

d.month1_pc_days,               

d.month1_pc_pv,                 

d.month1_pc_avg_pv,             

d.month1_pc_diff_ip_cnt,        

d.month1_pc_diff_cookie_cnt,    

d.month1_pc_common_ip,          

d.month1_pc_common_cookie,      

d.month1_pc_common_browser_name,

d.month1_pc_common_os,          

d.month1_hour025_cnt,           

d.month1_hour627_cnt,           

d.month1_hour829_cnt,           

d.month1_hour10212_cnt,         

d.month1_hour13214_cnt,         

d.month1_hour15217_cnt,         

d.month1_hour18219_cnt,         

d.month1_hour20221_cnt,         

d.month1_hour22223_cnt          

from gdm.itcast_gdm_user_basic a

left join gdm.itcast_gdm_user_consume_order b on a.user_id=b.user_id

left join gdm.itcast_gdm_user_buy_category c on a.user_id=c.user_id

left join gdm.itcast_gdm_user_visit d on a.user_id=d.user_id;

 

 

 

 

----------創建hive/hbase關聯表(利用hive/hbase之間互相映射的特性,將存在hive數據倉庫中的數據導入到hbase中)

 

CREATE TABLE adm.itcast_adm_personas_hbase (

  user_id STRING,

  user_name STRING,

  user_sex STRING,

  user_birthday STRING,

  user_age BIGINT,

  constellation STRING,

  province STRING,

  city STRING,

  city_level STRING,

  hex_mail STRING,

  op_mail STRING,

  hex_phone STRING,

  fore_phone STRING,

  op_phone STRING,

  add_time TIMESTAMP,

  login_ip STRING,

  login_source STRING,

  request_user STRING,

  total_mark BIGINT,

  used_mark BIGINT,

  level_name STRING,

  blacklist BIGINT,

  is_married BIGINT,

  education STRING,

  monthly_money DOUBLE,

  profession STRING,

  sex_model BIGINT,

  is_pregnant_woman BIGINT,

  is_have_children BIGINT,

  children_sex_rate DOUBLE,

  children_age_rate DOUBLE,

  is_have_car BIGINT,

  potential_car_user_rate DOUBLE,

  phone_brand STRING,

  phone_brand_level STRING,

  phone_cnt BIGINT,

  change_phone_rate BIGINT,

  majia_flag STRING,

  majie_account_cnt BIGINT,

  loyal_model BIGINT,

  shopping_type_model BIGINT,

  figure_model BIGINT,

  stature_model BIGINT,

  first_order_time TIMESTAMP,

  last_order_time TIMESTAMP,

  first_order_ago BIGINT,

  last_order_ago BIGINT,

  month1_hg_order_cnt BIGINT,

  month1_hg_order_amt DOUBLE,

  month2_hg_order_cnt BIGINT,

  month2_hg_order_amt DOUBLE,

  month3_hg_order_cnt BIGINT,

  month3_hg_order_amt DOUBLE,

  month1_order_cnt BIGINT,

  month1_order_amt DOUBLE,

  month2_order_cnt BIGINT,

  month2_order_amt DOUBLE,

  month3_order_cnt BIGINT,

  month3_order_amt DOUBLE,

  max_order_amt DOUBLE,

  min_order_amt DOUBLE,

  total_order_cnt BIGINT,

  total_order_amt DOUBLE,

  user_avg_amt DOUBLE,

  month3_user_avg_amt DOUBLE,

  common_address STRING,

  common_paytype STRING,

  month1_cart_cnt BIGINT,

  month1_cart_goods_cnt BIGINT,

  month1_cart_submit_cnt BIGINT,

  month1_cart_rate DOUBLE,

  month1_cart_cancle_cnt DOUBLE,

  return_cnt BIGINT,

  return_amt DOUBLE,

  reject_cnt BIGINT,

  reject_amt DOUBLE,

  last_return_time TIMESTAMP,

  school_order_cnt BIGINT,

  company_order_cnt BIGINT,

  home_order_cnt BIGINT,

  forenoon_order_cnt BIGINT,

  afternoon_order_cnt BIGINT,

  night_order_cnt BIGINT,

  morning_order_cnt BIGINT,

  first_category_id BIGINT,

  first_category_name STRING,

  second_category_id BIGINT,

  second_catery_name STRING,

  third_category_id BIGINT,

  third_category_name STRING,

  month1_category_cnt BIGINT,

  month1_category_amt STRING,

  month3_category_cnt BIGINT,

  month3_category_amt STRING,

  month6_category_cnt BIGINT,

  month6_category_amt STRING,

  total_category_cnt BIGINT,

  total_category_amt STRING,

  month1_cart_category_cnt BIGINT,

  month3_cart_category_cnt BIGINT,

  month6_cart_category_cnt BIGINT,

  total_cart_category_cnt BIGINT,

  last_category_time TIMESTAMP,

  last_category_ago BIGINT,

  latest_pc_visit_date STRING,

  latest_app_visit_date STRING,

  latest_pc_visit_session STRING,

  latest_pc_cookies STRING,

  latest_pc_pv STRING,

  latest_pc_browser_name STRING,

  latest_pc_visit_os STRING,

  latest_app_name STRING,

  latest_app_visit_os STRING,

  latest_visit_ip STRING,

  latest_city STRING,

  latest_province STRING,

  first_pc_visit_date STRING,

  first_app_visit_date STRING,

  first_pc_visit_session STRING,

  first_pc_cookies STRING,

  first_pc_pv STRING,

  first_pc_browser_name STRING,

  first_pc_visit_os STRING,

  first_app_name STRING,

  first_app_visit_os STRING,

  first_visit_ip STRING,

  first_city STRING,

  first_province STRING,

  day7_app_cnt BIGINT,

  day15_app_cnt BIGINT,

  month1_app_cnt BIGINT,

  month2_app_cnt BIGINT,

  month3_app_cnt BIGINT,

  day7_pc_cnt BIGINT,

  day15_pc_cnt BIGINT,

  month1_pc_cnt BIGINT,

  month2_pc_cnt BIGINT,

  month3_pc_cnt BIGINT,

  month1_pc_days BIGINT,

  month1_pc_pv BIGINT,

  month1_pc_avg_pv BIGINT,

  month1_pc_diff_ip_cnt BIGINT,

  month1_pc_diff_cookie_cnt BIGINT,

  month1_pc_common_ip string,

  month1_pc_common_cookie string,

  month1_pc_common_browser_name string,

  month1_pc_common_os string,

  month1_hour025_cnt BIGINT,

  month1_hour627_cnt BIGINT,

  month1_hour829_cnt BIGINT,

  month1_hour10212_cnt BIGINT,

  month1_hour13214_cnt BIGINT,

  month1_hour15217_cnt BIGINT,

  month1_hour18219_cnt BIGINT,

  month1_hour20221_cnt BIGINT,

  month1_hour22223_cnt BIGINT

) STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' WITH SERDEPROPERTIES (

  "hbase.columns.mapping" = ":key,

basicInfo:user_name,

basicInfo:user_sex,

basicInfo:user_birthday,

basicInfo:user_age,

basicInfo:constellation,

basicInfo:province,

basicInfo:city,

basicInfo:city_level,

basicInfo:hex_mail,

basicInfo:op_mail,

basicInfo:hex_phone,

basicInfo:fore_phone,

basicInfo:op_phone,

basicInfo:add_time,

basicInfo:login_ip,

basicInfo:login_source,

basicInfo:request_user,

basicInfo:total_mark,

basicInfo:used_mark,

basicInfo:level_name,

basicInfo:blacklist,

basicInfo:is_married,

basicInfo:education,

basicInfo:monthly_money,

basicInfo:profession,

basicInfo:sex_model,

basicInfo:is_pregnant_woman,

basicInfo:is_have_children,

basicInfo:children_sex_rate,

basicInfo:children_age_rate,

basicInfo:is_have_car,

basicInfo:potential_car_user_rate,

basicInfo:phone_brand,

basicInfo:phone_brand_level,

basicInfo:phone_cnt,

basicInfo:change_phone_rate,

basicInfo:majia_flag,

basicInfo:majie_account_cnt,

basicInfo:loyal_model,

basicInfo:shopping_type_model,

basicInfo:figure_model,

basicInfo:stature_model,

order:first_order_time,

order:last_order_time,

order:first_order_ago,

order:last_order_ago,

order:month1_hg_order_cnt,

order:month1_hg_order_amt,

order:month2_hg_order_cnt,

order:month2_hg_order_amt,

order:month3_hg_order_cnt,

order:month3_hg_order_amt,

order:month1_order_cnt,

order:month1_order_amt,

order:month2_order_cnt,

order:month2_order_amt,

order:month3_order_cnt,

order:month3_order_amt,

order:max_order_amt,

order:min_order_amt,

order:total_order_cnt,

order:total_order_amt,

order:user_avg_amt,

order:month3_user_avg_amt,

order:common_address,

order:common_paytype,

order:month1_cart_cnt,

order:month1_cart_goods_cnt,

order:month1_cart_submit_cnt,

order:month1_cart_rate,

order:month1_cart_cancle_cnt,

order:return_cnt,

order:return_amt,

order:reject_cnt,

order:reject_amt,

order:last_return_time,

order:school_order_cnt,

order:company_order_cnt,

order:home_order_cnt,

order:forenoon_order_cnt,

order:afternoon_order_cnt,

order:night_order_cnt,

order:morning_order_cnt,

category:first_category_id,

category:first_category_name,

category:second_category_id,

category:second_catery_name,

category:third_category_id,

category:third_category_name,

category:month1_category_cnt,

category:month1_category_amt,

category:month3_category_cnt,

category:month3_category_amt,

category:month6_category_cnt,

category:month6_category_amt,

category:total_category_cnt,

category:total_category_amt,

category:month1_cart_category_cnt,

category:month3_cart_category_cnt,

category:month6_cart_category_cnt,

category:total_cart_category_cnt,

category:last_category_time,

category:last_category_ago,

visit:latest_pc_visit_date,

visit:latest_app_visit_date,

visit:latest_pc_visit_session,

visit:latest_pc_cookies,

visit:latest_pc_pv,

visit:latest_pc_browser_name,

visit:latest_pc_visit_os,

visit:latest_app_name,

visit:latest_app_visit_os,

visit:latest_visit_ip,

visit:latest_city,

visit:latest_province,

visit:first_pc_visit_date,

visit:first_app_visit_date,

visit:first_pc_visit_session,

visit:first_pc_cookies,

visit:first_pc_pv,

visit:first_pc_browser_name,

visit:first_pc_visit_os,

visit:first_app_name,

visit:first_app_visit_os,

visit:first_visit_ip,

visit:first_city,

visit:first_province,

visit:day7_app_cnt,

visit:day15_app_cnt,

visit:month1_app_cnt,

visit:month2_app_cnt,

visit:month3_app_cnt,

visit:day7_pc_cnt,

visit:day15_pc_cnt,

visit:month1_pc_cnt,

visit:month2_pc_cnt,

visit:month3_pc_cnt,

visit:month1_pc_days,

visit:month1_pc_pv,

visit:month1_pc_avg_pv,

visit:month1_pc_diff_ip_cnt,

visit:month1_pc_diff_cookie_cnt,

visit:month1_pc_common_ip,

visit:month1_pc_common_cookie,

visit:month1_pc_common_browser_name,

visit:month1_pc_common_os,

visit:month1_hour025_cnt,

visit:month1_hour627_cnt,

visit:month1_hour829_cnt,

visit:month1_hour10212_cnt,

visit:month1_hour13214_cnt,

visit:month1_hour15217_cnt,

visit:month1_hour18219_cnt,

visit:month1_hour20221_cnt,

visit:month1_hour22223_cnt"

) TBLPROPERTIES (

  "hbase.table.name" = "itcast_adm_personas_hbase_2017_01_01",

"hbase.mapred.output.outputtable"="itcast_adm_personas_hbase_2017_01_01"

) ;

 

----向映射表中加載數據

 

insert overwrite table adm.itcast_adm_personas_hbase select * from adm.itcast_adm_personas;

 

 

----加載成功後查看錶中是否有數據存在

hive中的表adm.itcast_adm_personas_hbase

hive映射到hbase中的表itcast_adm_personas_hbase_2017_01_01

 

 

9、  電商用戶畫像數據可視化

9.1 數據可視化方案

技術框架:maven+spring+phoenix

核心點:採用phoenixhbase整合,經過咱們熟知的sql語句來操做NoSql數據庫。

 

 

9.2 Phoenix創建hbase表的映射表

CREATE TABLE IF NOT EXISTS "itcast_adm_personas_hbase_2017_01_01"(

rw varchar(100) primary key,

"basicInfo"."user_name" VARCHAR(100),

"basicInfo"."user_sex"  VARCHAR(100),

"basicInfo"."user_birthday" VARCHAR(100),

"basicInfo"."user_age"  VARCHAR(100),

"basicInfo"."constellation" VARCHAR(100),

"basicInfo"."province" VARCHAR(100),

"basicInfo"."city" VARCHAR(100),

"basicInfo"."city_level" VARCHAR(100),

"basicInfo"."hex_mail" VARCHAR(100),

"basicInfo"."op_mail" VARCHAR(100),

"basicInfo"."hex_phone" VARCHAR(100),

"basicInfo"."fore_phone" VARCHAR(100),

"basicInfo"."op_phone" VARCHAR(100),

"basicInfo"."add_time" VARCHAR(100),

"basicInfo"."login_ip" VARCHAR(100),

"basicInfo"."login_source" VARCHAR(100),

"basicInfo"."request_user" VARCHAR(100),

"basicInfo"."total_mark" VARCHAR(100),

"basicInfo"."used_mark" VARCHAR(100),

"basicInfo"."level_name" VARCHAR(100),

"basicInfo"."blacklist" VARCHAR(100),

"basicInfo"."is_married" VARCHAR(100),

"basicInfo"."education" VARCHAR(100),

"basicInfo"."monthly_money" VARCHAR(100),

"basicInfo"."profession" VARCHAR(100),

"basicInfo"."sex_model" VARCHAR(100),

"basicInfo"."is_pregnant_woman" VARCHAR(100),

"basicInfo"."is_have_children" VARCHAR(100),

"basicInfo"."children_sex_rate" VARCHAR(100),

"basicInfo"."children_age_rate" VARCHAR(100),

"basicInfo"."is_have_car" VARCHAR(100),

"basicInfo"."potential_car_user_rate" VARCHAR(100),

"basicInfo"."phone_brand" VARCHAR(100),

"basicInfo"."phone_brand_level" VARCHAR(100),

"basicInfo"."phone_cnt" VARCHAR(100),

"basicInfo"."change_phone_rate" VARCHAR(100),

"basicInfo"."majia_flag" VARCHAR(100),

"basicInfo"."majie_account_cnt" VARCHAR(100),

"basicInfo"."loyal_model" VARCHAR(100),

"basicInfo"."shopping_type_model" VARCHAR(100),

"basicInfo"."figure_model" VARCHAR(100),

"basicInfo"."stature_model" VARCHAR(100),

"order"."first_order_time" VARCHAR(100),

"order"."last_order_time" VARCHAR(100),

"order"."first_order_ago" VARCHAR(100),

"order"."last_order_ago" VARCHAR(100),

"order"."month1_hg_order_cnt" VARCHAR(100),

"order"."month1_hg_order_amt" VARCHAR(100),

"order"."month2_hg_order_cnt" VARCHAR(100),

"order"."month2_hg_order_amt" VARCHAR(100),

"order"."month3_hg_order_cnt" VARCHAR(100),

"order"."month3_hg_order_amt" VARCHAR(100),

"order"."month1_order_cnt" VARCHAR(100),

"order"."month1_order_amt" VARCHAR(100),

"order"."month2_order_cnt" VARCHAR(100),

"order"."month2_order_amt" VARCHAR(100),

"order"."month3_order_cnt" VARCHAR(100),

"order"."month3_order_amt" VARCHAR(100),

"order"."max_order_amt" VARCHAR(100),

"order"."min_order_amt" VARCHAR(100),

"order"."total_order_cnt" VARCHAR(100),

"order"."total_order_amt" VARCHAR(100),

"order"."user_avg_amt" VARCHAR(100),

"order"."month3_user_avg_amt" VARCHAR(100),

"order"."common_address" VARCHAR(100),

"order"."common_paytype" VARCHAR(100),

"order"."month1_cart_cnt" VARCHAR(100),

"order"."month1_cart_goods_cnt" VARCHAR(100),

"order"."month1_cart_submit_cnt" VARCHAR(100),

"order"."month1_cart_rate" VARCHAR(100),

"order"."month1_cart_cancle_cnt" VARCHAR(100),

"order"."return_cnt" VARCHAR(100),

"order"."return_amt" VARCHAR(100),

"order"."reject_cnt" VARCHAR(100),

"order"."reject_amt" VARCHAR(100),

"order"."last_return_time" VARCHAR(100),

"order"."school_order_cnt" VARCHAR(100),

"order"."company_order_cnt" VARCHAR(100),

"order"."home_order_cnt" VARCHAR(100),

"order"."forenoon_order_cnt" VARCHAR(100),

"order"."afternoon_order_cnt" VARCHAR(100),

"order"."night_order_cnt" VARCHAR(100),

"order"."morning_order_cnt" VARCHAR(100),

"category"."first_category_id" VARCHAR(100),

"category"."first_category_name" VARCHAR(100),

"category"."second_category_id" VARCHAR(100),

"category"."second_catery_name" VARCHAR(100),

"category"."third_category_id" VARCHAR(100),

"category"."third_category_name" VARCHAR(100),

"category"."month1_category_cnt" VARCHAR(100),

"category"."month1_category_amt" VARCHAR(100),

"category"."month3_category_cnt" VARCHAR(100),

"category"."month3_category_amt" VARCHAR(100),

"category"."month6_category_cnt" VARCHAR(100),

"category"."month6_category_amt" VARCHAR(100),

"category"."total_category_cnt" VARCHAR(100),

"category"."total_category_amt" VARCHAR(100),

"category"."month1_cart_category_cnt" VARCHAR(100),

"category"."month3_cart_category_cnt" VARCHAR(100),

"category"."month6_cart_category_cnt" VARCHAR(100),

"category"."total_cart_category_cnt" VARCHAR(100),

"category"."last_category_time" VARCHAR(100),

"category"."last_category_ago" VARCHAR(100),

"visit"."latest_pc_visit_date" VARCHAR(100),

"visit"."latest_app_visit_date" VARCHAR(100),

"visit"."latest_pc_visit_session" VARCHAR(100),

"visit"."latest_pc_cookies" VARCHAR(100),

"visit"."latest_pc_pv" VARCHAR(100),

"visit"."latest_pc_browser_name" VARCHAR(100),

"visit"."latest_pc_visit_os" VARCHAR(100),

"visit"."latest_app_name" VARCHAR(100),

"visit"."latest_app_visit_os" VARCHAR(100),

"visit"."latest_visit_ip" VARCHAR(100),

"visit"."latest_city" VARCHAR(100),

"visit"."latest_province" VARCHAR(100),

"visit"."first_pc_visit_date" VARCHAR(100),

"visit"."first_app_visit_date" VARCHAR(100),

"visit"."first_pc_visit_session" VARCHAR(100),

"visit"."first_pc_cookies" VARCHAR(100),

"visit"."first_pc_pv" VARCHAR(100),

"visit"."first_pc_browser_name" VARCHAR(100),

"visit"."first_pc_visit_os" VARCHAR(100),

"visit"."first_app_name" VARCHAR(100),

"visit"."first_app_visit_os" VARCHAR(100),

"visit"."first_visit_ip" VARCHAR(100),

"visit"."first_city" VARCHAR(100),

"visit"."first_province" VARCHAR(100),

"visit"."day7_app_cnt" VARCHAR(100),

"visit"."day15_app_cnt" VARCHAR(100),

"visit"."month1_app_cnt" VARCHAR(100),

"visit"."month2_app_cnt" VARCHAR(100),

"visit"."month3_app_cnt" VARCHAR(100),

"visit"."day7_pc_cnt" VARCHAR(100),

"visit"."day15_pc_cnt" VARCHAR(100),

"visit"."month1_pc_cnt" VARCHAR(100),

"visit"."month2_pc_cnt" VARCHAR(100),

"visit"."month3_pc_cnt" VARCHAR(100),

"visit"."month1_pc_days" VARCHAR(100),

"visit"."month1_pc_pv" VARCHAR(100),

"visit"."month1_pc_avg_pv" VARCHAR(100),

"visit"."month1_pc_diff_ip_cnt" VARCHAR(100),

"visit"."month1_pc_diff_cookie_cnt" VARCHAR(100),

"visit"."month1_pc_common_ip" VARCHAR(100),

"visit"."month1_pc_common_cookie" VARCHAR(100),

"visit"."month1_pc_common_browser_name" VARCHAR(100),

"visit"."month1_pc_common_os" VARCHAR(100),

"visit"."month1_hour025_cnt" VARCHAR(100),

"visit"."month1_hour627_cnt" VARCHAR(100),

"visit"."month1_hour829_cnt" VARCHAR(100),

"visit"."month1_hour10212_cnt" VARCHAR(100),

"visit"."month1_hour13214_cnt" VARCHAR(100),

"visit"."month1_hour15217_cnt" VARCHAR(100),

"visit"."month1_hour18219_cnt" VARCHAR(100),

"visit"."month1_hour20221_cnt" VARCHAR(100),

"visit"."month1_hour22223_cnt" VARCHAR(100)

);

 

這個語句有幾個注意點

創建映射表以前要說明的是,Phoenix是大小寫敏感的,而且全部命令都是大寫,若是你建的表名沒有用雙引號括起來,那麼不管你輸入的是大寫仍是小寫,創建出來的表名都是大寫的,若是你須要創建出同時包含大寫和小寫的表名和字段名,請把表名或者字段名用雙引號括起來。
你能夠創建讀寫的表或者只讀的表,他們的區別以下:

  • 讀寫表:若是你定義的列簇不存在,會被自動創建出來,而且賦以空值
  • 只讀表:你定義的列簇必須事先存在
  • IF NOT EXISTS能夠保證若是已經有創建過這個表,配置不會被覆蓋
  • 做爲rowkey的字段用 PRIMARY KEY標定
  • 列簇用 columnFamily.columnName 來表示

 

9.3 構建maven工程

 9.4 用戶畫像查詢展示

根據不一樣的維度進行組合查詢,篩選出知足條件的用戶。

 

相關文章
相關標籤/搜索