文章目录

  • 初识用户画像
  • 基础架构
  • 数仓分层
  • 用户画像标签
  • 用户源数据分析
  • 画像目标分析
  • 用户画像建模
    • 用户基本属性表
    • 用户消费订单表
    • 用户购买类目表
    • 用户访问信息表
    • 商品订单表
    • 埋点日志表
    • 访问日志表
    • 商品评论表
    • 搜索日志表
    • 用户收藏表、购物车信心表
  • 环境准备
  • 数仓表的基本介绍
  • 用户画像表结构开发
    • 客户消费订单表
      • 订单表(bdm_order)
      • 订单明细表(bdm_order_des)
      • 订单表(fdm_order)
      • 订单表明细表(fdm_order_desc)
      • 订单模型表(gdm_order)
      • 订单地址模型表(gdm_user_order_addr_model)
      • 订单商品表(bdm_order_goods)
      • 订单商品表 (fdm_order_goods)
      • 客户消费订单模型表-临时表01(gdm.gdm_user_consume_order_temp_01
      • 购物车表(bdm_order_cart)
      • 购物车表(fdm_order_cart)
      • 购物车临时模型表--临时表 02(gdm.gdm_user_consume_order_temp_02
      • 常用地址和常用支付方式-临时表 03(gdm_user_consume_order_temp_03)
      • 购物车表和订单表整合(gdm_user_consume_order_temp_100)
      • 客户消费订单表 ***(gdm_user_consume_order)
    • 客户购买类目表
      • 计算订单中客户购买情况(gdm_user_buy_category_temp)
      • 购物车中类目情况(gdm_user_cart_category_temp)
      • 类目表(gdm_category_code)
      • 整合购买情况和类目情况(gdm_user_category_total)
      • 客户购买类目表***(gdm_user_buy_category)
    • 客户基本属性表
      • 用户表(bdm_user)
      • 用户宽表 (fdm_user)
      • 客户基本属性表 *** (gdm_user_basic)
    • 客户访问信息表
      • 用户上网记录表(PC) (bdm_user_pc_click_log)
      • 用户 pc 端 view 表 (fdm_user_pc_pageview)
      • 用户30天访问临时表 (gdm_user_visit_temp_01)
      • 用户上网记录表(APP) (bdm_user_app_click_log)
      • 用户 app 端 view 表 (fdm_user_app_pageview)
      • 客户访问信息表 *** (gdm_user_visit)
    • 用户宽表汇总模型表(adm_personas)

基于电商的用户画像

初识用户画像

采集用户行为数据,挖掘有价值的东西,做到精准营销

构建用户画像的意义就是精准营销

用户画像作用

  • 精准营销
分析产品潜在用户,针对特定群体利用短信邮件等方式进行营销
  • 用户统计
比如中国大学购买书籍人数 TOP10
  • 数据挖掘
构建智能推荐系统,
利用关联规则计算,喜欢红酒的人通常喜欢什么运动品牌,
利用聚类算法分析,喜欢红酒的人年龄段分布情况
  • 进行效果评估
完善产品运营,提升服务质量,
其实这也就相当于市场调研、用户调研,迅速定位服务群体,提供高水平的服务;
  • 对服务或产品进行私人定制
即个性化的服务某类群体甚至每一位用户
(个人认为这是目前的发展趋势,未来的消费主流)。
比如,某公司想推出一款面向5-10岁儿童的玩具,通过用户画像进行分析,
发现形象=“喜羊羊”、价格区间=“中等”的偏好比重最大,
那么就给新产品提供了非常客观有效的决策依据。

基础架构

构建一个用户画像,包括数据源端数据收集、数据预处理、行为建模、构建用户画像

  • 1.采集数据源
  • 2.数据预处理
  • 3.建设数仓、打标签
  • 4.形成用户画像
 和日志流数据分析架构差不多 + 数据源
首先是获取数据源,数据可以来自mysql业务平台
电商用户画像的话,可以采集网站交易数据、用户行为数据、网络日志数据等+ 数据预处理
清洗、结构化、标准化的过程结构化就是二维表的数据,采集到的数据大多都是半结构化的,视频图片音频是非结构的从代码角度讲:就是封装数据即javabean的过程就是结构化的过程scala里就是封装到样例类里标准化,通俗的讲就是统一一个标准,比如格式整理、归一化、统一数据名称 类型等+ 建设数仓、打标签
离线处理、实时处理、模型挖掘算法数仓建设:hive:属性、行为、偏好等进行主题建模离线处理:用sparkSQL、hive实时处理:spark挖掘算法:文本挖掘、nlp、机器学习算法、行为建模(挖掘类标签)+ 形成用户画像

数仓分层

  • 第一层:原始数据拉过来
  • 第二层:数据预处理
  • 第三层:SQL做聚合统计
  • 第四层:结果做高度聚合,拼接成宽表

用户画像标签

统计类标签

  • 对数据表进行一系列的统计分析
这类标签是最为基础也是最为常见的标签类型例如:对于某个用户来说,其性别、年龄、城市、星座、
近7日活跃时长、近7日活跃天数、近7日活跃次数等字段可以
从用户注册数据、用户访问、消费数据中统计得出。该类标签构成了用户画像的基础

规则类标签

  • 由运营和数据人员共同协商确定的规则
该类标签是基于用户行为及确定的规则产生。例如:对平台上“消费活跃”用户这一口径的定义为“近30天交易次数>=2”
在实际开发画像的过程中,由于运营人员对业务更熟悉,而数据人员对数据的结构、分布、特征更为熟悉,
因此规则类标签的规则由运营人员和数据人员共同协商确定

挖掘类标签

  • 机器学习算法的预测判断
该类标签通过机器学习挖掘产生,用于对用户某些属性或行为进行预测判断例如,根据一个用户的行为习惯判断该用户是男性还是女性,
根据一个用户的消费习惯判断其对某商品的偏好程度。该类标签需要通过算法挖掘产生在项目开发工程中,一般统计类和规则类的标签即可以满足应用需求,在开发中占有较大的比例。
机器学习挖掘类标签多应用于预测场景,因为开发周期比较长,成本比较高,所以占比很小

用户源数据分析

画像目标分析

用户画像的目标是通过分析用户行为,最终为每个用户打上标签,以及该标签的权重

如,红酒 0.8、李宁 0.6。

标签:表现了内容,用户对该内容有兴趣、偏好、需求等等。

权重:表现了指数,用户的兴趣、偏好指数,也可能表现用户的需求度,可以简单的理解为可信度,概率。

用户画像建模

用户基本属性表

根据用户所填写的属性标签和推算出来的标签。用于了解用户的人口属性的基本情况和按不同属性维度统计。

作用:按人口属性营销、比如营销80后,对金牛座的优惠,生日营销。

主要数据来源:用户表、用户调查表、孕妇模型表、马甲模型表。

  • 用户表:记录用户最基本的属性特性。
  • 用户调查表:补充用户的其他基本信息。
用户所填写的基本信息:用户ID、用户名、密码、性别、手机号、邮箱、年龄、户籍省份、身份证编号、注册时间、收货地址等用户所填信息计算得到的指标:
生日、星座、城市等级、手机号前3位、手机号运营商、邮件运营商用户调查表得到:学历、收入、职业、婚姻、是否有小孩、是否有车有房、使用手机品牌。根据算法得到:
身高、体重、性别模型、孩子性别概率、潜在汽车用户概率、是否孕妇、孩子年龄概率、手机品牌、更换手机频率、是否有小孩,是否有车,使用手机档次,疑似马甲标准、疑似马甲账号数、用户忠诚度、用户购物类型。

模型算法—性别模型

模型算法—用户汽车模型

模型算法—用户忠诚度模型

模型算法—用户身高尺码模型

模型算法—用户马甲标志模型

模型算法—手机相关标签模型

用户消费订单表

根据客户消费的情况提取的客户标签,用于了解用户的消费总体情况,
最终的目的根据用户消费习惯与消费能力做营销。

主要数据来源:订单表、退货表、用户表、购物车表

订单表可以得到相关标签:

第一次消费时间、
最近一次消费时间、
首单距今时间、
尾单距今时间------分析用户什么时候来购买商品以及多久没有购买了。
最小消费金额、
最大消费金额、
累计消费次数(不含退拒)、
累计消费金额(不含退拒)、
累计使用代金券金额、
累计使用代金券次数。-----分析用户总体消费情况。
客单价(含退拒)、
近60天客单价(含退拒)-----分析用户消费水平。
常用收货地址、
常用支付方式----分析用户常用的消费属性,方便做定向营销。

退货表可以得到相关标签:

近30天购买次数(不含退拒)、
近30天购买金额(不含退拒)
近30天购买次数(含退拒)、
近30天购买金额(含退拒)----分析用户最近的消费能力。
退货商品数量、
退货商品金额、
拒收商品数量、
拒收商品金额、
最近一次退货时间-----分析用户拒收和退货习惯。

购物车表可以得到相关标签:

最近30天购物车次数、
最近30天购物车商品件数、
最近30天购物车提交商品件数、
最近30天购物车放弃件数、
最近30天购物车成功率------分析用户购物车使用习惯

订单表和用户表可以得到相关标签:

学校下单总数、
单位下单总数、
家里下单总数、
上午下单总数、
下午下单总数、
晚上下单总数----分析用户购物时间与地点习惯。

用户购买类目表

根据客户购买类目的情况提取客户标签,用于了解类目的购买人群情况和针对某一类目的营销等

主要数据来源:订单表、购物车表、类目维表

类目维表可以得到相关标签:

一级分类ID、
一级分类名称、
二级分类ID、
二级分类名称、
三级分类ID、
三级分类名称-----分析用户都购买了哪些类目。

订单表和类目维表可以得到相关标签:

近30天购买类目次数、
近30天购买类目金额、
近90天购买类目次数、
近90天购买类目金额、
近180天购买类目次数、
近180天购买类目金额、
累计购买类目次数、
累计购买类目金额----分析用户最近都购买了哪些类目。
最近一次购买类目时间、
最后一次购买类目距今天数----分析用户多久没有购买这个类目。

购物车表和类目维表可以得到相关标签:

近30天购物车类目次数、
近30天购物车类目金额、
近90天购物车类目次数、
近90天购物车类目金额----分析用户最近都挑中哪些类目。

用户访问信息表

根据客户访问的情况提取相关客户标签。

用于了解用户的访问总体情况,方便根据客户游览习惯做营销

主要数据来源:点击流日志行为表(PC/APP端)

点击流日志行为表可以得到相关标签:

最近一次APP/PC端访问日期、
最近一次APP/PC端访问使用操作系统、
最近一次APP/PC端访问使用游览器、
最近一次访问IP地址、
最近一次访问城市、
最近一次访问的省份-----分析用户最近一次访问情况。第一次APP/PC端访问日期、
第一次APP/PC端访问使用操作系统、
第一次APP/PC端访问使用游览器、
第一次访问IP地址、
第一次访问城市、
第一次访问的省份-----分析用户第一次访问情况。近7天APP/PC端访问次数、
近30天APP/PC访问次数、
近60天APP/PC端访问次数、
近90天APP/PC端访问次数、
近180天APP/PC端访问次数、
近365天APP/PC端访问次数----分析用户APP/PC端访问次数。近30天PC/APP端访问天数、
近30天PC/APP端访问并购买次数、
近30天PC/APP端访问PV、
近30天PC/APP端访问平均PV、
近30天PC/APP端最常用的游览器、
近30天PC/APP端不同IP数、
近30天PC/APP端最常用IP-----分析用户访问详情。近30天0-5点访问的次数、
近30天6-7点访问的次数、
近30天8-9点访问的次数、
近30天10-12点访问的次数、
近30天13-14点访问的次数、
近30天15-17点访问的次数、
近30天18-19点访问的次数、
近30天20-21点访问的次数、
近30天22-23点访问的次数----分析用户喜欢在哪个时间上网访问。

商品订单表

埋点日志表

存放用户访问app时点击相关控件的打点记录,通过客户端做埋点,从日志数据中解析出来

访问日志表

商品评论表

搜索日志表

用户收藏表、购物车信心表

环境准备

  • 1、搭建hadoop集群
  • 2、安装hive构建数据仓库
  • 3、安装spark集群
  • 4、sparksql 整合hive

数仓表的基本介绍

这里我们采用的是京东的数据仓库分层模式,是根据标准的模型演化而来。

数据仓库分层:BDM:缓冲数据,源数据的直接映像FDM:基础数据层,数据拉链处理、分区处理GDM:通用聚合ADM:高度聚合先把数据从源数据库中抽取加载到BDM层中,
然后FDM层根据BDM层的数据按天分区



用户画像表结构开发

这里采用脚本方式,也可以用spark SQL 直接创建并添加数据

上传数据到hdfshdfs dfs -mkdir -p /business/bdm_userhdfs dfs -put /export/servers/businessData/bdm_user.txt /business/bdm_user// load data inpath '/hivedatas/teacher.csv' into table teacher;
强制删除某个库drop database gdm cascade;
删除hdfs的目录hadoop fs -rm -r /business

客户消费订单表

订单表(bdm_order)

运行脚本
sh bdm_order.sh 2020-11-06
#***************************
#** 功能描述:bdm_order开发
#***************************
#!/bin/sh
yesterday=`date -d '-1 day' "+%Y-%m-%d"`
if [ $1 ];thenyesterday=$1
fi
# spark sql 链接
SPARK_SUBMIT_INFO="/export/servers/spark-2.2.0-bin-hadoop2.7/bin/spark-sql --master spark://node01:7077 --executor-memory 1g --total-executor-cores 2 --conf spark.sql.warehouse.dir=hdfs://node01:8020/user/hive/warehouse"
# 数据所在目录
SOURCE_DATA="/export/servers/businessData"
#订单主要信息表BDM层
SQl_BDM_ORDER="create database if not exists bdm;
create external table if not exists bdm.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   --订单更新时间
) partitioned by (dt string)
row format delimited fields terminated by ','
lines terminated by '\n'
location '/business/bdm_order';
alter table bdm.bdm_order add partition (dt='$yesterday');"##创建BDM_ORDER表
echo "${SQl_BDM_ORDER}"
${SPARK_SUBMIT_INFO} -e "${SQl_BDM_ORDER}"
##添加数据到BDM
hdfs dfs -put $SOURCE_DATA/bdm_order.txt /business/bdm_order/dt=$yesterday

订单明细表(bdm_order_des)

sh bdm_order_des.sh 2020-11-06
#***************************
#** 功能描述:bdm_order_desc开发
#***************************
#!/bin/sh
yesterday=`date -d '-1 day' "+%Y-%m-%d"`
if [ $1 ];thenyesterday=$1
fi
SPARK_SUBMIT_INFO="/export/servers/spark-2.2.0-bin-hadoop2.7/bin/spark-sql --master spark://node01:7077 --executor-memory 1g --total-executor-cores 2 --conf spark.sql.warehouse.dir=hdfs://node01:8020/user/hive/warehouse"
SOURCE_DATA="/export/servers/businessData"
SQL_BDM_ORDER_DESC="create database if not exists bdm;
create external table if not exists bdm.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 ','
location '/business/bdm_order_desc';
alter table bdm.bdm_order_desc add partition (dt='$yesterday');"##创建BDM_ORDER_DESC表
echo "${SQL_BDM_ORDER_DESC}"
$SPARK_SUBMIT_INFO -e "${SQL_BDM_ORDER_DESC}"
##添加数据到BDM_ORDER_DESC表
hdfs dfs -put $SOURCE_DATA/bdm_order_desc.txt /business/bdm_order_desc/dt=$yesterday

订单表(fdm_order)

sh fdm_order.sh 2020-11-06
#***************************
#** 功能描述:fdm_order开发
#***************************
#!/bin/sh
yesterday=`date -d '-1 day' "+%Y-%m-%d"`
if [ $1 ];thenyesterday=$1
fi
SPARK_SUBMIT_INFO="/export/servers/spark-2.2.0-bin-hadoop2.7/bin/spark-sql --master spark://node01:7077 --executor-memory 1g --total-executor-cores 2 --conf spark.sql.warehouse.dir=hdfs://node01:8020/user/hive/warehouse"
SOURCE_DATA="/export/servers/businessData"
SQL_FDM_ORDER="create database if not exists fdm;
create  table if not exists fdm.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);"
#加载数据
LOAD_DATA_FDM_ORDER="insert overwrite table fdm.fdm_order partition(dt='$yesterday')
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.bdm_order t where dt='$yesterday';"
##创建SQL_FDM_ORDER表
echo "${SQL_FDM_ORDER}"
$SPARK_SUBMIT_INFO -e "${SQL_FDM_ORDER}"
##导入数据到FDM
echo "${LOAD_DATA_FDM_ORDER}"
$SPARK_SUBMIT_INFO -e "${LOAD_DATA_FDM_ORDER}"

订单表明细表(fdm_order_desc)

sh fdm_order_desc.sh 2020-11-06
#***************************
#** 功能描述:fdm_order_desc开发
#***************************
#!/bin/sh
yesterday=`date -d '-1 day' "+%Y-%m-%d"`
if [ $1 ];thenyesterday=$1
fi
SPARK_SUBMIT_INFO="/export/servers/spark-2.2.0-bin-hadoop2.7/bin/spark-sql --master spark://node01:7077 --executor-memory 1g --total-executor-cores 2 --conf spark.sql.warehouse.dir=hdfs://node01:8020/user/hive/warehouse"
SOURCE_DATA="/export/servers/businessData"
SQL_FDM_ORDER_DESC="create database if not exists fdm;
create table if not exists fdm.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);"##加载数据
LOAD_FDM_ORDER_DESC="insert overwrite table fdm.fdm_order_desc partition(dt='$yesterday')
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.bdm_order_desc t where dt='$yesterday';"##创建FDM_ORDER_DESC表
echo "${SQL_FDM_ORDER_DESC}"
$SPARK_SUBMIT_INFO -e "${SQL_FDM_ORDER_DESC}"
##导入数据到FDM_ORDER_DESC
echo "${LOAD_FDM_ORDER_DESC}"
$SPARK_SUBMIT_INFO -e "${LOAD_FDM_ORDER_DESC}"

订单模型表(gdm_order)

sh gdm_order.sh 2020-11-06
#***************************
#** 功能描述:gdm_order开发
#***************************
#!/bin/sh
yesterday=`date -d '-1 day' "+%Y-%m-%d"`
if [ $1 ];thenyesterday=$1
fi
SPARK_SUBMIT_INFO="/export/servers/spark-2.2.0-bin-hadoop2.7/bin/spark-sql --master spark://node01:7077 --executor-memory 512m --total-executor-cores 1 --conf spark.sql.warehouse.dir=hdfs://node01:8020/user/hive/warehouse"
SOURCE_DATA="/export/servers/businessData"
##订单宽表模型表GDM
SQL_GDM_ORDER="create database if not exists gdm;
create external table if not exists gdm.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)
location '/business/gdm_order';"
#加载数据
LOAD_GDM_ORDER="insert overwrite table gdm.gdm_order partition(dt='$yesterday')
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.fdm_order where dt='$yesterday') a
join (select * from fdm.fdm_order_desc where dt='$yesterday') b on a.order_id=b.order_id;"##创建GDM_ORDER表
echo "${SQL_GDM_ORDER}"
$SPARK_SUBMIT_INFO -e "${SQL_GDM_ORDER}"
##导入数据到GDM_ORDER
echo "${LOAD_GDM_ORDER}"
$SPARK_SUBMIT_INFO -e "${LOAD_GDM_ORDER}"

订单地址模型表(gdm_user_order_addr_model)

sh gdm_user_order_addr_model.sh 2020-11-06
#***************************
#** 功能描述:gdm_user_order_addr_model开发
#***************************
#!/bin/sh
yesterday=`date -d '-1 day' "+%Y-%m-%d"`
if [ $1 ];thenyesterday=$1
fi
SPARK_SUBMIT_INFO="/export/servers/spark-2.2.0-bin-hadoop2.7/bin/spark-sql --master spark://node01:7077 --executor-memory 1g --total-executor-cores 2 --conf spark.sql.warehouse.dir=hdfs://node01:8020/user/hive/warehouse"
SOURCE_DATA="/export/servers/businessData"
#客户订单地址模型表
SQL_GDM_USER_ORDER_ADDR_MODEL="create database if not exists gdm;
create  table if not exists gdm.gdm_user_order_addr_model(
user_id string,--客户ID
order_addr bigint,--1表示学校、2表示单位、3表示家里
user_order_flag string-- 关联标识
)
row format delimited fields terminated by ','
lines terminated by '\n';
load data local inpath '/export/servers/businessData/gdm_user_order_addr_model.txt' overwrite into table gdm.gdm_user_order_addr_model;"##创建gdm_user_order_addr_model表
echo "${SQL_GDM_USER_ORDER_ADDR_MODEL}"
$SPARK_SUBMIT_INFO -e "${SQL_GDM_USER_ORDER_ADDR_MODEL}"

订单商品表(bdm_order_goods)

sh bdm_order_goods.sh 2020-11-06
#***************************
#** 功能描述:bdm_order_goods开发
#***************************
#!/bin/sh
yesterday=`date -d '-1 day' "+%Y-%m-%d"`
if [ $1 ];thenyesterday=$1
fi
SPARK_SUBMIT_INFO="/export/servers/spark-2.2.0-bin-hadoop2.7/bin/spark-sql --master spark://node01:7077 --executor-memory 1g --total-executor-cores 2 --conf spark.sql.warehouse.dir=hdfs://node01:8020/user/hive/warehouse"
SOURCE_DATA="/export/servers/businessData"
SQL_BDM_ORDER_GOODS="create database if not exists bdm;
create external table if not exists bdm.bdm_order_goods(
user_id string,--用户ID
order_id string,--订单ID
order_no string,--订单号
sku_id bigint,--SKU编号
sku_name string,--SKU名称
goods_id bigint,--商品编号
goods_no string,--商品货号
goods_sn string,--商品条码
goods_name string,--商品名称
goods_amount bigint,--商品数量
size_id bigint,--尺码编号
size_name string,--尺码名称
colour_id bigint,--颜色ID
shop_id string,--店铺编号
shop_name string,--店铺名称
curr_price double,--售卖价格
market_price double,--市场价格
discount double,--折扣比例
cost_price double,--成本价格
cost_type string,--成本类型
warehouse string,--所在仓库
first_cart bigint,-- 一级分类ID
first_cart_name string,-- 一级分类名称
second_cart bigint,-- 二级分类ID
second_cart_name string,-- 二级分类名称
third_cart bigint,-- 三级分类ID
third_cart_name string,-- 三级分类名称
dw_date timestamp
) partitioned by (dt string)
row format delimited fields terminated by ','
lines terminated by '\n'
location '/business/bdm_order_goods';
alter table bdm.bdm_order_goods add partition (dt='$yesterday');"
##创建SQL_FDM_ORDER表
echo "${SQL_BDM_ORDER_GOODS}"
$SPARK_SUBMIT_INFO -e "${SQL_BDM_ORDER_GOODS}"
##导入数据到BDM_ORDER_GOODS
hdfs dfs -put $SOURCE_DATA/bdm_order_goods.txt /business/bdm_order_goods/dt=$yesterday

订单商品表 (fdm_order_goods)

sh fdm_order_goods.sh 2020-11-06
#***************************
#** 功能描述:fdm_order_goods开发
#***************************
#!/bin/sh
yesterday=`date -d '-1 day' "+%Y-%m-%d"`
if [ $1 ];thenyesterday=$1
fi
SPARK_SUBMIT_INFO="/export/servers/spark-2.2.0-bin-hadoop2.7/bin/spark-sql --master spark://node01:7077 --executor-memory 512m --total-executor-cores 1 --conf spark.sql.warehouse.dir=hdfs://node01:8020/user/hive/warehouse"
SOURCE_DATA="/export/servers/businessData"
SQL_FDM_ORDER_GOODS="create database if not exists fdm;
create  table if not exists fdm.fdm_order_goods(
user_id  string,--用户ID
order_id string,--订单ID
order_no string,--订单号
sku_id bigint,--SKU编号
sku_name string,--SKU名称
goods_id bigint,--商品编号
goods_no string,--商品货号
goods_sn string,--商品条码
goods_name string,--商品名称
goods_amount bigint,--商品数量
size_id bigint,--尺码编号
size_name string,--尺码名称
colour_id bigint,--颜色ID
shop_id string,--店铺编号
shop_name string,--店铺名称
curr_price double,--售卖价格
market_price double,--市场价格
discount double,--折扣比例
cost_price double,--成本价格
cost_type string,--成本类型
warehouse string,--所在仓库
first_cart bigint,-- 一级分类ID
first_cart_name string,-- 一级分类名称
second_cart bigint,-- 二级分类ID
second_cart_name string,-- 二级分类名称
third_cart bigint,-- 三级分类ID
third_cat_name string,-- 三级分类名称
dw_date timestamp
) partitioned by (dt string);"
##加载数据
LOAD_FDM_ORDER_GOODS="insert overwrite table fdm.fdm_order_goods partition(dt='$yesterday')
select
t.user_id,--用户ID
t.order_id,--订单ID
t.order_no,--订单号
t.sku_id,--SKU编号
t.sku_name,--SKU名称
t.goods_id,--商品编号
t.goods_no,--商品货号
t.goods_sn,--商品条码
t.goods_name,--商品名称
t.goods_amount,--商品数量
t.size_id,--尺码编号
t.size_name,--尺码名称
t.colour_id,--颜色ID
t.shop_id,--店铺编号
t.shop_name,--店铺名称
t.curr_price,--售卖价格
t.market_price,--市场价格
t.discount,--折扣比例
t.cost_price,--成本价格
t.cost_type,--成本类型
t.warehouse,--所在仓库
t.first_cart,-- 一级分类ID
t.first_cart_name,-- 一级分类名称
t.second_cart,-- 二级分类ID
t.second_cart_name,-- 二级分类名称
t.third_cart,-- 三级分类ID
t.third_cart_name,-- 三级分类名称
from_unixtime(unix_timestamp()) dw_date
from bdm.bdm_order_goods t where dt='$yesterday';"
##创建FDM_ORDER_GOODS表
echo "${SQL_FDM_ORDER_GOODS}"
$SPARK_SUBMIT_INFO -e "${SQL_FDM_ORDER_GOODS}"
##导入数据到FDM_ORDER_GOODS
$SPARK_SUBMIT_INFO -e "${LOAD_FDM_ORDER_GOODS}"

客户消费订单模型表-临时表01(gdm.gdm_user_consume_order_temp_01

sh gdm_user_consume_order_temp_01.sh 2020-11-06
#***************************
#** 功能描述:gdm_user_consume_order_temp_01开发
#***************************
#!/bin/sh
yesterday=`date -d '-1 day' "+%Y-%m-%d"`
if [ $1 ];thenyesterday=$1
fi
SPARK_SUBMIT_INFO="/export/servers/spark-2.2.0-bin-hadoop2.7/bin/spark-sql --master spark://node01:7077 --executor-memory 512m --total-executor-cores 1 --conf spark.sql.warehouse.dir=hdfs://node01:8020/user/hive/warehouse"
SOURCE_DATA="/export/servers/businessData"
##客户消费订单模型表-临时表01
SQL_GDM_USER_CONSUME_ORDER_TEMP_01="drop table if exists gdm.gdm_user_consume_order_temp_01;
CREATE TABLE gdm.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), '$yesterday') first_order_ago,--首单距今时间DATEDIFF(MAX(order_date), '$yesterday') last_order_ago,--尾单距今时间SUM(CASEWHEN t.dat_30 = 1 AND t.order_flag = 0 THEN 1 else 0END) month1_hg_order_cnt,--近30天购买次数(不含退拒)SUM(CASEWHEN t.dat_30 = 1 AND t.order_flag = 0 THEN t.order_money else 0END) month1_hg_order_amt,--近30天购买金额(不含退拒)SUM(CASEWHEN t.dat_60 = 1 AND t.order_flag = 0 THEN 1 else 0END) month2_hg_order_cnt,--近60天购买次数(不含退拒)SUM(CASEWHEN t.dat_60 = 1 AND t.order_flag = 0 THEN t.order_money else 0END) month2_hg_order_amt,--近60天购买金额(不含退拒)SUM(CASEWHEN t.dat_90 = 1 AND t.order_flag = 0 THEN 1 else 0END) month3_hg_order_cnt,--近90天购买次数(不含退拒)SUM(CASEWHEN t.dat_90 = 1 AND t.order_flag = 0 THEN t.order_money else 0END) month3_hg_order_amt,--近90天购买金额(不含退拒)SUM(dat_30) month1_order_cnt,--近30天购买次数(含退拒)SUM(CASEWHEN t.dat_30 = 1 THEN t.order_money else 0END) month1_order_amt,--近30天购买金额(含退拒)SUM(dat_60) month2_order_cnt,--近60天购买次数(含退拒)SUM(CASEWHEN t.dat_60 = 1 THEN t.order_money else 0END) month2_order_amt,--近60天购买金额(含退拒)SUM(dat_90) month3_order_cnt,--近90天购买次数(含退拒)SUM(CASEWHEN t.dat_90 = 1 THEN t.order_money else 0END) month3_order_amt,--近90天购买金额(含退拒)MAX(t.order_money) max_order_amt,--最大消费金额MIN(t.order_money) min_order_amt,--最小消费金额SUM(CASEWHEN t.order_flag = 0 THEN 1else 0END) total_order_cnt,--累计消费次数(不含退拒)SUM(CASEWHEN t.order_flag = 0 THEN t.order_money else 0END) 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(CASEWHEN t.order_status = 3 THEN t1.goods_amount else 0END) return_cnt,--退货商品数量SUM(CASEWHEN t.order_status = 3 THEN t.order_money else 0END) return_amt,--退货商品金额SUM(CASEWHEN t.order_status = 4 THEN t1.goods_amount else 0END) reject_cnt,--拒收商品数量SUM(CASEWHEN t.order_status = 4 THEN t.order_money else 0END) reject_amt,--拒收商品金额MAX(CASEWHEN t.order_status = 3 THEN t.order_date END) last_return_time,--最近一次退货时间SUM(CASEWHEN t2.order_addr = 1 THEN 1 else 0END) school_order_cnt,--学校下单总数SUM(CASEWHEN t2.order_addr = 2 THEN 1 else 0END) company_order_cnt,--单位下单总数SUM(CASEWHEN t2.order_addr = 3 THEN 1 else 0END) home_order_cnt,--家里下单总数SUM(CASEWHEN t.order_hour >= 8 AND t.order_hour <= 11 THEN 1 else 0END) forenoon_order_cnt,--上午下单总数SUM(CASEWHEN t.order_hour >= 12 AND t.order_hour <= 18 THEN 1 else 0END) afternoon_order_cnt,--下午下单总数SUM(CASEWHEN t.order_hour >= 19 AND t.order_hour <= 22 THEN 1 else 0END) night_order_cnt,--晚上下单总数SUM(CASEWHEN t.order_hour >= 23 or t.order_hour <= 7 THEN 1 else 0END) morning_order_cnt --凌晨下单总数
FROM(SELECT a.*,(CASEWHEN order_date >= DATE_SUB('$yesterday', 29) AND order_date <= '$yesterday' THEN 1 else 0END) dat_30,(CASEWHEN order_date >= DATE_SUB('$yesterday', 59) AND order_date <= '$yesterday' THEN 1 else 0END) dat_60,(CASEWHEN order_date >= DATE_SUB('$yesterday', 89) AND order_date <= '$yesterday' THEN 1 else 0END) dat_90,(CASEWHEN a.order_status IN (3, 4) THEN 1 ELSE 0 END) order_flag,--退货与拒收标示HOUR(order_date) order_hour  --2018-05-19 13:20:20FROMgdm.gdm_order a WHERE dt = '$yesterday') t LEFT JOIN (SELECT order_id,goods_amount FROMfdm.fdm_order_goods) t1ON (t.order_id = t1.order_id) LEFT JOIN (SELECT user_id,order_addr FROMgdm.gdm_user_order_addr_model) t2 ON (t.user_id = t2.user_id)
GROUP BY t.user_id ;"##创建SQL_GDM_USER_CONSUME_ORDER_TEMP_01表
echo "${SQL_GDM_USER_CONSUME_ORDER_TEMP_01}"
$SPARK_SUBMIT_INFO -e "${SQL_GDM_USER_CONSUME_ORDER_TEMP_01}"

购物车表(bdm_order_cart)

sh bdm_order_cart.sh 2020-11-06
#***************************
#** 功能描述:SQL_BDM_ORDER_CART开发
#***************************
#!/bin/sh
yesterday=`date -d '-1 day' "+%Y-%m-%d"`
if [ $1 ];thenyesterday=$1
fi
SPARK_SUBMIT_INFO="/export/servers/spark-2.2.0-bin-hadoop2.7/bin/spark-sql --master spark://node01:7077 --executor-memory 1g --total-executor-cores 2 --conf spark.sql.warehouse.dir=hdfs://node01:8020/user/hive/warehouse"
SOURCE_DATA="/export/servers/businessData"
#SQL_BDM_ORDER_CART表
SQL_BDM_ORDER_CART="create database if not exists bdm;
create external table if not exists bdm.bdm_order_cart(
id bigint,--ID
session_id string,--sessionID
user_id string,--用户ID
goods_id string,--商品ID
goods_num bigint,--商品数量
add_time string,--商品加入时间
cancle_time string,--商品取消时间
sumbit_time string,--商品提交时间
dw_date timestamp
) partitioned by (dt string)
row format delimited fields terminated by ','
lines terminated by '\n'
location '/business/bdm_order_cart';
alter table bdm.bdm_order_cart add partition (dt='$yesterday');"##创建SQL_BDM_ORDER_CART表
echo "${SQL_BDM_ORDER_CART}"
$SPARK_SUBMIT_INFO -e "${SQL_BDM_ORDER_CART}"
##添加数据到SQL_BDM_ORDER_CART
hdfs dfs -put $SOURCE_DATA/bdm_order_cart.txt /business/bdm_order_cart/dt=$yesterday

购物车表(fdm_order_cart)

sh fdm_order_cart.sh 2020-11-06
#***************************
#** 功能描述:fdm_order_cart开发
#***************************
#!/bin/sh
yesterday=`date -d '-1 day' "+%Y-%m-%d"`
if [ $1 ];thenyesterday=$1
fi
SPARK_SUBMIT_INFO="/export/servers/spark-2.2.0-bin-hadoop2.7/bin/spark-sql --master spark://node01:7077 --executor-memory 512m --total-executor-cores 1 --conf spark.sql.warehouse.dir=hdfs://node01:8020/user/hive/warehouse"
SOURCE_DATA="/export/servers/businessData"
SQL_FDM_ORDER_CART="create database if not exists fdm;
create  table  if not exists fdm.fdm_order_cart(
id bigint,--ID
session_id string,--sessionID
user_id string,--用户ID
goods_id string,--商品ID
goods_num bigint,--商品数量
add_time string,--商品加入时间
cancle_time string,--商品取消时间
sumbit_time string,--商品提交时间
dw_date timestamp
) partitioned by (dt string);"
#加载数据
LOAD_FDM_ORDER_CART="insert overwrite table fdm.fdm_order_cart partition(dt='$yesterday')
select
t.id,--ID
t.session_id,--sessionID
t.user_id,--用户ID
t.goods_id,--商品ID
t.goods_num ,--商品数量
t.add_time,--商品加入时间
t.cancle_time,--商品取消时间
t.sumbit_time,--商品提交时间
from_unixtime(unix_timestamp())  dw_date
from bdm.bdm_order_cart t where dt='$yesterday';"
##创建SQL_FDM_ORDER表
echo "${SQL_FDM_ORDER_CART}"
$SPARK_SUBMIT_INFO -e "${SQL_FDM_ORDER_CART}"
##导入数据到FDM
echo "${LOAD_FDM_ORDER_CART}"
$SPARK_SUBMIT_INFO -e "${LOAD_FDM_ORDER_CART}"

购物车临时模型表–临时表 02(gdm.gdm_user_consume_order_temp_02

sh gdm_user_consume_order_temp_02.sh 2020-11-06
#***************************
#** 功能描述:GDM_USER_CONSUME_ORDER_TEMP_02开发
#***************************
#!/bin/sh
yesterday=`date -d '-1 day' "+%Y-%m-%d"`
if [ $1 ];thenyesterday=$1
fi
SPARK_SUBMIT_INFO="/export/servers/spark-2.2.0-bin-hadoop2.7/bin/spark-sql --master spark://node01:7077 --executor-memory 512m --total-executor-cores 1 --conf spark.sql.warehouse.dir=hdfs://node01:8020/user/hive/warehouse"
SOURCE_DATA="/export/servers/businessData"
##gdm_user_consume_order_temp_02
SQL_GDM_USER_CONSUME_ORDER_TEMP_02="drop table if exists gdm.gdm_user_consume_order_temp_02;
CREATE TABLE gdm.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(CASEWHEN sumbit_time <> '' THEN goods_num ELSE 0 END) month1_cart_submit_cnt,--最近30天购物车提交商品件数SUM(CASEWHEN sumbit_time <> '' THEN goods_num ELSE 0 END)/SUM(goods_num) month1_cart_rate,--最近30天购物车成功率SUM(CASEWHEN cancle_time <> '' THEN goods_num ELSE 0 END) month1_cart_cancle_cnt  --最近30天购物车放弃件数
FROMfdm.fdm_order_cart
WHERE dt = '$yesterday' AND to_date (add_time) >= DATE_SUB('$yesterday', 29) AND to_date (add_time) <= '$yesterday'
GROUP BY user_id ;"##创建GDM_USER_CONSUME_ORDER_TEMP_02表
echo "${SQL_GDM_USER_CONSUME_ORDER_TEMP_02}"
$SPARK_SUBMIT_INFO -e "${SQL_GDM_USER_CONSUME_ORDER_TEMP_02}"

常用地址和常用支付方式-临时表 03(gdm_user_consume_order_temp_03)

sh gdm_user_consume_order_temp_03.sh 2020-11-06
#***************************
#** 功能描述:gdm_user_consume_order_temp_03开发
#***************************
#!/bin/sh
yesterday=`date -d '-1 day' "+%Y-%m-%d"`
if [ $1 ];thenyesterday=$1
fi
SPARK_SUBMIT_INFO="/export/servers/spark-2.2.0-bin-hadoop2.7/bin/spark-sql --master spark://node01:7077 --executor-memory 512m --total-executor-cores 1 --conf spark.sql.warehouse.dir=hdfs://node01:8020/user/hive/warehouse"
SOURCE_DATA="/export/servers/businessData"
#购物车临时模型表---常用地址和常用支付方式-临时表03
SQL_GDM_USER_CONSUME_ORDER_TEMP_03="drop table if exists gdm.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 fromgdm.gdm_order a where dt = '$yesterday' group by a.user_id,concat(coalesce(area_name, ''),coalesce(address, '')) unionall select a.user_id,a.pay_type con,'pay_type' type,count(1) cnt fromgdm.gdm_order a where dt = '$yesterday' group by a.user_id,a.pay_type) b) t where t.rn = 1;"##创建gdm_user_consume_order_temp_03表
echo "${SQL_GDM_USER_CONSUME_ORDER_TEMP_03}"
$SPARK_SUBMIT_INFO -e "${SQL_GDM_USER_CONSUME_ORDER_TEMP_03}"

购物车表和订单表整合(gdm_user_consume_order_temp_100)

sh gdm_user_consume_order_temp_100.sh 2020-11-06
#***************************
#** 功能描述:gdm_user_consume_order_temp_100开发
#***************************
#!/bin/sh
yesterday=`date -d '-1 day' "+%Y-%m-%d"`
if [ $1 ];thenyesterday=$1
fi
SPARK_SUBMIT_INFO="/export/servers/spark-2.2.0-bin-hadoop2.7/bin/spark-sql --master spark://node01:7077 --executor-memory 512m --total-executor-cores 1 --conf spark.sql.warehouse.dir=hdfs://node01:8020/user/hive/warehouse"
SOURCE_DATA="/export/servers/businessData"
##购物车表和订单表整合
SQL_GDM_USER_CONSUME_ORDER_TEMP_100="drop table if exists gdm.gdm_user_consume_order_temp_100;
create table gdm.gdm_user_consume_order_temp_100 as
select a.user_id
from(select user_id fromgdm.gdm_user_consume_order_temp_01 unionall select user_id fromgdm.gdm_user_consume_order_temp_02) a
group by a.user_id ;"##创建gdm_user_consume_order_temp_100表
echo "${SQL_GDM_USER_CONSUME_ORDER_TEMP_100}"
$SPARK_SUBMIT_INFO -e "${SQL_GDM_USER_CONSUME_ORDER_TEMP_100}"

客户消费订单表 ***(gdm_user_consume_order)

sh gdm_user_consume_order.sh 2020-11-06
#***************************
#** 功能描述:gdm_order开发
#***************************
#!/bin/sh
yesterday=`date -d '-1 day' "+%Y-%m-%d"`
if [ $1 ];thenyesterday=$1
fi
SPARK_SUBMIT_INFO="/export/servers/spark-2.2.0-bin-hadoop2.7/bin/spark-sql --master spark://node01:7077 --executor-memory 512m --total-executor-cores 1 --conf spark.sql.warehouse.dir=hdfs://node01:8020/user/hive/warehouse"
SOURCE_DATA="/export/servers/businessData"#gdm_user_consume_order
SQL_GDM_USER_CONSUME_ORDER="create database if not exists gdm;
create  table if not exists gdm.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);"
#加载数据
LOAD_GDM_USER_CONSUME_ORDER="INSERT overwrite TABLE gdm.gdm_user_consume_order PARTITION (dt = '$yesterday')
SELECT t.user_id,            --客户IDt1.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,        --客单价(含退拒)(CASEWHEN 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天购物车提交商品件数(CASEWHEN 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.gdm_user_consume_order_temp_100 t LEFT JOIN gdm.gdm_user_consume_order_temp_01 t1 ON (t.user_id = t1.user_id) LEFT JOIN gdm.gdm_user_consume_order_temp_02 t2 ON (t.user_id = t2.user_id) LEFT JOIN (SELECT user_id,MAX(CASEWHEN type = 'address' THEN con END) common_address,MAX(CASEWHEN type = 'pay_type' THEN con END) common_paytype FROMgdm.gdm_user_consume_order_temp_03 group by user_id) t3 ON (t.user_id = t3.user_id);"##创建GDM_USER_CONSUME_ORDER表
echo "${SQL_GDM_USER_CONSUME_ORDER}"
$SPARK_SUBMIT_INFO -e "${SQL_GDM_USER_CONSUME_ORDER}"
##导入数据到GDM_USER_CONSUME_ORDER
echo "${LOAD_GDM_USER_CONSUME_ORDER}"
$SPARK_SUBMIT_INFO -e "${LOAD_GDM_USER_CONSUME_ORDER}"

客户购买类目表

计算订单中客户购买情况(gdm_user_buy_category_temp)

sh gdm_user_buy_category_temp.sh 2020-11-06
#***************************
#** 功能描述:GDM_USER_CART_CATEGORY_TEMP开发
#***************************
#!/bin/sh
yesterday=`date -d '-1 day' "+%Y-%m-%d"`
if [ $1 ];thenyesterday=$1
fi
SPARK_SUBMIT_INFO="/export/servers/spark-2.2.0-bin-hadoop2.7/bin/spark-sql --master spark://node01:7077 --executor-memory 512m --total-executor-cores 1 --conf spark.sql.warehouse.dir=hdfs://node01:8020/user/hive/warehouse"
SOURCE_DATA="/export/servers/businessData"
##订单宽表模型表GDM
SQL_GDM_USER_BUY_CATEGORY_TEMP="drop table if exists gdm.gdm_user_buy_category_temp;
CREATE TABLE gdm.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(CASEWHEN a.dat_30 = 1 THEN b.goods_amount else 0END) month1_category_cnt, --近30天购物类目次数SUM(CASEWHEN a.dat_30 = 1 THEN COALESCE(b.curr_price,0)* COALESCE(b.goods_amount,0)else 0END) month1_category_amt, --近30天购物类目金额SUM(CASEWHEN a.dat_90 = 1 THEN b.goods_amount else 0END) month3_category_cnt,--近90天购物类目次数SUM(CASEWHEN a.dat_90 = 1 THEN COALESCE(b.curr_price, 0)* COALESCE(b.goods_amount, 0)else 0END) month3_category_amt,--近90天购物类目金额SUM(CASEWHEN a.dat_180 = 1 THEN b.goods_amount else 0END) month6_category_cnt,--近180天购物类目次数SUM(CASEWHEN a.dat_180 = 1 THEN COALESCE(b.curr_price, 0) * COALESCE(b.goods_amount, 0)else 0END) month6_category_amt,--近180天购物类目金额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(MAX(a.order_date), '$yesterday') last_category_ago,--最后一次购买类目距今天数FROM_UNIXTIME(UNIX_TIMESTAMP()) dw_date
FROM(SELECT a.*,(CASEWHEN order_date >= DATE_SUB('$yesterday', 29) AND order_date <= '$yesterday' THEN1END) dat_30, --近30天标识(CASEWHEN order_date >= DATE_SUB('$yesterday', 89) AND order_date <= '$yesterday' THEN 1END) dat_90,--近90天标识(CASEWHEN order_date >= DATE_SUB('$yesterday', 179) AND order_date <= '$yesterday' THEN 1END) dat_180  --近180天标识FROMfdm.fdm_order a WHERE dt = '$yesterday') a JOIN (SELECT * FROMfdm.fdm_order_goods WHERE dt = '$yesterday') b ON (a.user_id = b.user_id)
GROUP BY a.user_id,b.third_cart,b.third_cat_name;"##创建SQL_GDM_USER_BUY_CATEGORY_TEMP表
echo "${SQL_GDM_USER_BUY_CATEGORY_TEMP}"
$SPARK_SUBMIT_INFO -e "${SQL_GDM_USER_BUY_CATEGORY_TEMP}"

购物车中类目情况(gdm_user_cart_category_temp)

sh gdm_user_cart_category_temp.sh 2020-11-06
#***************************
#** 功能描述:GDM_USER_CART_CATEGORY_TEMP开发
#***************************
#!/bin/sh
yesterday=`date -d '-1 day' "+%Y-%m-%d"`
if [ $1 ];thenyesterday=$1
fi
SPARK_SUBMIT_INFO="/export/servers/spark-2.2.0-bin-hadoop2.7/bin/spark-sql --master spark://node01:7077 --executor-memory 512m --total-executor-cores 1 --conf spark.sql.warehouse.dir=hdfs://node01:8020/user/hive/warehouse"
SOURCE_DATA="/export/servers/businessData"
#购物车中的类目情况
SQl_GDM_USER_CART_CATEGORY_TEMP="drop table if exists gdm.gdm_user_cart_category_temp;
create table gdm.gdm_user_cart_category_temp as
select a.user_id,b.third_cart,sum(casewhen to_date (add_time) >= date_sub('$yesterday', 29) and to_date (add_time) <= '$yesterday' then 1 else 0end) month1_cart_category_cnt, --近30天的标识sum(casewhen to_date (add_time) >= date_sub('$yesterday', 89) and to_date (add_time) <= '$yesterday' then 1 else 0end) month3_cart_category_cnt,  --近90天的标识sum(casewhen to_date (add_time) >= date_sub('$yesterday', 179) and to_date (add_time) <= '$yesterday' then 1 else 0end) month6_cart_category_cnt, --近180天的标识count(1) total_category_cnt  --累计购物车类目次数
from(select * fromfdm.fdm_order_cart where dt = '$yesterday' and to_date (add_time) >= date_sub('$yesterday', 179) and to_date (add_time) <= '$yesterday') a left join (select goods_id,third_cart fromfdm.fdm_order_goods where dt = '$yesterday' group by goods_id,third_cart) b on (a.goods_id = b.goods_id)
group by user_id,b.third_cart ;"##创建GDM_USER_CART_CATEGORY_TEMP表
echo "${SQl_GDM_USER_CART_CATEGORY_TEMP}"
${SPARK_SUBMIT_INFO} -e "${SQl_GDM_USER_CART_CATEGORY_TEMP}"

类目表(gdm_category_code)

sh gdm_category_code.sh 2020-11-06
#***************************
#** 功能描述:GDM_CATEGORY_CODE开发
#***************************
#!/bin/sh
yesterday=`date -d '-1 day' "+%Y-%m-%d"`
if [ $1 ];thenyesterday=$1
fi
SPARK_SUBMIT_INFO="/export/servers/spark-2.2.0-bin-hadoop2.7/bin/spark-sql --master spark://node01:7077 --executor-memory 512m --total-executor-cores 1 --conf spark.sql.warehouse.dir=hdfs://node01:8020/user/hive/warehouse"
SOURCE_DATA="/export/servers/businessData"
##订单宽表模型表GDM
SQL_GDM_CATEGORY_CODE="create database if not exists gdm;
create external table if not exists gdm.gdm_category_code(
third_category_id bigint,--三级分类ID
third_category_name string,--三级分类名称
second_category_id bigint,--二级分类ID
second_catery_name string,--二级分类名称
first_category_id bigint,--一级分类ID
first_category_name string --一级分类名称
)row format delimited fields terminated by ','
lines terminated by '\n'
location  '/business/gdm_category_code';
load data local inpath '/export/servers/businessData/gdm_category_code.txt' overwrite into table gdm.gdm_category_code;"##创建GDM_CATEGORY_CODE表
echo "${SQL_GDM_CATEGORY_CODE}"
$SPARK_SUBMIT_INFO -e "${SQL_GDM_CATEGORY_CODE}"

整合购买情况和类目情况(gdm_user_category_total)

sh gdm_user_category_total.sh 2020-11-06
#***************************
#** 功能描述:GDM_USER_CATEGORY_TOTAL开发
#***************************
#!/bin/sh
yesterday=`date -d '-1 day' "+%Y-%m-%d"`
if [ $1 ];thenyesterday=$1
fi
SPARK_SUBMIT_INFO="/export/servers/spark-2.2.0-bin-hadoop2.7/bin/spark-sql --master spark://node01:7077 --executor-memory 512m --total-executor-cores 1 --conf spark.sql.warehouse.dir=hdfs://node01:8020/user/hive/warehouse"
SOURCE_DATA="/export/servers/businessData"
##订单宽表模型表GDM
SQL_GDM_USER_CATEGORY_TOTAL="drop table if exists gdm.gdm_user_category_total;
create table gdm.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 fromgdm.gdm_user_buy_category_temp unionall select user_id,third_cart fromgdm.gdm_user_cart_category_temp) a left join gdm.gdm_category_code bon (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 ;"##创建gdm_user_buy_category_temp表
echo "${SQL_GDM_USER_CATEGORY_TOTAL}"
$SPARK_SUBMIT_INFO -e "${SQL_GDM_USER_CATEGORY_TOTAL}"

spark sql 的 shuffle默认并行度是200

客户购买类目表***(gdm_user_buy_category)

sh gdm_user_buy_category.sh 2020-11-06
#***************************
#** 功能描述:GDM_USER_BUY_CATEGORY开发
#***************************
#!/bin/sh
yesterday=`date -d '-1 day' "+%Y-%m-%d"`
if [ $1 ];thenyesterday=$1
fi
SPARK_SUBMIT_INFO="/export/servers/spark-2.2.0-bin-hadoop2.7/bin/spark-sql --master spark://node01:7077 --executor-memory 512m --total-executor-cores 1 --conf spark.sql.warehouse.dir=hdfs://node01:8020/user/hive/warehouse"
SOURCE_DATA="/export/servers/businessData"
#订单主要信息表FDM层
SQl_GDM_USER_BUY_CATEGORY="create database if not exists gdm;
CREATE  TABLE if not exists gdm.gdm_user_buy_category (user_id STRING,                 --客户IDfirst_category_id BIGINT,       --一级分类IDfirst_category_name STRING,     --一级分类名称second_category_id BIGINT,      --二级分类IDsecond_catery_name STRING,      --二级分类名称third_category_id BIGINT,       --三级分类IDthird_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,       --最后一次购买类目距今天数dw_date TIMESTAMP
) partitioned BY (dt STRING) ;
"
#加载数据
LOAD_GDM_USER_BUY_CATEGORY="INSERT overwrite TABLE gdm.gdm_user_buy_category PARTITION (dt = '$yesterday')
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_cart_category_cnt,t2.month3_cart_category_cnt,t2.month6_cart_category_cnt,t2.total_category_cnt,t1.last_category_time,t1.last_category_ago,FROM_UNIXTIME(UNIX_TIMESTAMP()) dw_date
FROMgdm.gdm_user_category_total t LEFT JOIN gdm.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.gdm_user_cart_category_temp t2 ON (t.user_id = t2.user_id AND t.third_category_id = t2.third_cart) ;"
##创建GDM_USER_BUY_CATEGORY表
echo "${SQl_GDM_USER_BUY_CATEGORY}"
${SPARK_SUBMIT_INFO} -e "${SQl_GDM_USER_BUY_CATEGORY}"
##加载数据
echo "${LOAD_GDM_USER_BUY_CATEGORY}"
${SPARK_SUBMIT_INFO} -e "${LOAD_GDM_USER_BUY_CATEGORY}"

客户基本属性表

这里统一执行后面的shell脚本

vim userall.sh
sh /export/servers/businessData/shells/user/bdm_user.sh
sh /export/servers/businessData/shells/user/fdm_user.sh
sh /export/servers/businessData/shells/user/gdm_user_basic.sh
sh userall.sh 2020-11-06

用户表(bdm_user)

#***************************
#** 功能描述:bdm_user开发
#***************************
#!/bin/sh
yesterday=`date -d '-1 day' "+%Y-%m-%d"`
if [ $1 ];thenyesterday=$1
fi
SPARK_SUBMIT_INFO="/export/servers/spark-2.2.0-bin-hadoop2.7/bin/spark-sql --master spark://node01:7077 --executor-memory 512m --total-executor-cores 1 --conf spark.sql.warehouse.dir=hdfs://node01:8020/user/hive/warehouse"
SOURCE_DATA="/export/servers/businessData"
#订单主要信息表BDM层
SQl_BDM_USER="create database if not exists bdm;create external table bdm.bdm_user(
user_id string,
user_name string,
user_sex string,
user_birthday string,
user_age int,
constellation string,
province string,
city string,
city_level string,
hex_mail string,
op_mail string,
hex_phone string,
op_phone string,
fore_phone string,
add_time string,
login_ip string,
login_source string,
request_user string,
total_mark bigint,
used_mark bigint,
level_name string,
blacklist int,
is_married int,
educattion string,
monthly_money double,
profession string)
partitioned by(dt string)
row format delimited fields terminated by ',' location '/business/bdm_user';
alter table bdm.bdm_user add partition (dt='$yesterday');
load data local inpath '/export/servers/businessData/bdm_user.txt' into table bdm.bdm_user partition(dt='$yesterday');"##创建BDM_ORDER表
echo "${SQl_BDM_USER}"
${SPARK_SUBMIT_INFO} -e "${SQl_BDM_USER}"

用户宽表 (fdm_user)

#***************************
#** 功能描述:bdm_user开发
#***************************
#!/bin/sh
yesterday=`date -d '-1 day' "+%Y-%m-%d"`
if [ $1 ];thenyesterday=$1
fi
SPARK_SUBMIT_INFO="/export/servers/spark-2.2.0-bin-hadoop2.7/bin/spark-sql --master spark://node01:7077 --executor-memory 512m --total-executor-cores 1 --conf spark.sql.warehouse.dir=hdfs://node01:8020/user/hive/warehouse"
SOURCE_DATA="/export/servers/businessData"
#订单主要信息表FDM层
SQl_FDM_USER="create database if not exists fdm;
create external table fdm.fdm_user(
user_id string,
user_name string,
user_sex string,
user_birthday string,
user_age int,
constellation string,
province string,
city string,
city_level string,
hex_mail string,
op_mail string,
hex_phone string,
op_phone string,
fore_phone string,
add_time string,
login_ip string,
login_source string,
requste_user string,
total_mark int,
used_mark int,
level_name string,
blacklist int,
is_married int,
education string,
monthly_money double,
profession string,
dw_date timestamp)
partitioned by (dt string)
row format delimited fields terminated by ','
location '/business/fdm_user';"
#加载数据
LOAD_FDM_USER="insert overwrite table fdm.fdm_user 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.op_phone ,
t.fore_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.educattion ,
t.monthly_money,
t.profession,
from_unixtime(unix_timestamp())  dw_date
from bdm.bdm_user t where dt='$yesterday';"
##创建FDM_ORDER表
echo "${SQl_FDM_USER}"
${SPARK_SUBMIT_INFO} -e "${SQl_FDM_USER}"
##加载数据
echo "${LOAD_FDM_USER}"
${SPARK_SUBMIT_INFO} -e "${LOAD_FDM_USER}"

客户基本属性表 *** (gdm_user_basic)

#***************************
#** 功能描述:bdm_user开发
#***************************
#!/bin/sh
yesterday=`date -d '-1 day' "+%Y-%m-%d"`
if [ $1 ];thenyesterday=$1
fi
SPARK_SUBMIT_INFO="/export/servers/spark-2.2.0-bin-hadoop2.7/bin/spark-sql --master spark://node01:7077 --executor-memory 512m --total-executor-cores 1 --conf spark.sql.warehouse.dir=hdfs://node01:8020/user/hive/warehouse"
SOURCE_DATA="/export/servers/businessData"
#订单主要信息表FDM层
SQl_GDM_USER_BASIC="create database if not exists gdm;
create  table gdm.gdm_user_basic(
user_id string,
user_name string,
user_sex string,
user_birthday string,
user_age int,
constellation string,
province string,
city string,
city_level string,
hex_mail string,
op_mail string,
hex_phone string,
op_phone string,
fore_phone string,
add_time string,
login_ip string,
login_source string,
request_user string,
total_mark int,
used_mark int,
level_name string,
blacklist int,
is_married int,
education string,
monthly_money double,
profession string,
sex_model int,
is_pregnant_woman int,
is_have_children int,
children_age int,
children_age_rate double,
children_sex string,
children_sex_rate double,
is_have_car int,
potential_car_user_rate double,
phone_brand string,
phone_brand_level string,
phone_cnt int,
change_phone_rate int,
majia_flag string,
majia_account_cnt int,
loyal_model int,
shopping_type_model int,
figure_model int,
stature_model int,
dw_date timestamp)
partitioned by (dt string)
row format delimited fields terminated by ','
location '/business/gdm_user_basic';"
#加载数据
LOAD_GDM_USER_BASIC="insert into table gdm.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.op_phone ,
t.fore_phone ,
t.add_time ,
t.login_ip ,
t.login_source ,
t.requste_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_wowan ,
null is_have_children ,
null children_age ,
null children_age_rate ,
null children_sex ,
null children_sex_rate ,
null is_have_car ,
null potential_car_user_rate ,
null phone_brand ,
null phone_brand_level ,
null phone_cnt ,
null phone_change_rate ,
null majia_flag ,
null majia_account_cnt ,
null loyal_model ,
null shopping_type_model ,
null figure_model ,
null stature_model ,
from_unixtime(unix_timestamp())  dw_date
from fdm.fdm_user t where dt='$yesterday';"
##创建BDM_ORDER表
echo "${SQl_GDM_USER_BASIC}"
${SPARK_SUBMIT_INFO} -e "${SQl_GDM_USER_BASIC}"
##加载数据
echo "${LOAD_GDM_USER_BASIC}"
${SPARK_SUBMIT_INFO} -e "${LOAD_GDM_USER_BASIC}"

客户访问信息表

这里统一执行后面的shell脚本

vim uservisitall.sh
sh /export/servers/businessData/shells/uservisit/bdm_user_app_click_log.sh
sh /export/servers/businessData/shells/uservisit/bdm_user_pc_click_log.sh
sh /export/servers/businessData/shells/uservisit/fdm_user_app_pageview.sh
sh /export/servers/businessData/shells/uservisit/fdm_user_pc_pageview.sh
sh /export/servers/businessData/shells/uservisit/gdm_user_visit_temp_01.sh
sh /export/servers/businessData/shells/uservisit/gdm_user_visit.sh
sh uservisitall.sh 2020-11-06

用户上网记录表(PC) (bdm_user_pc_click_log)

#***************************
#** 功能描述:bdm_user_pc_click_log开发
#***************************
#!/bin/sh
yesterday=`date -d '-1 day' "+%Y-%m-%d"`
if [ $1 ];thenyesterday=$1
fi
SPARK_SUBMIT_INFO="/export/servers/spark-2.2.0-bin-hadoop2.7/bin/spark-sql --master spark://node01:7077 --executor-memory 1g --total-executor-cores 2 --conf spark.sql.warehouse.dir=hdfs://node01:8020/user/hive/warehouse"
SOURCE_DATA="/export/servers/businessData"
#订单主要信息表BDM层
SQl_BDM_USER_PC_CLICK_LOG="create database if not exists bdm;
create external table if not exists bdm.bdm_user_pc_click_log(session_id STRING,  --sessionIDcookie_id STRING,   --cookieIDvisit_time STRING,  --访问时间user_id STRING,     --用户IDvisit_url STRING,   --访问的URLvisit_os STRING,    --操作系统browser_name STRING,--游览器名称visit_ip STRING,    --访问ipprovince STRING,    --省份city STRING,        --城市page_id STRING,     --页面IDgoods_id STRING,    --商品IDshop_id STRING      --商店ID
) partitioned by (dt string)
row format delimited fields terminated by ','
lines terminated by '\n'
location '/business/bdm_user_pc_click_log';
alter table bdm.bdm_user_pc_click_log add partition (dt='$yesterday');"##创建bdm_user_pc_click_log表
echo "${SQl_BDM_USER_PC_CLICK_LOG}"
${SPARK_SUBMIT_INFO} -e "${SQl_BDM_USER_PC_CLICK_LOG}"
##添加数据到BDM
hdfs dfs -put /export/servers/businessData/bdm_user_pc_click_log.txt /business/bdm_user_pc_click_log/dt=$yesterday

用户 pc 端 view 表 (fdm_user_pc_pageview)

#***************************
#** 功能描述:fdm_user_pc_pageview开发
#***************************
#!/bin/sh
yesterday=`date -d '-1 day' "+%Y-%m-%d"`
if [ $1 ];thenyesterday=$1
fi
SPARK_SUBMIT_INFO="/export/servers/spark-2.2.0-bin-hadoop2.7/bin/spark-sql --master spark://node01:7077 --executor-memory 512m --total-executor-cores 1 --conf spark.sql.warehouse.dir=hdfs://node01:8020/user/hive/warehouse"
SOURCE_DATA="/export/servers/businessData"
#用户上网轨迹表FDM层--PC端
SQl_FDM_USER_PC_PAGEVIEW="create database if not exists fdm;
create table if not exists fdm.fdm_user_pc_pageview(session_id STRING,  --sessionIDcookie_id STRING,   --cookieIDuser_id STRING,     --用户IDin_time STRING,     --访问进入时间out_time STRING,    --访问离开时间stay_time STRING,   --访问停留时间pv BIGINT,          --PVvisit_os STRING,    --操作系统browser_name STRING,--游览器名称visit_ip STRING,    --访问ipprovince STRING,    --省份city STRING         --城市
) partitioned BY (dt STRING);"
#加载数据
LOAD_FDM_USER_PC_PAGEVIEW="INSERT overwrite TABLE fdm.fdm_user_pc_pageview PARTITION (dt = '$yesterday')
SELECT t.session_id,t.cookie_id,t.user_id,MIN(visit_time) in_time,MAX(visit_time) out_time,(caseWHEN 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
FROMbdm.bdm_user_pc_click_log t
WHERE dt = '$yesterday'
GROUP BY t.session_id,t.cookie_id,t.user_id,t.visit_os,t.browser_name,t.visit_ip,t.province,t.city ;"
##创建FDM_USER_PC_PAGEVIEW表
echo "${SQl_FDM_USER_PC_PAGEVIEW}"
${SPARK_SUBMIT_INFO} -e "${SQl_FDM_USER_PC_PAGEVIEW}"
##加载数据
echo "${LOAD_FDM_USER_PC_PAGEVIEW}"
${SPARK_SUBMIT_INFO} -e "${LOAD_FDM_USER_PC_PAGEVIEW}"

用户30天访问临时表 (gdm_user_visit_temp_01)

#***************************
#** 功能描述:GDM_USER_VISIT_TEMP_01开发
#***************************
#!/bin/sh
yesterday=`date -d '-1 day' "+%Y-%m-%d"`
if [ $1 ];thenyesterday=$1
fi
SPARK_SUBMIT_INFO="/export/servers/spark-2.2.0-bin-hadoop2.7/bin/spark-sql --master spark://node01:7077 --executor-memory 1g --total-executor-cores 2 --conf spark.sql.warehouse.dir=hdfs://node01:8020/user/hive/warehouse"
SOURCE_DATA="/export/servers/businessData"
#近30天PC端访问最常用的指标
SQL_GDM_USER_VISIT_TEMP_01="drop table if exists gdm.gdm_user_visit_temp_01;
create table gdm.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 fromfdm.fdm_user_pc_pageview a where dt >= date_add('$yesterday', -29) group by a.user_id,a.visit_ip unionall select a.user_id,a.cookie_id con,'cookie_id' type,count(1) cnt fromfdm.fdm_user_pc_pageview a where dt >= date_add('$yesterday', -29) group by a.user_id,a.cookie_id unionall select a.user_id,a.browser_name con,'browser_name' type,count(1) cnt fromfdm.fdm_user_pc_pageview a where dt >= date_add('$yesterday', -29) group by a.user_id,a.browser_name unionall select a.user_id,a.visit_os con,'visit_os' type,count(1) cnt fromfdm.fdm_user_pc_pageview a where dt >= date_add('$yesterday', -29) group by a.user_id,a.visit_os) b) t ;"##创建gdm_user_order_addr_model表
echo "${SQL_GDM_USER_VISIT_TEMP_01}"
$SPARK_SUBMIT_INFO -e "${SQL_GDM_USER_VISIT_TEMP_01}"

用户上网记录表(APP) (bdm_user_app_click_log)

#***************************
#** 功能描述:bdm_user_app_click_log开发
#***************************
#!/bin/sh
yesterday=`date -d '-1 day' "+%Y-%m-%d"`
if [ $1 ];thenyesterday=$1
fi
SPARK_SUBMIT_INFO="/export/servers/spark-2.2.0-bin-hadoop2.7/bin/spark-sql --master spark://node01:7077 --executor-memory 521m --total-executor-cores 1 --conf spark.sql.warehouse.dir=hdfs://node01:8020/user/hive/warehouse"
SOURCE_DATA="/export/servers/businessData"
#订单主要信息表BDM层
SQl_BDM_USER_APP_CLICK_LOG="create database if not exists bdm;
create external table if not exists bdm.bdm_user_app_click_log(user_id string,        --用户IDlog_time string,       --访问时间phone_id string,       --手机ID,唯一标识一台设备visit_os string,       --操作系统 android、ios、wpos_version string,     --操作系统版本app_name string,       --APP的名称app_version string,    --APP的版本device_token string,   --PUSH码,消息推送的visit_ip string,       --访问ipprovince string,       --省份city string            --城市
) partitioned by (dt string)
row format delimited fields terminated by ','
lines terminated by '\n'
location '/business/bdm_user_app_click_log';
alter table bdm.bdm_user_app_click_log add partition (dt='$yesterday');"##创建bdm_user_app_click_log表
echo "${SQl_BDM_USER_APP_CLICK_LOG}"
${SPARK_SUBMIT_INFO} -e "${SQl_BDM_USER_APP_CLICK_LOG}"
##添加数据到BDM
hdfs dfs -put /export/servers/businessData/bdm_user_app_click_log.txt /business/bdm_user_app_click_log/dt=$yesterday

用户 app 端 view 表 (fdm_user_app_pageview)

#***************************
#** 功能描述:fdm_user_app_pageview开发
#***************************
#!/bin/sh
yesterday=`date -d '-1 day' "+%Y-%m-%d"`
if [ $1 ];thenyesterday=$1
fi
SPARK_SUBMIT_INFO="/export/servers/spark-2.2.0-bin-hadoop2.7/bin/spark-sql --master spark://node01:7077 --executor-memory 512m --total-executor-cores 1 --conf spark.sql.warehouse.dir=hdfs://node01:8020/user/hive/warehouse"
SOURCE_DATA="/export/servers/businessData"
#用户上网轨迹表FDM层--app端
SQl_FDM_USER_APP_PAGEVIEW="create database if not exists fdm;
create  table if not exists fdm.fdm_user_app_pageview(user_id string,        --用户IDlog_time string,       --访问时间 2016-12-12 10:20:30log_hour string,         --访问时间的小时数 10phone_id string,       --手机ID,唯一标识一台设备visit_os string,       --操作系统 android、ios、wpos_version string,     --操作系统版本app_name string,       --APP的名称app_version string,    --APP的版本device_token string,   --PUSH码,消息推送的visit_ip string,       --访问ipprovince string,       --省份city string            --城市
) partitioned BY (dt STRING);"
#加载数据
LOAD_FDM_USER_APP_PAGEVIEW="INSERT overwrite TABLE fdm.fdm_user_app_pageview PARTITION (dt = '$yesterday')
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
FROMbdm.bdm_user_app_click_log t
WHERE dt = '$yesterday' ;"
##创建FDM_USER_PC_PAGEVIEW表
echo "${SQl_FDM_USER_APP_PAGEVIEW}"
${SPARK_SUBMIT_INFO} -e "${SQl_FDM_USER_APP_PAGEVIEW}"
##加载数据
echo "${LOAD_FDM_USER_APP_PAGEVIEW}"
${SPARK_SUBMIT_INFO} -e "${LOAD_FDM_USER_APP_PAGEVIEW}"

客户访问信息表 *** (gdm_user_visit)

#***************************
#** 功能描述:gdm_user_visit开发
#***************************
#!/bin/sh
yesterday=`date -d '-1 day' "+%Y-%m-%d"`
if [ $1 ];thenyesterday=$1
fi
SPARK_SUBMIT_INFO="/export/servers/spark-2.2.0-bin-hadoop2.7/bin/spark-sql --master spark://node01:7077 --executor-memory 512m --total-executor-cores 1 --conf spark.sql.warehouse.dir=hdfs://node01:8020/user/hive/warehouse"
SOURCE_DATA="/export/servers/businessData"
#客户访问信息表模型开发
SQL_GDM_USER_VISIT="create database if not exists gdm;
create  table if not exists gdm.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(不分APP与PC)
latest_city string,                     --最近一次访问城市(不分APP与PC)
latest_province string,                 --最近一次访问省份(不分APP与PC)
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(不分APP与PC)
first_city string,                      --第一次访问城市(不分APP与PC)
first_province string,                  --第一次访问省份(不分APP与PC)
day7_app_cnt bigint,                    --近7天APP端访问次数
day15_app_cnt bigint,                   --近15天APP端访问次数
month1_app_cnt bigint,                  --近30天APP端访问次数
month2_app_cnt bigint,                  --近60天APP端访问次数
month3_app_cnt bigint,                  --近90天APP端访问次数
day7_pc_cnt bigint,                     --近7天PC端访问次数
day15_pc_cnt bigint,                    --近15天PC端访问次数
month1_pc_cnt bigint,                   --近30天PC端访问次数
month2_pc_cnt bigint,                   --近60天PC端访问次数
month3_pc_cnt bigint,                   --近90天PC端访问次数
month1_pc_days bigint,                  --近30天PC端访问天数
month1_pc_pv bigint,                    --近30天PC端访问PV
month1_pc_avg_pv bigint,                --近30天PC端访问平均PV
month1_pc_diff_ip_cnt bigint,           --近30天PC端访问不同ip数
month1_pc_diff_cookie_cnt bigint,       --近30天PC端访问不同的cookie数
month1_pc_common_ip string,             --近30天PC端访问最常用ip
month1_pc_common_cookie string,         --近30天PC端访问最常用的cookie
month1_pc_common_browser_name string,   --近30天PC端访问最常用游览器
month1_pc_common_os string,             --近30天PC端访问最常用的操作系统
month1_hour025_cnt bigint,              --近30天PC端0-5点访问次数
month1_hour627_cnt bigint,              --近30天PC端6-7点访问次数
month1_hour829_cnt bigint,              --近30天PC端8-9点访问次数
month1_hour10212_cnt bigint,            --近30天PC端10-12点访问次数
month1_hour13214_cnt bigint,            --近30天PC端13-14点访问次数
month1_hour15217_cnt bigint,            --近30天PC端15-17点访问次数
month1_hour18219_cnt bigint,            --近30天PC端18-19点访问次数
month1_hour20221_cnt bigint,            --近30天PC端20-21点访问次数
month1_hour22223_cnt bigint,            --近30天PC端22-23点访问次数
dw_date timestamp
) partitioned by (dt string);"
#加载数据
LOAD_GDM_USER_VISIT="INSERT overwrite TABLE gdm.gdm_user_visit PARTITION (dt = '$yesterday')
SELECT t.user_id,--客户IDMAX(CASEWHEN pc.rn_desc = 1 THEN pc.in_time END) latest_pc_visit_date,--最近一次PC端访问日期MAX(CASEWHEN app.rn_desc = 1 THEN app.log_time END) latest_app_visit_date,--最近一次APP端访问日期MAX(CASEWHEN pc.rn_desc = 1 THEN pc.session_id END) latest_pc_visit_session,--最近一次PC端访问的sessionMAX(CASEWHEN pc.rn_desc = 1 THEN pc.cookie_id END) latest_pc_cookies,--最近一次PC端访问的cookiesMAX(CASEWHEN pc.rn_desc = 1 THEN pc.pv END) latest_pc_pv,--最近一次PC端访问的PVMAX(CASEWHEN pc.rn_desc = 1 THEN pc.browser_name END) latest_pc_browser_name,--最近一次PC端访问使用的游览器MAX(CASEWHEN pc.rn_desc = 1 THEN pc.visit_os END) latest_pc_visit_os,--最近一次PC端访问使用的操作系统MAX(CASEWHEN app.rn_desc = 1 THEN app.app_name END) latest_app_name,--最近一次APP端访问app名称MAX(CASEWHEN app.rn_desc = 1 THEN app.visit_os END) latest_app_visit_os,--最近一次APP端访问使用的操作系统MAX(CASEWHEN pc.rn_desc = 1 AND app.rn_desc = 1 AND pc.in_time >= app.log_time THEN pc.visit_ipWHEN 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(不分APP与PC)MAX(CASEWHEN pc.rn_desc = 1 AND app.rn_desc = 1 AND pc.in_time >= app.log_time THEN pc.cityWHEN pc.rn_desc = 1 AND app.rn_desc = 1 AND pc.in_time < app.log_time THEN app.city END) latest_city,--最近一次访问城市(不分APP与PC)MAX(CASEWHEN 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,--最近一次访问省份(不分APP与PC)MAX(CASEWHEN pc.rn_asc = 1 THEN pc.in_time END) first_pc_visit_date,--第一次PC端访问日期MAX(CASEWHEN app.rn_asc = 1 THEN app.log_time END) first_app_visit_date,--第一次APP端访问日期MAX(CASEWHEN pc.rn_asc = 1 THEN pc.session_id END) first_pc_visit_session,--第一次PC端访问的sessionMAX(CASEWHEN pc.rn_asc = 1 THEN pc.cookie_id END) first_pc_cookies,--第一次PC端访问的cookiesMAX(CASEWHEN pc.rn_asc = 1 THEN pc.pv END) first_pc_pv,--第一次PC端访问的PVMAX(CASEWHEN pc.rn_asc = 1 THEN pc.browser_name END) first_pc_browser_name,--第一次PC端访问使用的游览器MAX(CASEWHEN pc.rn_asc = 1 THEN pc.visit_os END) first_pc_visit_os,--第一次PC端访问使用的操作系统MAX(CASEWHEN app.rn_asc = 1 THEN app.app_name END) first_app_name,--第一次APP端访问app名称MAX(CASEWHEN app.rn_asc = 1 THEN app.visit_os END) first_app_visit_os,--第一次APP端访问使用的操作系统MAX(CASEWHEN 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(不分APP与PC)MAX(CASEWHEN 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,--第一次访问城市(不分APP与PC)MAX(CASEWHEN 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,--第一次访问省份(不分APP与PC)SUM(CASEWHEN app.dat_7 = 1 THEN 1 END) day7_app_cnt,--近7天APP端访问次数SUM(CASEWHEN app.dat_15 = 1 THEN 1 END) day15_app_cnt,--近15天APP端访问次数SUM(CASEWHEN app.dat_30 = 1 THEN 1 END) month1_app_cnt,--近30天APP端访问次数SUM(CASEWHEN app.dat_60 = 1 THEN 1 END) month2_app_cnt,--近60天APP端访问次数SUM(CASEWHEN app.dat_90 = 1 THEN 1 END) month3_app_cnt,--近90天APP端访问次数COUNT(CASEWHEN pc.dat_7 = 1 THEN pc.session_id END) day7_pc_cnt,--近7天PC端访问次数COUNT(CASEWHEN pc.dat_15 = 1 THEN pc.session_id END) day15_pc_cnt,--近15天PC端访问次数COUNT(CASEWHEN pc.dat_30 = 1 THEN pc.session_id END) month1_pc_cnt,--近30天PC端访问次数COUNT(CASEWHEN pc.dat_60 = 1 THEN pc.session_id END) month2_pc_cnt,--近60天PC端访问次数COUNT(CASEWHEN pc.dat_90 = 1 THEN pc.session_id END) month3_pc_cnt,--近90天PC端访问次数 $yesterday 12:20:30COUNT(DISTINCT substr(pc.in_time,0,10)) month1_pc_days,--近30天PC端访问天数SUM(CASEWHEN pc.dat_30 = 1 THEN pc.pv END) month1_pc_pv,--近30天PC端访问PVSUM(CASEWHEN pc.dat_30 = 1 THEN pc.pv END) / COUNT(DISTINCT substr(pc.in_time,0,10)) month1_pc_avg_pv,--近30天PC端访问平均PVMAX(b.month1_pc_diff_ip_cnt),--近30天PC端访问不同ip数MAX(b.month1_pc_diff_cookie_cnt),--近30天PC端访问不同的cookie数MAX(b.month1_pc_common_ip),--近30天PC端访问最常用ipMAX(b.month1_pc_common_cookie),--近30天PC端访问最常用的cookieMAX(b.month1_pc_common_browser_name),--近30天PC端访问最常用游览器MAX(b.month1_pc_common_os),--近30天PC端访问最常用的操作系统COUNT(CASEWHEN pc.visit_hour >= 0 AND pc.visit_hour <= 5 THEN pc.session_id END) month1_hour025_cnt,--近30天PC端0-5点访问次数COUNT(CASEWHEN pc.visit_hour >= 6 AND pc.visit_hour <= 7 THEN pc.session_id END) month1_hour627_cnt,--近30天PC端6-7点访问次数COUNT(CASEWHEN pc.visit_hour >= 8 AND pc.visit_hour <= 9 THEN pc.session_id END) month1_hour829_cnt,--近30天PC端8-9点访问次数COUNT(CASEWHEN pc.visit_hour >= 10 AND pc.visit_hour <= 12 THEN pc.session_id END) month1_hour10212_cnt,--近30天PC端10-12点访问次数COUNT(CASEWHEN pc.visit_hour >= 13 AND pc.visit_hour <= 14 THEN pc.session_id END) month1_hour13214_cnt,--近30天PC端13-14点访问次数COUNT(CASEWHEN pc.visit_hour >= 15 AND pc.visit_hour <= 17 THEN pc.session_id END) month1_hour15217_cnt,--近30天PC端15-17点访问次数COUNT(CASEWHEN pc.visit_hour >= 18 AND pc.visit_hour <= 19 THEN pc.session_id END) month1_hour18219_cnt,--近30天PC端18-19点访问次数COUNT(CASEWHEN pc.visit_hour >= 20 AND pc.visit_hour <= 21 THEN pc.session_id END) month1_hour20221_cnt,--近30天PC端20-21点访问次数COUNT(CASEWHEN pc.visit_hour >= 22 AND pc.visit_hour <= 23 THEN pc.session_id END) month1_hour22223_cnt,--近30天PC端22-23点访问次数FROM_UNIXTIME(UNIX_TIMESTAMP()) dw_date
FROM(SELECT user_id FROMfdm.fdm_user WHERE dt = '$yesterday') t LEFT JOIN (SELECT a.*,(CASEWHEN in_time >= DATE_SUB('$yesterday', 6) AND in_time <= '$yesterday' THEN 1 END) dat_7,(CASEWHEN in_time >= DATE_SUB('$yesterday', 14) AND in_time <= '$yesterday' THEN 1 END) dat_15,(CASEWHEN in_time >= DATE_SUB('$yesterday', 29) AND in_time <= '$yesterday' THEN 1 END) dat_30,(CASEWHEN in_time >= DATE_SUB('$yesterday', 59) AND in_time <= '$yesterday' THEN 1 END) dat_60,(CASEWHEN in_time >= DATE_SUB('$yesterday', 89) AND in_time <= '$yesterday' 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 FROMfdm.fdm_user_pc_pageview a WHERE in_time >= DATE_ADD('$yesterday', -89) AND in_time <= '$yesterday') pc ON (t.user_id = pc.user_id) LEFT JOIN (SELECT user_id,sum(CASEWHEN TYPE = 'visit_ip' THEN cnt END) month1_pc_diff_ip_cnt,MAX(CASEWHEN TYPE = 'visit_ip' and rn= 1THEN con END) month1_pc_common_ip,sum(CASEWHEN TYPE = 'cookie_id' THEN cnt END) month1_pc_diff_cookie_cnt,MAX(CASEWHEN TYPE = 'cookie_id' and rn = 1THEN con END) month1_pc_common_cookie,MAX(CASEWHEN TYPE = 'browser_name' and rn = 1THEN con END) month1_pc_common_browser_name,MAX(CASEWHEN TYPE = 'visit_os' and rn = 1THEN con END) month1_pc_common_os FROMgdm.gdm_user_visit_temp_01 GROUP BY user_id) b ON (t.user_id = b.user_id) LEFT JOIN (SELECT a.*,(CASEWHEN log_time >= DATE_SUB('$yesterday', 6) AND log_time <= '$yesterday' THEN 1 END) dat_7,(CASEWHEN log_time >= DATE_SUB('$yesterday', 14) AND log_time <= '$yesterday' THEN 1 END) dat_15,(CASEWHEN log_time >= DATE_SUB('$yesterday', 29) AND log_time <= '$yesterday' THEN 1 END) dat_30,(CASEWHEN log_time >= DATE_SUB('$yesterday', 59) AND log_time <= '$yesterday' THEN 1 END) dat_60,(CASEWHEN log_time >= DATE_SUB('$yesterday', 89) AND log_time <= '$yesterday' 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 FROMfdm.fdm_user_app_pageview a WHERE log_time >= DATE_ADD('$yesterday', -89) AND log_time <= '$yesterday') app ON (t.user_id = app.user_id)
GROUP BY t.user_id ;"
##创建GDM_USER_VISIT表
echo "${SQL_GDM_USER_VISIT}"
${SPARK_SUBMIT_INFO} -e "${SQL_GDM_USER_VISIT}"
##加载数据
echo "${LOAD_GDM_USER_VISIT}"
${SPARK_SUBMIT_INFO} -e "${LOAD_GDM_USER_VISIT}"

用户宽表汇总模型表(adm_personas)

sh adm_personas.sh 2020-11-06
#***************************
#** 功能描述:adm_personas开发
#***************************
#!/bin/sh
yesterday=`date -d '-1 day' "+%Y-%m-%d"`
if [ $1 ];thenyesterday=$1
fi
SPARK_SUBMIT_INFO="/export/servers/spark-2.2.0-bin-hadoop2.7/bin/spark-sql --master spark://node01:7077 --executor-memory 512m --total-executor-cores 1 --conf spark.sql.warehouse.dir=hdfs://node01:8020/user/hive/warehouse"
SOURCE_DATA="/export/servers/businessData"
#客户访问信息表模型开发
SQL_ADM_PERSONAS="create database if not exists adm;
create table if not exists adm.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           ,--马甲标志
majia_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(不分APP与PC)
latest_city string,                     --最近一次访问城市(不分APP与PC)
latest_province string,                 --最近一次访问省份(不分APP与PC)
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(不分APP与PC)
first_city string,                      --第一次访问城市(不分APP与PC)
first_province string,                  --第一次访问省份(不分APP与PC)
day7_app_cnt bigint,                    --近7天APP端访问次数
day15_app_cnt bigint,                   --近15天APP端访问次数
month1_app_cnt bigint,                  --近30天APP端访问次数
month2_app_cnt bigint,                  --近60天APP端访问次数
month3_app_cnt bigint,                  --近90天APP端访问次数
day7_pc_cnt bigint,                     --近7天PC端访问次数
day15_pc_cnt bigint,                    --近15天PC端访问次数
month1_pc_cnt bigint,                   --近30天PC端访问次数
month2_pc_cnt bigint,                   --近60天PC端访问次数
month3_pc_cnt bigint,                   --近90天PC端访问次数
month1_pc_days bigint,                  --近30天PC端访问天数
month1_pc_pv bigint,                    --近30天PC端访问PV
month1_pc_avg_pv bigint,                --近30天PC端访问平均PV
month1_pc_diff_ip_cnt bigint,           --近30天PC端访问不同ip数
month1_pc_diff_cookie_cnt bigint,       --近30天PC端访问不同的cookie数
month1_pc_common_ip string,             --近30天PC端访问最常用ip
month1_pc_common_cookie string,         --近30天PC端访问最常用的cookie
month1_pc_common_browser_name string,   --近30天PC端访问最常用游览器
month1_pc_common_os string,             --近30天PC端访问最常用的操作系统
month1_hour025_cnt bigint,              --近30天PC端0-5点访问次数
month1_hour627_cnt bigint,              --近30天PC端6-7点访问次数
month1_hour829_cnt bigint,              --近30天PC端8-9点访问次数
month1_hour10212_cnt bigint,            --近30天PC端10-12点访问次数
month1_hour13214_cnt bigint,            --近30天PC端13-14点访问次数
month1_hour15217_cnt bigint,            --近30天PC端15-17点访问次数
month1_hour18219_cnt bigint,            --近30天PC端18-19点访问次数
month1_hour20221_cnt bigint,            --近30天PC端20-21点访问次数
month1_hour22223_cnt bigint             --近30天PC端22-23点访问次数
);"
#加载数据
LOAD_ADM_PERSONAS="insert overwrite table adm.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.majia_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_cart_category_cnt,
c.month3_cart_category_cnt,
c.month6_cart_category_cnt,
c.total_cart_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.gdm_user_basic a
left join gdm.gdm_user_consume_order b on a.user_id=b.user_id
left join gdm.gdm_user_buy_category c on a.user_id=c.user_id
left join gdm.gdm_user_visit d on a.user_id=d.user_id;"
##创建GDM_USER_VISIT表
echo "${SQL_ADM_PERSONAS}"
${SPARK_SUBMIT_INFO} -e "${SQL_ADM_PERSONAS}"
##加载数据
echo "${LOAD_ADM_PERSONAS}"
${SPARK_SUBMIT_INFO} -e "${LOAD_ADM_PERSONAS}"

用户画像(一)数仓与表结构的基本构建相关推荐

  1. 数仓维度表的作用和好处

    数仓维度表的作用和好处 1.缩小了事实表的大小. 2.便于维度的管理和维护,增加,删除和修改维度的属性,不必对事实表的大量记录进行改动. 3.维度表可以为多个事实表重用,以减少重复工作.

  2. 数据库与MPP数仓(十三):数据仓库的模型构建

    上一篇主要讲到的是数据仓库对于企业的价值,以及同数据库系统的区别,数据仓库作为业务扩张和数据价值萃取需求的必然产物,其在构建上也有一些参照的既定规范模式,本篇将讲解数据仓库模型构建思路: 目录 一.数 ...

  3. 用户画像:数据指标与表结构设计

    本篇博客介绍一下画像中需要开发的数据指标与开发过程中表结构的设计. 首先介绍画像开发的数据指标,画像开发过程中通用类的指标体系包括用户属性类.用户行为标签类.用户活跃时间段类.用户消费能力类.用户偏好 ...

  4. 谈笑间学会数仓—维度表概念及设计案例

    维度表 维度定义 从某个角度观察事实数据的窗口,存储的数据用来从某个角度描述事实.维度表可以看成是用户用来分析一个事实的窗口,它里面的数据应该是对事实的各个方面描述,比如时间维度表,它里面的数据就是一 ...

  5. 用户画像建模(客户基本属性表,客户营销信息表)

    2-4 客户基本属性 客户基本属性表 客户基本属性表- 根据用户所填的用户标签与推算出来的标签- 用于了解用户的人口属性的基本情况和按属性统计- 按人口属性营销.比如营销80后,对金牛座的优惠.生日营 ...

  6. 数仓基于表级别的数据血缘分析

    1.背景:数据血缘分析在日常的处理过程中非常重要,其分为应用级别.表级别和字段级别三种.在数据处理过程中,需要知道目标表的字段是来源于哪张原始表.对于简单的hsql来说我们很容易可以知道目标表的来源, ...

  7. 数仓主题表怎么设计_陶瓷企业展厅设计主题风格怎么确定?

    陶瓷是文化的结晶,艺术的精华.陶瓷的发明,是人类社会发展史上划时代的标志.当前,越来越多的陶瓷企业开始通过打造数字展厅来树立品牌形象,这一便捷的宣传推广形式,向消费者传达着陶瓷品牌个性.那么,在陶瓷企 ...

  8. 【电商数仓】数仓即席查询之Kylin Cube构建原理和构建优化

    文章目录 一 Kylin Cube构建原理 1 维度和度量 2 Cube和Cuboid 3 Cube构建算法 (1)逐层构建算法(layer) (2)快速构建算法(inmem) 4 Cube存储原理 ...

  9. 用户画像构建方法调研和初步解决方案

    近期,由于项目需求,需要构建一套消费者的用户画像.这是一套偏大数据处理和实时数仓领域的解决方案,因为之前对这个领域并不熟悉,因此做了下前期的方案调研和初步的解决方案设计,本文将这个过程做个记录总结,希 ...

最新文章

  1. 了解1D和3D卷积神经网络|Keras
  2. Hibernate各种主键生成策略与配置详解 - 真的很详细啊!!
  3. weblogic:local class incompatible: stream classdesc serialVersionUID
  4. String 字符串最长可以有多长?
  5. 【Spring注解系列14】@Profile进行环境切换
  6. 分类变量 哑变量矩阵 指标矩阵_不懂数据集重排序?分类变量转换苦难?4种python方法,不再难!...
  7. Filtering microblogging messages for Social TV
  8. 2019年广西大学硕士研究生入学《数据结构与程序设计(817)》考试大纲
  9. 在Block中使用weakSelf与strongSelf的意义
  10. js中的面向对象入门
  11. eclipse debug 工程源码时出现source not found问题解决
  12. 阿里云apache配置php mysql_阿里云CentOS7搭建Apache+PHP+MySQL环境
  13. c# list排序的三种实现方式
  14. Azure平台跨存储迁移虚拟机
  15. linux重装系统保留文件,linux重装系统,如何保存硬盘中的内容
  16. H5标签在页面被转译
  17. 希尔伯特:数学世界的亚历山大(2009-06-11 21:33:43)
  18. 阅读高效能人士七个习惯
  19. 第二十二课--提示工具(Tooltip)
  20. 【机械仿真】基于matlab GUI曲柄摇杆机构运动仿真【含Matlab源码 1608期】

热门文章

  1. Spring是如何扫描包的 Spring5.x(源码向)
  2. PING(计算机网络)
  3. 梅科尔工作室——SVM 机器学习算法
  4. FlatList使用示例包含RefreshControl
  5. BBQ---Java算法
  6. 三国志战略版:剧本解读之官渡之战
  7. 基于jQuery实现的 “滑动门”
  8. C语言——计算时间差、求平均值
  9. 攻防世界——WEB题目Ikun_BILIBILI
  10. word引用目录出现两个的原因