使用zeppelin分析电子商务消费行为

项目文件获取,提取码: m3d4

文章目录

一.任务描述

需求概述

  • 对某零售企业最近1年门店收集的数据进行数据分析
  • 潜在客户画像
  • 用户消费统计
  • 门店的资源利用率
  • 消费的特征人群定位
  • 数据的可视化展现

二.问题分析

问题分析1:Customer表

customer_detailsdetails
customer_idInt, 1 - 500
first_namestring
last_namestring
emailstring, such as willddy@gmail.com
genderstring, Male or female
addressstring
countrystring
languagestring
jobstring, job title/position
credit_typestring, credit card type, such as visa
credit_nostring, credit card number

问题:language字段数据存在错误

问题分析2:Transaction表

transaction_detailsdetails
transaction_idInt, 1 - 1000
customer_idInt, 1 - 500
store_idInt, 1 - 5
pricedecimal, such as 5.08
productstring, things bought
datestring, when to purchase
timestring, what time to purchase

问题:表中transaction_id有重复,但数据有效,需要修复数据

问题分析3:Store表

transaction_detailsdetails
transaction_idInt, 1 - 1000
customer_idInt, 1 - 500
store_idInt, 1 - 5
pricedecimal, such as 5.08
productstring, things bought
datestring, when to purchase
timestring, what time to purchase

问题分析1:Review表

store_reviewdetails
stransaction_idInt, 1 - 8000
store_idInt, 1 - 5
review_storeInt, 1 - 5

问题:表中有无效的score数据表中有将transaction_id映射到错误的store_id

三.连接zeppelin

导入电子商务消费行为分析数据及模板
在这里插入图片描述
在这里插入图片描述

使用刚才创建的模板

1.从windows上传到linux 的/tmp/data目录下

2.Understand the Data

%sh
## /tmp/data/
-- 查看行数
cd /tmp/data/
wc -l customer_details.csv
wc -l store_details.csv
wc -l transaction_details.csv
wc -l store_review.csv
-- 查看头两行
head -2 customer_details.csv
head -2 transaction_details.csv
head -2 store_details.csv
head -2 store_review.csv

3. Upload the file to HDFS

%sh
cd /tmp/data/
hdfs dfs -rm -r -f /tmp/shopping
hdfs dfs -mkdir -p /tmp/shopping/data/customer
hdfs dfs -mkdir -p /tmp/shopping/data/store
hdfs dfs -mkdir -p /tmp/shopping/data/review
hdfs dfs -mkdir -p /tmp/shopping/data/transaction
hdfs dfs -chmod -R 777 /tmp
-- 上传数据到hdfs
hdfs dfs -put customer_details.csv /tmp/shopping/data/customer/
hdfs dfs -put transaction_details.csv /tmp/shopping/data/transaction/
hdfs dfs -put store_details.csv /tmp/shopping/data/store/
hdfs dfs -put store_review.csv /tmp/shopping/data/review/

4.建表查表

4.1 Clear all tables if exists

create database if not exists shopping
use shopping
-- 创建顾客表
create external table if not exists ext_customer_details (
customer_id string, --we can use int as well
first_name string,
last_name string,
email string,
gender string,
address string,
country string,
language string,
job string,
credit_type string,
credit_no string
)
row format serde 'org.apache.hadoop.hive.serde2.OpenCSVSerde'
location '/tmp/shopping/data/customer' --this must tblproperties 
tblproperties ("skip.header.line.count"="1")
-- 创建交易流水表
create external table if not exists ext_transaction_details (
transaction_id string,
customer_id string,
store_id string,
price decimal(8,2),
product string,
purchase_date string,
purchase_time string
)
row format serde 'org.apache.hadoop.hive.serde2.OpenCSVSerde'
location '/tmp/shopping/data/transaction' --this must tblproperties 
tblproperties ("skip.header.line.count"="1")
-- 创建商店详情表
create external table if not exists ext_store_details (
store_id string,
store_name string,
employee_number int
)
row format serde 'org.apache.hadoop.hive.serde2.OpenCSVSerde'
location '/tmp/shopping/data/store' --this must tblproperties 
tblproperties ("skip.header.line.count"="1")
-- 创建评价表
create external table if not exists ext_store_review (
transaction_id string,
store_id string,
review_score int
)
row format serde 'org.apache.hadoop.hive.serde2.OpenCSVSerde'
location '/tmp/shopping/data/review' --this must tblproperties 
tblproperties ("skip.header.line.count"="1")

4.2 Verify all Tables are Created

%hive
--select * from ext_customer_details limit 20
--select * from ext_transaction_details limit 20
--select * from ext_store_details limit 20
select * from ext_store_review limit 20

5.数据清洗

解决以下有问题的数据

  • 对transaction_details中的重复数据生成新ID
  • 过滤掉store_review中没有评分的数据
  • 可以把清洗好的数据放到另一个表或者用View表示
  • 找出PII (personal information identification) 或PCI (personal confidential information) 数据进行加密或hash
  • 重新组织transaction数据按照日期YYYY-MM做分区

5.1 Clean and Mask customer_details

%hive
-- 敏感信息加密
-- drop view vm_customer_details
create view if not exists vm_customer_details as
select
customer_id ,
first_name ,
unbase64(last_name) lastname,
unbase64(email) email,
gender ,
unbase64(address) address,
country ,
language,
job ,
credit_type ,
unbase64(credit_no) credit_no
from 
ext_customer_details

5.2 Clean transaction_details into partition table

%hive
-- 创建流水详情表
create table if not exists transaction_details
(
transaction_id string,
customer_id string,
store_id string,
price decimal(8,2),
product string,
purchase_date date,
purchase_time string
)
partitioned by(purchase_month string)
-- select transaction_id,count(1) from ext_transaction_details group by transaction_id having count(1)>1
-- select * from ext_transaction_details where transaction_id=8001
set hive.exec.dynamic.partition.mode=nonstrict -- 开启动态分区
-- 重写数据
with base as (
select
transaction_id,
customer_id ,
store_id ,
price ,
product,
purchase_date,
purchase_time,
from_unixtime(unix_timestamp(purchase_date,'yyyy-MM-dd'),'yyyy-MM') as purchase_month,
row_number() over (partition by transaction_id order by store_id) as rn
from ext_transaction_details
)
insert overwrite table transaction_details partition(purchase_month)
select
if(rn=1,transaction_id,concat_ws('-',transaction_id,'_fix')) ,
customer_id ,
store_id ,
price ,
product,
purchase_date ,
purchase_time,
purchase_month
from base
-- 查看修复信息
select * from transaction_details where transaction_id like '%fix%'

5.3 Clean store_review table

create view if not exists vw_store_review as
select
transaction_id,
review_score
from  ext_store_review where review_score <> ''
show tables

最终会出现如下7个表:
在这里插入图片描述

6.Customer分析

6.1找出顾客最常用的信用卡
%hive
select credit_type,count(distinct credit_no) as credit_cnt
from
vm_customer_details
group by credit_type
order by credit_cnt desc

在这里插入图片描述

6.2找出客户资料中排名前五的职位名称
%hive
select job ,count(1) as pn
from vm_customer_details
group by job
order by pn desc
limit 5

在这里插入图片描述

6.3在美国女性最常用的信用卡
%hive
select  credit_type,count(1) as ct
from vm_customer_details
where country='United States' and gender =='Female'
group by credit_type
order by ct desc limit 5

在这里插入图片描述

6.4按性别和国家进行客户统计
%hive
select country,gender, count(1) cnt
from vm_customer_details
group by country,gender

在这里插入图片描述

7.Transaction分析

7.1计算每月总收入
%hive
select sum(price) as revenue_mon,purchase_month
from transaction_details
group by purchase_month

在这里插入图片描述

7.2计算每个季度的总收入
%hive
with
bash as
(
select price, ( concat(year(purchase_date),'-',ceil(month(purchase_date)/3)))as year_quarter
from transaction_details
)
select sum(price) revenue_quarter
from bash

在这里插入图片描述

7.3按年计算总收入
select year(purchase_date),sum(price)
from transaction_details
group by year(purchase_date)

在这里插入图片描述

7.4按工作日计算总收入
%hive
select dayofweek(cast(purchase_date as string))-1 work_date,sum(price)
from transaction_details
where dayofweek(cast(purchase_date as string)) between 2 and 6
group by dayofweek(cast(purchase_date as string))

在这里插入图片描述

7.5/7.6按时间段计算总收入(需要清理数据)
-- 使用正则表达式清理数据然后使用case when 分组查询
with
t1 as(
select *, if(instr(purchase_time,'PM')>0,
				if(cast(regexp_extract(purchase_time,'([0-9]{1,2}):([0-9]{2}\\w*)',1)as int)+12>=24,
					0,
					cast(regexp_extract(purchase_time,'([0-9]{1,2}):([0-9]{2}\\w*)',1)as int)+12),
				cast(regexp_extract(purchase_time,'([0-9]{1,2}):([0-9]{2}\\w*)',1)as int)) as timeTrans
from transaction_details), t2 as(
select t1.*,case when t1.timeTrans<=8 and t1.timeTrans>5 then 'early morning'
				 when t1.timeTrans<=11 and t1.timeTrans>8 then 'morning'
				 when t1.timeTrans<=13 and t1.timeTrans>11 then 'noon'
				 when t1.timeTrans<=18 and t1.timeTrans>13 then 'afternoon'
				 when t1.timeTrans<=22 and t1.timeTrans>18 then 'evening'
				 else 'night'
			end as timeSplit
from t1)
select t2.timeSplit,sum(price)
from t2 
group by t2.timeSplit

在这里插入图片描述

7.7按工作日计算平均消费
%hive
select dayofweek(cast(purchase_date as string))-1 work_date,avg(price)
from transaction_details
where dayofweek(cast(purchase_date as string)) between 2 and 6
group by dayofweek(cast(purchase_date as string))

在这里插入图片描述

7.8计算年、月、日的交易总数
-- 按天计数
select purchase_date ,count(1)
from transaction_details
group by purchase_date 
-- 按年计数
select year(purchase_date),count(1)
from transaction_details
group by year(purchase_date)
-- 按月计数
select concat(year(purchase_date),'-',month(purchase_date)),count(1)
from transaction_details
group by year(purchase_date),month(purchase_date)
-- 合计
select purchase_date,
	count(1) over(partition by year(purchase_date)),
	count(1) over(partition by year(purchase_date),month(purchase_date)),
	count(1) over(partition by year(purchase_date),month(purchase_date),day(purchase_date))
from transaction_details

在这里插入图片描述

7.9找出交易量最大的10个客户
select customer_id,count(1) c
from transaction_details
group by customer_id
order by c desc
limit 10

在这里插入图片描述

7.10找出消费最多的前10位顾客
select customer_id ,sum(price) s
from transaction_details
group by customer_id
order by s desc
limit 10

在这里插入图片描述

7.11统计该期间交易数量最少的用户
select customer_id ,count(1) c
from transaction_details
group by customer_id
order by c asc
limit 1

在这里插入图片描述

7.12计算每个季度的独立客户总数
select concat(year(purchase_date),'年',ceil(month(purchase_date)/3),'季度'),count(distinct customer_id)
from transaction_details
group by year(purchase_date),ceil(month(purchase_date)/3)

在这里插入图片描述

7.13计算每周的独立客户总数
select concat(year(purchase_date),'年第',weekofyear(purchase_date),'周'),count(distinct customer_id)
from transaction_details
group by year(purchase_date),weekofyear(purchase_date)

在这里插入图片描述

7.14计算整个活动客户平均花费的最大值
select a.customer_id,max(a.av)
from
(
select customer_id,avg(price) av
from transaction_details
group by customer_id) a
group by a.customer_id;

在这里插入图片描述

7.15统计每月花费最多的客户
select b.m,b.id,b.s
from(
select a.m,a.id,a.s ,row_number() over(partition by  a.m order by a.s desc) as win1
from(
select concat(year(purchase_date),'-',month(purchase_date)) m,customer_id id,sum(price) s
from transaction_details
group by year(purchase_date),month(purchase_date),customer_id)a) b 
where b.win1=1

在这里插入图片描述

7.16统计每月访问次数最多的客户
select b.m,b.id,b.c
from(
select a.m,a.id,a.c,row_number() over(partition by a.m order by a.c desc) as win1 
from(
select concat(year(purchase_date),'-',month(purchase_date)) m,customer_id id, count(1) c
from transaction_details
group by year(purchase_date),month(purchase_date),customer_id) a) b 
where b.win1=1

在这里插入图片描述

7.17按总价找出最受欢迎的5种产品
select product,sum(price) s 
from transaction_details
group by product
order by s desc
limit 5

在这里插入图片描述

7.18根据购买频率找出最畅销的5种产品
select product,count(1) c 
from transaction_details
group by product
order by c desc
limit 5

在这里插入图片描述

7.19根据客户数量找出最受欢迎的5种产品
select product,count(distinct customer_id) c 
from transaction_details
group by product
order by c
limit 5

8.Store分析

8.1按客流量找出最受欢迎的商店
select store_id,count(1) c 
from transaction_details
group by store_id
order by c desc
limit 1

在这里插入图片描述

8.2根据顾客消费价格找出最受欢迎的商店
select store_id,sum(price) s 
from transaction_details
group by store_id 
order by s desc  
limit 1

在这里插入图片描述

8.3根据顾客交易情况找出最受欢迎的商店
select store_id,count(1) c ,sum(price) s 
from transaction_details
group by store_id 
order by c desc ,s desc 
limit 1
8.4根据商店和唯一的顾客id获取最受欢迎的产品
select b.store_id,b.product
from (
select a.store_id,a.product,a.c ,row_number() over(partition by store_id order by a.c desc )as win1 
from(
select store_id,product,count(distinct customer_id) c 
from transaction_details
group by store_id,product) a )b 
where b.win1 =1

在这里插入图片描述

8.5获取每个商店的员工与顾客比
select a.store_id,concat_ws(':',cast(ceil(round(s.employee_number/a.c*100))as string),'100')
from(
select t.store_id,count(distinct customer_id) c
from transaction_details t 
group by t.store_id)a join ext_store_details s 
on a.store_id=s.store_id

在这里插入图片描述

8.6按年和月计算每家店的收入
-- 按月
select  store_id,year(purchase_date),month(purchase_date),sum(price)
from transaction_details
group by store_id,year(purchase_date),month(purchase_date)
-- 按年 
select  store_id,year(purchase_date),sum(price)
from transaction_details
group by store_id,year(purchase_date)
-- 合计到一张表
select distinct *
from(
select store_id,year(purchase_date),sum(price) over(partition by year(purchase_date)),month(purchase_date),sum(price) over(partition by year(purchase_date),month(purchase_date))
from transaction_details)a

在这里插入图片描述

8.7按店铺制作总收益饼图
select store_id,sum(price)
from transaction_details
group by store_id

在这里插入图片描述

8.8找出每个商店最繁忙的时间段
with
t1 as(
select *, if(instr(purchase_time,'PM')>0,
				if(cast(regexp_extract(purchase_time,'([0-9]{1,2}):([0-9]{2}\\w*)',1)as int)+12>=24,
					0,
					cast(regexp_extract(purchase_time,'([0-9]{1,2}):([0-9]{2}\\w*)',1)as int)+12),
				cast(regexp_extract(purchase_time,'([0-9]{1,2}):([0-9]{2}\\w*)',1)as int)) as timeTrans
from transaction_details), t2 as(
select t1.*,case when t1.timeTrans<=8 and t1.timeTrans>5 then 'early morning'
				 when t1.timeTrans<=11 and t1.timeTrans>8 then 'morning'
				 when t1.timeTrans<=13 and t1.timeTrans>11 then 'noon'
				 when t1.timeTrans<=18 and t1.timeTrans>13 then 'afternoon'
				 when t1.timeTrans<=22 and t1.timeTrans>18 then 'evening'
				 else 'night'
			end as timeSplit
from t1),
t3 as(
select t2.store_id,t2.timeSplit,count(1) c 
from t2 
group by t2.store_id,t2.timeSplit),
t4 as(
select t3.store_id,t3.timeSplit,row_number() over(partition by store_id order by t3.timeSplit desc)as win1
from t3 )
select t4.store_id,t4.timeSplit
from t4
where t4.win1=1

在这里插入图片描述

8.9找出每家店的忠实顾客
-- 购买超过6次
select a.*
from(
select store_id,customer_id,count(1) c
from transaction_details
group by store_id,customer_id)a 
where a.c>6

在这里插入图片描述

8.10根据每位员工的最高收入找出明星商店
-- 求总收入与雇员比值的最大值
with
t1 as (
select  store_id,sum(price) s 
from transaction_details 
group by store_id)
select t1.store_id,t1.s/s.employee_number ss
from t1 join ext_store_details s  on s.store_id= t1.store_id
order by ss desc 
limit 1

在这里插入图片描述

9.Review分析

9.1在ext_store_review中找出存在冲突的交易映射关系
select transaction_id
from vw_store_review
group by transaction_id
having count(1)>1

在这里插入图片描述

9.2了解客户评价的覆盖率
-- 求各个店共有多少顾客评价
with 
t1 as(
select t2.store_id,t1.transaction_id,t2.customer_id
from vw_store_review t1 join transaction_details t2 on t1.transaction_id=t2.transaction_id)
select t1.store_id,count(distinct t1.customer_id)
from t1 
group by t1.store_id

在这里插入图片描述

9.3根据评分了解客户的分布情况
-- 求每家店每个评分有多少个客户给的
with
t1 as(
select  t2.store_id ,t1.review_score,t2.customer_id
from vw_store_review t1 join  transaction_details t2 on t1.transaction_id=t2.transaction_id)
select t1.store_id,t1.review_score,count(distinct customer_id)
from t1
group by t1.store_id,t1.review_score

在这里插入图片描述

9.4根据交易了解客户的分布情况
-- 求每家店每个客户的订单数
select store_id,customer_id,count(1)
from transaction_details
group by store_id,customer_id

在这里插入图片描述

9.5客户给出的最佳评价是否总是同一家门店
-- 每位顾客对每家店的评分只取最大值,然后筛选每家店评分为5的数量,最大就是最优店
with
t1 as(
select r.store_id,t.customer_id,max(r.review_score) m 
from ext_store_review r  join transaction_details t 
on r.transaction_id = t.transaction_id 
group by r.store_id,t.customer_id),
t2 as (select * from t1 where t1.m=5)
select store_id,count(t2.m) c  from t2 
group by store_id
order by c desc 
limit 1

在这里插入图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<h3>回答1:</h3><br/>Zeppelin是一个开源的数据分析和可视化工具,可以连接多种数据源,包括Spark。要连接Spark,需要进行以下配置和使用步骤: 1. 在Zeppelin的主页上,点击“Create new note”创建一个新的笔记本。 2. 在笔记本中,点击“Interpreter binding”按钮,选择“spark”作为解释器。 3. 在“Interpreter setting”页面中,配置Spark的相关参数,包括Spark的主机名、端口号、应用名称等。 4. 点击“Save”保存配置,然后点击“Run”运行解释器。 5. 在笔记本中输入Spark的代码,例如: ``` val data = Seq(1, 2, 3, 4, 5) val rdd = sc.parallelize(data) rdd.collect() ``` 6. 点击“Run”运行代码,可以看到Spark的输出结果。 通过以上步骤,就可以在Zeppelin中连接和使用Spark了。需要注意的是,Zeppelin的版本和Spark的版本需要匹配,否则可能会出现兼容性问题。 <h3>回答2:</h3><br/>Zeppelin是一种使用方便的笔记本工具,它可以支持多语言,包括Scala、Python等,而且对于连接Spark来说非常友好。在Zeppelin与Spark之间,有两个连接选项可以使用使用本地模式连接或者使用远程模式连接。本地模式连接意味着Zeppelin与Spark运行在同一台机器上,而远程模式连接意味着Zeppelin与Spark运行在不同的机器上。 下面是使用Zeppelin连接Spark的步骤: 1. 首先,在Zeppelin中创建一个新的笔记本。 2. 接着,创建一个新的Spark interpreter。在Zeppelin首页的设置菜单中选择“Interpreter”,然后选择“Create”按钮。在弹出的界面中,填写相关信息(interpreter名称、Spark master URL等)后,选择“保存”按钮。 3. 启动interpreter。在Zeppelin中选择“Notebook”按钮,然后选择“Interpreters”按钮,接着在“Spark Interpreter”下面选择“start”按钮即可启动interpreter。 4. 配置Spark连接。如果使用本地模式连接,那么不需要其他操作。如果使用远程模式连接,需要在Zeppelin的配置文件中添加Spark的master URL。在Zeppelin的安装目录下找到conf目录中的zeppelin-site.xml文件,接着在其中添加: <property> <name>spark.master</name> <value>spark://your-spark-master-url:7077</value> </property> 上面的your-spark-master-url应该替换为实际的Spark master URL。 5. 测试Spark连接。在Zeppelin的笔记本中输入Spark相关的代码,例如: val data = Array(1, 2, 3, 4, 5) val distData = sc.parallelize(data) distData.map(x => x * x).collect().foreach(println) 接着执行代码,如果能够得到正确的结果,那么就说明Zeppelin与Spark已经连接成功了。 总之,Zeppelin与Spark的连接非常简单,只需要按照上面的步骤进行配置即可。使用Zeppelin可以方便地进行Spark相关的编程任务,而且支持多种语言,非常实用。 <h3>回答3:</h3><br/>Zeppelin 是一个开源的数据分析和可视化工具,提供了丰富的组件。其中,连接 Spark 可以让用户更加方便地利用 Zeppelin 的交互式笔记本功能来进行 Spark 的数据分析和处理。 一、配置 1. 安装 Spark 首先需要安装 Spark 并设置好环境变量,确保命令行中可以调用 Spark 相关命令。同时,需要设置 Spark 依赖的 Hadoop 和 Hive 环境。 2. 配置 Zeppelin Interpreter 进入 Zeppelin 配置页面,点击 Interpreter 选项卡。找到 spark 相关的 Interpreter,设置为以下参数: - master:设置为本地或集群的 Spark 主节点 URL; - deploy.mode:设置为 client 或 cluster,表示在本地还是在集群环境中运行; - executor.memory:设置每个 executor 的内存大小; - Num executors:设置 executor 的数量。 保存设置,重新启动 Zeppelin。 二、使用 1. 创建 Notebook 在 Zeppelin 主界面中创建一个新的 Notebook,选择与 Spark 相关的 Interpreter。创建 Notebook 后,会自动在页面中显示 Spark 的版本和相关信息,说明连接 Spark 成功。 2. 编写代码 在 Notebook 中,可以使用 Scala、Python 或 SQL 等语言编写 Spark 相关代码。Zeppelin 会自动将代码解析并显示在界面中,供用户查看和交互。用户可以在代码中使用 Spark 相关的 API,完成数据分析和处理任务,并将结果显示在界面中进行可视化。 3. 运行代码和查看结果 用户可以使用 Shift+Enter 快捷键或点击运行按钮来运行代码。运行结束后,可以在页面下方查看代码执行的结果和输出。如果有可视化图表,会自动显示在页面中。用户还可以进行进一步的分析和操作,以及保存 Notebook 和结果。 总之,连接 Spark 可以让 Zeppelin 更加方便地进行数据分析和可视化。用户可以利用 Zeppelin 提供的丰富功能和灵活性,快速完成数据分析任务并生成可视化报告。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值