為了方便報表應用使用數(shù)據(jù),需將ADS各項指標統(tǒng)計結果導出到MySQL,方便熟悉 SQL 人員使用。
1 MySQL建庫建表
1.1 創(chuàng)建數(shù)據(jù)庫
創(chuàng)建car_data_report數(shù)據(jù)庫:
CREATE DATABASE IF NOT EXISTS car_data_report
# 字符集
DEFAULT CHARSET utf8mb4
# 排序規(guī)則
COLLATE utf8mb4_general_ci;
1.1.2 創(chuàng)建表
① 里程相關統(tǒng)計
創(chuàng)建ads_mileage_stat_last_month表,存儲里程相關統(tǒng)計數(shù)據(jù)。
DROP TABLE IF EXISTS ads_mileage_stat_last_month;
CREATE TABLE ads_mileage_stat_last_month (
vin VARCHAR(20) COMMENT '汽車唯一ID',
mon VARCHAR(7) COMMENT '統(tǒng)計月份',
avg_mileage INT COMMENT '日均里程',
avg_speed DECIMAL(16, 2) COMMENT '平均時速分子',
danger_count DECIMAL(16, 2) COMMENT '平均百公里急加減速次數(shù)'
) COMMENT '里程相關統(tǒng)計';
② 告警相關統(tǒng)計
創(chuàng)建ads_alarm_stat_last_month表,存儲告警相關的統(tǒng)計數(shù)據(jù)。
DROP TABLE IF EXISTS ads_alarm_stat_last_month;
CREATE TABLE ads_alarm_stat_last_month (
vin VARCHAR(20) COMMENT '汽車唯一ID',
mon VARCHAR(7) COMMENT '統(tǒng)計月份',
alarm_count INT COMMENT '告警次數(shù)',
l1_alarm_count INT COMMENT '一級告警次數(shù)',
l2_alarm_count INT COMMENT '二級告警次數(shù)',
l3_alarm_count INT COMMENT '三級告警次數(shù)'
) COMMENT '告警相關統(tǒng)計';
3)溫控相關統(tǒng)計
創(chuàng)建ads_temperature_stat_last_month表,存儲溫控相關的統(tǒng)計數(shù)據(jù)。
DROP TABLE IF EXISTS ads_temperature_stat_last_month;
CREATE TABLE ads_temperature_stat_last_month (
vin VARCHAR(20) COMMENT '汽車唯一ID',
mon VARCHAR(7) COMMENT '統(tǒng)計月份',
max_motor_temperature INT COMMENT '電機最高溫度',
avg_motor_temperature DECIMAL(16, 2) COMMENT '電機平均溫度',
max_motor_controller_temperature INT COMMENT '電機控制器最高溫度',
avg_motor_controller_temperature DECIMAL(16, 2) COMMENT '電機控制器平均溫度',
max_battery_temperature INT COMMENT '最高電池溫度',
battery_temperature_abnormal_count INT COMMENT '電池溫度異常值次數(shù)'
) COMMENT '溫控相關統(tǒng)計';
4)能耗相關統(tǒng)計
創(chuàng)建ads_consume_stat_last_month表,存儲能耗相關的統(tǒng)計數(shù)據(jù)。
DROP TABLE IF EXISTS ads_consume_stat_last_month;
CREATE TABLE ads_consume_stat_last_month (
vin VARCHAR(20) COMMENT '汽車唯一ID',
mon VARCHAR(7) COMMENT '統(tǒng)計月份',
soc_per_charge DECIMAL(16, 2) COMMENT '次均充電電量',
duration_per_charge DECIMAL(16, 2) COMMENT '次均充電時長',
charge_count INT COMMENT '充電次數(shù)',
fast_charge_count INT COMMENT '快充次數(shù)',
slow_charge_count INT COMMENT '慢充次數(shù)',
fully_charge_count INT COMMENT '深度充電次數(shù)',
soc_per_100km DECIMAL(16, 2) COMMENT 'soc百公里平均消耗',
soc_per_run DECIMAL(16, 2) COMMENT '每次里程soc平均消耗',
soc_last_100km DECIMAL(16, 2) COMMENT '最近百公里soc消耗'
) COMMENT '能耗主題統(tǒng)計';
2 數(shù)據(jù)導出
DataX作為數(shù)據(jù)導出工具,并選擇HDFSReader和MySQLWriter作為數(shù)據(jù)源和目標。
2.1 編寫DataX配置文件
我們需要為每個表編寫一個DataX配置文件。以ads_alarm_stat_last_month為例:
{
"job": {
"setting": {
"speed": {
"channel": 1 // DataX 作業(yè)的并發(fā)通道數(shù),一般根據(jù)系統(tǒng)資源進行調整,1 表示單通道
}
},
"content": [
{
"reader": {
...
},
"writer": {
"name": "mysqlwriter", // 寫入數(shù)據(jù)的插件類型為 MySQL 數(shù)據(jù)庫寫入
"parameter": {
"writeMode": "replace", // 寫入模式為替換(如果表存在則先刪除再寫入)
"username": "root", // 數(shù)據(jù)庫用戶名
"password": "000000", // 數(shù)據(jù)庫密碼
"column": [ // 寫入的列信息,包括 vin、mon、alarm_count、l1_alarm_count、l2_alarm_count、l3_alarm_count
"vin",
"mon",
"alarm_count",
"l1_alarm_count",
"l2_alarm_count",
"l3_alarm_count"
],
"connection": [ // 數(shù)據(jù)庫連接信息列表,支持多個數(shù)據(jù)庫連接
{
"jdbcUrl": "jdbc:mysql://hadoop102:3306/car_data_report?useSSL=false&allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=utf-8", // MySQL 數(shù)據(jù)庫連接地址,設置了 SSL、公鑰檢索、Unicode 編碼等參數(shù)
"table": [ // 寫入的數(shù)據(jù)庫表列表,這里只寫入 ads_alarm_stat_last_month 表
"ads_alarm_stat_last_month"
]
}
]
}
}
}
]
}
}
導出路徑參數(shù)path并未寫死,需在提交任務時通過參數(shù)動態(tài)傳入,參數(shù)名稱為exportdir。
模版配置參數(shù)解析:
HDFSReader:
即:
"reader": {
"name": "hdfsreader", // 讀取數(shù)據(jù)的插件類型為 HDFS 文件讀取
"parameter": {
"path": "${exportdir}", // HDFS 文件路徑,使用 ${exportdir} 變量表示動態(tài)路徑
"defaultFS": "hdfs://hadoop102:8020", // HDFS 默認文件系統(tǒng)地址
"column": [ // 需要讀取的列信息,這里使用通配符 * 表示讀取所有列
"*"
],
"fileType": "text", // 文件類型為文本文件
"encoding": "UTF-8", // 文件編碼格式為 UTF-8
"fieldDelimiter": "\t", // 字段分隔符為制表符
"nullFormat": "\\N" // 空值格式為 \N
}
},
MySQLWriter:
"writer": {
"name": "mysqlwriter", // 寫入數(shù)據(jù)的插件類型為 MySQL 數(shù)據(jù)庫寫入
"parameter": {
"writeMode": "replace", // 寫入模式為替換(如果表存在則先刪除再寫入)
"username": "root", // 數(shù)據(jù)庫用戶名
"password": "000000", // 數(shù)據(jù)庫密碼
"column": [ // 寫入的列信息,包括 vin、mon、alarm_count、l1_alarm_count、l2_alarm_count、l3_alarm_count
"vin",
"mon",
"alarm_count",
"l1_alarm_count",
"l2_alarm_count",
"l3_alarm_count"
],
"connection": [ // 數(shù)據(jù)庫連接信息列表,支持多個數(shù)據(jù)庫連接
{
"jdbcUrl": "jdbc:mysql://hadoop102:3306/car_data_report?useSSL=false&allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=utf-8", // MySQL 數(shù)據(jù)庫連接地址,設置了 SSL、公鑰檢索、Unicode 編碼等參數(shù)
"table": [ // 寫入的數(shù)據(jù)庫表列表,這里只寫入 ads_alarm_stat_last_month 表
"ads_alarm_stat_last_month"
]
}
]
}
}
2.2 DataX配置文件生成腳本
將 TODO(在下載的資料壓縮包里)datax_config_generator拷貝到/opt/module。
修改/opt/module/datax_config_generator/configuration.properties:
mysql.username=root
mysql.password=000000
mysql.host=hadoop102
mysql.port=3306
mysql.database.import=car_data
mysql.database.export=car_data_report
mysql.tables.import=
mysql.tables.export=
is.seperated.tables=0
hdfs.uri=hdfs://hadoop102:8020
import_out_dir=/opt/module/datax/job/import
export_out_dir=/opt/module/datax/job/export
執(zhí)行配置文件生成器:
java -jar datax-config-generator-1.0.1-jar-with-dependencies.jar
觀察生成的配置文件:
ll /opt/module/datax/job/export/
總用量 20
-rw-rw-r--. 1 atguigu atguigu 961 4月 26 19:47 car_data_report.ads_alarm_stat_last_month.json
-rw-rw-r--. 1 atguigu atguigu 1095 4月 26 19:47 car_data_report.ads_consume_stat_last_month.json
-rw-rw-r--. 1 atguigu atguigu 1062 4月 26 19:47 car_data_report.ads_electric_stat_last_month.json
-rw-rw-r--. 1 atguigu atguigu 939 4月 26 19:47 car_data_report.ads_mileage_stat_last_month.json
-rw-rw-r--. 1 atguigu atguigu 1083 4月 26 19:47 car_data_report.ads_temperature_stat_last_month.json
2.3 測試生成的DataX配置文件
以ads_trans_order_stats為例,測試用腳本生成的配置文件是否可用。
1)執(zhí)行DataX同步命令
python /opt/module/datax/bin/datax.py -p"-Dexportdir=/warehouse/car_data/ads/ads_order_stats" /opt/module/datax/job/export/tms_report.ads_order_stats.json
2)觀察同步結果
觀察MySQL目標表是否出現(xiàn)數(shù)據(jù)。
2.4 編寫導出腳本
創(chuàng)建hdfs_to_mysql.sh
vim hdfs_to_mysql.sh
#!/bin/bash
# 設置 DataX 的安裝路徑
DATAX_HOME=/opt/module/datax
# 清理指定路徑下的空文件
# 參數(shù) $1: 待清理的路徑
handle_export_path() {
for file in $(hadoop fs -ls -R "$1" | awk '{print $8}'); do
# 檢查文件是否為空
if hadoop fs -test -z "$file"; then
echo "$file 文件大小為0,正在刪除..."
# 刪除空文件
hadoop fs -rm -r -f "$file"
fi
done
}
# 導出數(shù)據(jù)到指定路徑
# 參數(shù) $1: DataX 配置文件路徑
# 參數(shù) $2: 導出路徑
export_data() {
datax_config="$1"
export_dir="$2"
# 調用清理空文件函數(shù)
handle_export_path "$export_dir"
# 執(zhí)行 DataX 導出命令
$DATAX_HOME/bin/datax.py -p"-Dexportdir=$export_dir" "$datax_config"
}
# 主邏輯,根據(jù)傳入的參數(shù)執(zhí)行數(shù)據(jù)導出操作
case $1 in
'ads_mileage_stat_last_month' | 'ads_alarm_stat_last_month' | 'ads_temperature_stat_last_month' | 'ads_electric_stat_last_month' | 'ads_consume_stat_last_month')
# 導出單個表的數(shù)據(jù)
export_data "/opt/module/datax/job/export/car_data_report.$1.json" "/warehouse/car_data/ads/$1"
;;
'all')
# 導出所有表的數(shù)據(jù)
for table in 'ads_mileage_stat_last_month' 'ads_alarm_stat_last_month' 'ads_temperature_stat_last_month' 'ads_electric_stat_last_month' 'ads_consume_stat_last_month'; do
export_data "/opt/module/datax/job/export/car_data_report.$table.json" "/warehouse/car_data/ads/$table"
done
;;
*)
# 未知參數(shù),打印提示信息
echo "Usage: $0 {ads_table_name | all}"
echo "Example: $0 ads_mileage_stat_last_month"
;;
esac
chmod +x hdfs_to_mysql.sh
hdfs_to_mysql.sh all
關注我,緊跟本系列專欄文章,咱們下篇再續(xù)!
作者簡介:魔都技術專家兼架構,多家大廠后端一線研發(fā)經(jīng)驗,各大技術社區(qū)頭部專家博主。具有豐富的引領團隊經(jīng)驗,深厚業(yè)務架構和解決方案的積累。
負責:
- 中央/分銷預訂系統(tǒng)性能優(yōu)化
- 活動&優(yōu)惠券等營銷中臺建設
- 交易平臺及數(shù)據(jù)中臺等架構和開發(fā)設計
目前主攻降低軟件復雜性設計、構建高可用系統(tǒng)方向。
參考:文章來源:http://www.zghlxwxcb.cn/news/detail-840655.html
- 編程嚴選網(wǎng)
本文由博客一文多發(fā)平臺 OpenWrite 發(fā)布!文章來源地址http://www.zghlxwxcb.cn/news/detail-840655.html
到了這里,關于離線數(shù)倉建設之數(shù)據(jù)導出的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!