oneid與使用者標籤之間的相互打通 實現使用者標籤
阿新 • • 發佈:2020-08-10
oneid與使用者標籤之間的相互打通 實現使用者畫像打標行為
附錄說明:
打個標籤盯住他
實現完整的使用者標籤需要以完整的oneid生成且oneid定時更新新增使用者 才可以實現
整個流程:
所用元件 hive-》es(與hive產生對映的表)
資料處理流程
1、使用資料來源資料進行資料清理(整理出關聯資料與將要打標的資料值)
ods_tds_ddc_sqoop.crmc01c樣例資料
2、將關聯鍵值與oneid對應關聯起來 並整理好將要打標值的外部關聯格式化
oneid_data.oneid_data_sink_id_mapping資料樣式
lable.lable_new_dict標籤字典樣例資料
3、資料打標形成格式
(oneid label1) (oneid label2) (oneid label3) 的格式也可以(oneid label1,label2,label3)均可
- 1
- 2
- 3
- 4
–資料插入中間表 處理出每條資料的唯一ID和對應ID身份 以及每個打標職位
insert into lable.dws_oneid_hq_app_lable
select
null AS user_name
,user_id
,null AS user_mobile
,null AS user_birthday
,null AS user_registime
,null AS user_lastlogintime
,null AS user_address
,label_code
,uni
,label_name
,id_mapping
from
(--關聯鍵值用作與字典表對接使用
select
user_id
,uni
,id_mapping
from
(--選出每個id對應的最大數值用於去重
select
user_id
,CONCAT_WS('&&',max(vsex),max(VMARRIGE)) AS valu
,id_mapping
from
(
--資料格式化處理 與對接外部值關聯選擇
select
case
when length(VMOBILE)=11 then VMOBILE
when length(VLKMOBILE)=11 then VLKMOBILE
when length(VCERTIFICATENO) is not null then VCERTIFICATENO
else null end ss
,case
when vsex="0" then "男"
when vsex="1" then "女"
else "性別_其他" end vsex
,CASE
WHEN VMARRIGE ='01' or VMARRIGE ='已婚' THEN '已婚'
WHEN VMARRIGE ='02' or VMARRIGE ='未婚' THEN '未婚'
ELSE '婚姻_其他' END VMARRIGE
from
ods_tds_ddc_sqoop.crmc01c
)sou
left join
( --使用行轉列的方式 將oneid轉換成對應多行
select
user_id
,id_mapping
,split(unio,'#&')[1] AS con
from
oneid_data.oneid_data_sink_id_mapping lateral view explode(split(id_mapping, ','))tt as unio
where
dt='${dt}'
) oneid
on sou.ss=oneid.con
where sou.ss is not null
and oneid.user_id is not null
group by user_id,id_mapping
) tt lateral view explode(split(valu, '&&'))tt as uni
) etl
left join
(select label_name_data,label_code,label_name from lable.lable_new_dict where label_name_data is not null )
dict
on etl.uni=dict.label_name_data;
4、資料輸入中間表 目的是彙總多個源產生的資料標籤方便下一步彙總使用
HIVE建表語句:
dws_oneid_hq_app_lable(中間層建表)
CREATE TABLE `lable.dws_oneid_hq_app_lable`(
user_name String COMMENT '使用者名稱稱',
user_id String COMMENT '使用者id',
user_mobile String COMMENT '使用者手機號',
user_birthday String COMMENT '使用者生日',
user_registime String COMMENT '使用者註冊時間',
user_lastlogintime String COMMENT '使用者最後登入時間',
user_address String COMMENT '使用者地址',
lable String COMMENT '標籤',
label_name String COMMENT '標籤名稱',
label_name_data String COMMENT '資料標籤名稱',
id_mapping String COMMENT '使用者身份集合'
)
COMMENT 'oneid打通標籤資料彙總第一步中間表';
5、建立hive最終結果表
--結果表對應 es的表建立
tdm_oneid_hq_app_lable(對應es表標籤彙總表)
add jar hdfs:///user/es_hadoop/elasticsearch-hadoop-7.3.2.jar;
CREATE EXTERNAL TABLE `lable.tdm_oneid_hq_app_lable`(
user_name String COMMENT '使用者名稱稱',
user_id String COMMENT '使用者id',
user_mobile String COMMENT '使用者手機號',
user_birthday String COMMENT '使用者生日',
user_registime String COMMENT '使用者註冊時間',
user_lastlogintime String COMMENT '使用者最後登入時間',
user_address String COMMENT '使用者地址',
lable String COMMENT '標籤',
id_mapping String COMMENT '使用者身份集合'
)
COMMENT '標籤資料彙總'
STORED BY 'org.elasticsearch.hadoop.hive.EsStorageHandler'
TBLPROPERTIES (
'es.resource' = 'tdm_oneid_hq_app_lable',
'es.nodes'='prod.dbaas.private',
'es.port'='19204',
'es.mapping.id' = 'user_id',
'es.write.operation'='upsert',
'es.mapping.names'='user_name:user_name,user_id:user_id,user_mobile:user_mobile
,user_birthday:user_birthday,user_registime:user_registime,user_lastlogintime:user_lastlogintime
,user_address:user_address,lable:lable,id_mapping:id_mapping'
)
6、對應建立es表建立對映關係
部分因為業務需求需要建立成分詞形式以便於後期查詢使用
ES建表語句
PUT tdm_oneid_hq_app_lable/?include_type_name=true
{
"mappings":{
"_doc":{
"properties":{
"user_name":{
"type":"keyword"
},
"user_id":{
"type":"keyword"
},
"user_mobile":{
"type":"keyword"
},
"user_birthday":{
"type":"keyword"
},
"user_registime":{
"type":"keyword"
},
"user_lastlogintime":{
"type":"keyword"
},
"user_address":{
"type":"keyword"
},
"lable":{
"type":"text",
"analyzer": "standard"
},
"id_mapping":{
"type":"text",
"analyzer": "standard"
}
}
}
}
}
7、從中間表讀取資料進行彙總
add jar hdfs:///user/es_hadoop/elasticsearch-hadoop-7.3.2.jar;
add jar hdfs:///user/es_hadoop/httpclient-4.5.5.jar;
add jar hdfs:///user/es_hadoop/org.apache.commons.httpclient.jar;
insert into lable.tdm_oneid_hq_app_lable
select
max(user_name) -- '使用者名稱稱',
,max(user_id) -- '使用者id',
,max(user_mobile) -- '使用者手機號',
,max(user_birthday) -- '使用者生日',
,max(user_registime) -- '使用者註冊時間',
,max(user_lastlogintime) -- '使用者最後登入時間',
,max(user_address) -- '使用者地址',
,CONCAT_WS(',',collect_set(lable))
,id_mapping -- '使用者身份集合'
from lable.dws_oneid_hq_app_lable
group by
user_id -- '使用者id',
,id_mapping -- '使用者身份集合'
8、資料結果輸入到建立對映的hive表中在es中查詢
GET tdm_oneid_hq_app_lable/_search
- 1
辛苦碼字如有轉載請標明出處謝謝!——拜耳法
都看到這裡了非常感謝!
本片章暫未完結 有疑問請+vx :baierfa