離線數倉建設之數據導出

爲了方便報表應用使用數據,需將ADS各項指標統計結果導出到MySQL,方便熟悉 SQL 人員使用。

1 MySQL建庫建表

1.1 創建數據庫

創建car_data_report數據庫:

CREATE DATABASE IF NOT EXISTS car_data_report
# 字符集
DEFAULT CHARSET utf8mb4
# 排序規則
COLLATE utf8mb4_general_ci;

1.1.2 創建表

① 里程相關統計

創建ads_mileage_stat_last_month表,存儲里程相關統計數據。

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 '統計月份',
  avg_mileage INT COMMENT '日均里程',
  avg_speed DECIMAL(16, 2) COMMENT '平均時速分子',
  danger_count DECIMAL(16, 2) COMMENT '平均百公里急加減速次數'
) COMMENT '里程相關統計';
② 告警相關統計

創建ads_alarm_stat_last_month表,存儲告警相關的統計數據。

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 '統計月份',
  alarm_count INT COMMENT '告警次數',
  l1_alarm_count INT COMMENT '一級告警次數',
  l2_alarm_count INT COMMENT '二級告警次數',
  l3_alarm_count INT COMMENT '三級告警次數'
) COMMENT '告警相關統計';

3)溫控相關統計

創建ads_temperature_stat_last_month表,存儲溫控相關的統計數據。

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 '統計月份',
  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 '電池溫度異常值次數'
) COMMENT '溫控相關統計';

4)能耗相關統計

創建ads_consume_stat_last_month表,存儲能耗相關的統計數據。

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 '統計月份',
  soc_per_charge DECIMAL(16, 2) COMMENT '次均充電電量',
  duration_per_charge DECIMAL(16, 2) COMMENT '次均充電時長',
  charge_count INT COMMENT '充電次數',
  fast_charge_count INT COMMENT '快充次數',
  slow_charge_count INT COMMENT '慢充次數',
  fully_charge_count INT COMMENT '深度充電次數',
  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 '能耗主題統計';

2 數據導出

DataX作爲數據導出工具,並選擇HDFSReader和MySQLWriter作爲數據源和目標。

2.1 編寫DataX配置文件

我們需要爲每個表編寫一個DataX配置文件。以ads_alarm_stat_last_month爲例:

{
  "job": {
    "setting": {
      "speed": {
        "channel": 1  // DataX 作業的併發通道數,一般根據系統資源進行調整,1 表示單通道
      }
    },
    "content": [
      {
        "reader": {
          ...
        },
        "writer": {
          "name": "mysqlwriter",  // 寫入數據的插件類型爲 MySQL 數據庫寫入
          "parameter": {
            "writeMode": "replace",  // 寫入模式爲替換(如果表存在則先刪除再寫入)
            "username": "root",  // 數據庫用戶名
            "password": "000000",  // 數據庫密碼
            "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": [  // 數據庫連接信息列表,支持多個數據庫連接
              {
                "jdbcUrl": "jdbc:mysql://hadoop102:3306/car_data_report?useSSL=false&allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=utf-8",  // MySQL 數據庫連接地址,設置了 SSL、公鑰檢索、Unicode 編碼等參數
                "table": [  // 寫入的數據庫表列表,這裏只寫入 ads_alarm_stat_last_month 表
                  "ads_alarm_stat_last_month"
                ]
              }
            ]
          }
        }
      }
    ]
  }
}

導出路徑參數path並未寫死,需在提交任務時通過參數動態傳入,參數名稱爲exportdir。

模版配置參數解析:

HDFSReader:

即:

"reader": {
  "name": "hdfsreader",  // 讀取數據的插件類型爲 HDFS 文件讀取
  "parameter": {
    "path": "${exportdir}",  // HDFS 文件路徑,使用 ${exportdir} 變量表示動態路徑
    "defaultFS": "hdfs://hadoop102:8020",  // HDFS 默認文件系統地址
    "column": [  // 需要讀取的列信息,這裏使用通配符 * 表示讀取所有列
      "*"
    ],
    "fileType": "text",  // 文件類型爲文本文件
    "encoding": "UTF-8",  // 文件編碼格式爲 UTF-8
    "fieldDelimiter": "\t",  // 字段分隔符爲製表符
    "nullFormat": "\\N"  // 空值格式爲 \N
  }
},

MySQLWriter:

"writer": {
  "name": "mysqlwriter",  // 寫入數據的插件類型爲 MySQL 數據庫寫入
  "parameter": {
    "writeMode": "replace",  // 寫入模式爲替換(如果表存在則先刪除再寫入)
    "username": "root",  // 數據庫用戶名
    "password": "000000",  // 數據庫密碼
    "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": [  // 數據庫連接信息列表,支持多個數據庫連接
      {
        "jdbcUrl": "jdbc:mysql://hadoop102:3306/car_data_report?useSSL=false&allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=utf-8",  // MySQL 數據庫連接地址,設置了 SSL、公鑰檢索、Unicode 編碼等參數
        "table": [  // 寫入的數據庫表列表,這裏只寫入 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

執行配置文件生成器:

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)執行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目標表是否出現數據。

2.4 編寫導出腳本

創建hdfs_to_mysql.sh

vim hdfs_to_mysql.sh
#!/bin/bash

# 設置 DataX 的安裝路徑
DATAX_HOME=/opt/module/datax

# 清理指定路徑下的空文件
# 參數 $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
}

# 導出數據到指定路徑
# 參數 $1: DataX 配置文件路徑
# 參數 $2: 導出路徑
export_data() {
  datax_config="$1"
  export_dir="$2"
  # 調用清理空文件函數
  handle_export_path "$export_dir"
  # 執行 DataX 導出命令
  $DATAX_HOME/bin/datax.py -p"-Dexportdir=$export_dir" "$datax_config"
}

# 主邏輯,根據傳入的參數執行數據導出操作
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')
    # 導出單個表的數據
    export_data "/opt/module/datax/job/export/car_data_report.$1.json" "/warehouse/car_data/ads/$1"
    ;;
  'all')
    # 導出所有表的數據
    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
    ;;
  *)
    # 未知參數,打印提示信息
    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

關注我,緊跟本系列專欄文章,咱們下篇再續!

作者簡介:魔都技術專家兼架構,多家大廠後端一線研發經驗,各大技術社區頭部專家博主。具有豐富的引領團隊經驗,深厚業務架構和解決方案的積累。

負責:

  • 中央/分銷預訂系統性能優化
  • 活動&優惠券等營銷中臺建設
  • 交易平臺及數據中臺等架構和開發設計

目前主攻降低軟件複雜性設計、構建高可用系統方向。

參考:

本文由博客一文多發平臺 OpenWrite 發佈!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章