這篇博客主要彙總了我在具體項目中一些處理步驟。
1、項目背景:
首先介紹一個這裏會提到的一個trace表,這個表是之前項目中我彙總了各類信息,包括人臉識別後的對比數據、某學校卡口的進入車輛數據、訪客記錄、門禁刷卡記錄與一卡通消費記錄等。因爲其中很多字段其實是重複的,所以彙總在這個表裏。然後會有一個屬性告知這條數據是來源於何種表,這樣統計各種統一要獲取的信息。比如:通過一卡通在時間段內的消費記錄判斷某學生是否異常消費、通過一週的門禁刷卡記錄與攝像頭比對記錄判斷學生是否在校等。這些不是這次總結的內容,都是之前項目已經完成的。只是之前每天彙總了trace表數據後,再統計一次今天所有的數據,但是現在甲方有些需求需要我們每個小時更新一次,比如在某小時內是否有同學出現了異常消費。所以之前的博客都是做一些鋪墊。
2、hive與MySQL中創建的對應表
hive:
CREATE TABLE activemq_topic.trace_copy (
alarmtime string,
num string,
xm string,
type string,
indexcode string,
date string,
timehour string,
detail string,
fromtype string
)partitioned by (datet string,hours string);
這裏跟之前要求添加的數據一樣創建日期與小時的雙分區。
MySQL:
然後同樣要在mysql中創建同樣字段與屬性的對應表:
3、修改插入hive中trace表的指令
A.修改hive中各種數據彙總與trace表的語句:
可以先看之前我隔天統計一次的指令,一開始我分區就按照日期分區:
insert overwrite table activemq_topic.trace partition(date_time='${lastDay}')
SELECT h.credentialsNum num,h.humanName xm,type,indexCode,alarmTime,SUBSTR(s.alarmTime,0,10) as date,SUBSTR(s.alarmTime,11,3) as timeHour,s.bkgpicurl as detail,'抓拍機' as fromType
FROM activemq_topic.snap_match s LEFT JOIN activemq_topic.humans h ON s.humanId = h.humanId
WHERE alarmTime > '${lastDay}' AND alarmTime <= '${thisDay}'
union all
SELECT .......................
按照小時則修改爲以下指令:
insert overwrite table activemq_topic.trace_copy partition(datet='${date_today}',hours='${hour}')
SELECT h.credentialsNum num,h.humanName xm,type,indexCode,alarmTime,SUBSTR(s.alarmTime,0,10) as date,SUBSTR(s.alarmTime,11,3) as timeHour,s.bkgpicurl as detail,'抓拍機' as fromType
FROM activemq_topic.snap_match_copy s LEFT JOIN activemq_topic.humans h ON s.humanId = h.humanId
WHERE `datet`='${date_today}' AND `hours`='${hour}'
union all
SELECT yhm ..............................
這裏我們插入時候就設定之前創建trace的兩個日期與小時分區。還有這裏因爲這些我所需要查詢的表也都已經按照日期還有小時分區了,所以他們的判斷條件就是 `datet`='${date_today}' AND `hours`='${hour}' 。
B.插入hive的數據已經有了,接下去就是從hive導入數據到mysql中的sqoop指令了。
export --connect jdbc:mysql://xdata4:3306/jxcy_dpxs?characterEncoding=utf8 --username root --password P@ssw0rd4321 --table trace_copy --fields-terminated-by "\001" --input-null-string "\\N" --input-null-non-string "\\N" --export-dir /user/hive/warehouse/activemq_topic.db/trace_copy/datet=${date_today}/hours=${hour}
很多參數跟import的參數差不多,就不多說了。記得前面是export,別弄錯了。這裏的--fields-terminated-by參數還是要再提一下,這個是字段分割符。hive默認是 '\001',這個需要跟我們在hive加載數據時的一致,不然會出錯。我那時候就是用的 '\001' 所以這裏也還是'\001'。還有 --export-dir 這個參數,他表示存放數據的 HDFS 的源目錄。這個爲了弄清楚我也去找了一下具體的位置:
這裏拓展一下,可以看這裏總結的sqoop的各種指令參數介紹:
https://blog.csdn.net/JJBOOM425/article/details/105560551
C.這兩個都寫好了,就可以在HUE上的WorkFlow中配置工作流了:
注意:
- 這樣子已經之前寫好的hive與sqoop需要用 DOCUMENTS 中的hive、sqoop來創建工作流。
- 裏面有用到參數,所以還要再 PARAMETERS 中添加參數。
- 對於下面的Sqoop,因爲涉及到對MySQL的操作,所以要添加對應的驅動,只要提前下好驅動程序,然後在FILES中添加即可。
綜上就是這此工作的總結。具體這個WorkFlow如何設計將會在下篇博客中總結。