|
| 1 | +# 数仓数据导出 |
| 2 | + |
| 3 | +为了方便报表应用使用数据,需将ADS各项指标统计结果导出到MySQL,方便熟悉 SQL 人员使用。 |
| 4 | + |
| 5 | +## 1 MySQL建库建表 |
| 6 | + |
| 7 | +### 1.1 创建数据库 |
| 8 | + |
| 9 | +创建car_data_report数据库: |
| 10 | + |
| 11 | +```sql |
| 12 | +CREATE DATABASE IF NOT EXISTS car_data_report |
| 13 | +# 字符集 |
| 14 | +DEFAULT CHARSET utf8mb4 |
| 15 | +# 排序规则 |
| 16 | +COLLATE utf8mb4_general_ci; |
| 17 | +``` |
| 18 | + |
| 19 | +#### 1.1.2 创建表 |
| 20 | + |
| 21 | +##### ① 里程相关统计 |
| 22 | + |
| 23 | +创建ads_mileage_stat_last_month表,存储里程相关统计数据。 |
| 24 | + |
| 25 | +```sql |
| 26 | +DROP TABLE IF EXISTS ads_mileage_stat_last_month; |
| 27 | + |
| 28 | +CREATE TABLE ads_mileage_stat_last_month ( |
| 29 | + vin VARCHAR(20) COMMENT '汽车唯一ID', |
| 30 | + mon VARCHAR(7) COMMENT '统计月份', |
| 31 | + avg_mileage INT COMMENT '日均里程', |
| 32 | + avg_speed DECIMAL(16, 2) COMMENT '平均时速分子', |
| 33 | + danger_count DECIMAL(16, 2) COMMENT '平均百公里急加减速次数' |
| 34 | +) COMMENT '里程相关统计'; |
| 35 | +``` |
| 36 | + |
| 37 | +##### ② 告警相关统计 |
| 38 | + |
| 39 | +创建ads_alarm_stat_last_month表,存储告警相关的统计数据。 |
| 40 | + |
| 41 | +```sql |
| 42 | +DROP TABLE IF EXISTS ads_alarm_stat_last_month; |
| 43 | + |
| 44 | +CREATE TABLE ads_alarm_stat_last_month ( |
| 45 | + vin VARCHAR(20) COMMENT '汽车唯一ID', |
| 46 | + mon VARCHAR(7) COMMENT '统计月份', |
| 47 | + alarm_count INT COMMENT '告警次数', |
| 48 | + l1_alarm_count INT COMMENT '一级告警次数', |
| 49 | + l2_alarm_count INT COMMENT '二级告警次数', |
| 50 | + l3_alarm_count INT COMMENT '三级告警次数' |
| 51 | +) COMMENT '告警相关统计'; |
| 52 | +``` |
| 53 | + |
| 54 | +**3)温控相关统计** |
| 55 | + |
| 56 | +创建ads_temperature_stat_last_month表,存储温控相关的统计数据。 |
| 57 | + |
| 58 | +```sql |
| 59 | +DROP TABLE IF EXISTS ads_temperature_stat_last_month; |
| 60 | + |
| 61 | +CREATE TABLE ads_temperature_stat_last_month ( |
| 62 | + vin VARCHAR(20) COMMENT '汽车唯一ID', |
| 63 | + mon VARCHAR(7) COMMENT '统计月份', |
| 64 | + max_motor_temperature INT COMMENT '电机最高温度', |
| 65 | + avg_motor_temperature DECIMAL(16, 2) COMMENT '电机平均温度', |
| 66 | + max_motor_controller_temperature INT COMMENT '电机控制器最高温度', |
| 67 | + avg_motor_controller_temperature DECIMAL(16, 2) COMMENT '电机控制器平均温度', |
| 68 | + max_battery_temperature INT COMMENT '最高电池温度', |
| 69 | + battery_temperature_abnormal_count INT COMMENT '电池温度异常值次数' |
| 70 | +) COMMENT '温控相关统计'; |
| 71 | +``` |
| 72 | + |
| 73 | +**4)能耗相关统计** |
| 74 | + |
| 75 | +创建ads_consume_stat_last_month表,存储能耗相关的统计数据。 |
| 76 | + |
| 77 | +```sql |
| 78 | +DROP TABLE IF EXISTS ads_consume_stat_last_month; |
| 79 | + |
| 80 | +CREATE TABLE ads_consume_stat_last_month ( |
| 81 | + vin VARCHAR(20) COMMENT '汽车唯一ID', |
| 82 | + mon VARCHAR(7) COMMENT '统计月份', |
| 83 | + soc_per_charge DECIMAL(16, 2) COMMENT '次均充电电量', |
| 84 | + duration_per_charge DECIMAL(16, 2) COMMENT '次均充电时长', |
| 85 | + charge_count INT COMMENT '充电次数', |
| 86 | + fast_charge_count INT COMMENT '快充次数', |
| 87 | + slow_charge_count INT COMMENT '慢充次数', |
| 88 | + fully_charge_count INT COMMENT '深度充电次数', |
| 89 | + soc_per_100km DECIMAL(16, 2) COMMENT 'soc百公里平均消耗', |
| 90 | + soc_per_run DECIMAL(16, 2) COMMENT '每次里程soc平均消耗', |
| 91 | + soc_last_100km DECIMAL(16, 2) COMMENT '最近百公里soc消耗' |
| 92 | +) COMMENT '能耗主题统计'; |
| 93 | +``` |
| 94 | + |
| 95 | +## 2 数据导出 |
| 96 | + |
| 97 | +DataX作为数据导出工具,并选择HDFSReader和MySQLWriter作为数据源和目标。 |
| 98 | + |
| 99 | +### 2.1 编写DataX配置文件 |
| 100 | + |
| 101 | +我们需要为每个表编写一个DataX配置文件。以ads_alarm_stat_last_month为例: |
| 102 | + |
| 103 | +```json |
| 104 | +{ |
| 105 | + "job": { |
| 106 | + "setting": { |
| 107 | + "speed": { |
| 108 | + "channel": 1 // DataX 作业的并发通道数,一般根据系统资源进行调整,1 表示单通道 |
| 109 | + } |
| 110 | + }, |
| 111 | + "content": [ |
| 112 | + { |
| 113 | + "reader": { |
| 114 | + ... |
| 115 | + }, |
| 116 | + "writer": { |
| 117 | + "name": "mysqlwriter", // 写入数据的插件类型为 MySQL 数据库写入 |
| 118 | + "parameter": { |
| 119 | + "writeMode": "replace", // 写入模式为替换(如果表存在则先删除再写入) |
| 120 | + "username": "root", // 数据库用户名 |
| 121 | + "password": "000000", // 数据库密码 |
| 122 | + "column": [ // 写入的列信息,包括 vin、mon、alarm_count、l1_alarm_count、l2_alarm_count、l3_alarm_count |
| 123 | + "vin", |
| 124 | + "mon", |
| 125 | + "alarm_count", |
| 126 | + "l1_alarm_count", |
| 127 | + "l2_alarm_count", |
| 128 | + "l3_alarm_count" |
| 129 | + ], |
| 130 | + "connection": [ // 数据库连接信息列表,支持多个数据库连接 |
| 131 | + { |
| 132 | + "jdbcUrl": "jdbc:mysql://hadoop102:3306/car_data_report?useSSL=false&allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=utf-8", // MySQL 数据库连接地址,设置了 SSL、公钥检索、Unicode 编码等参数 |
| 133 | + "table": [ // 写入的数据库表列表,这里只写入 ads_alarm_stat_last_month 表 |
| 134 | + "ads_alarm_stat_last_month" |
| 135 | + ] |
| 136 | + } |
| 137 | + ] |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + ] |
| 142 | + } |
| 143 | +} |
| 144 | +``` |
| 145 | + |
| 146 | +导出路径参数path并未写死,需在提交任务时通过参数动态传入,参数名称为exportdir。 |
| 147 | + |
| 148 | +模版配置参数解析: |
| 149 | + |
| 150 | +HDFSReader: |
| 151 | + |
| 152 | + |
| 153 | + |
| 154 | +即: |
| 155 | + |
| 156 | +```json |
| 157 | +"reader": { |
| 158 | + "name": "hdfsreader", // 读取数据的插件类型为 HDFS 文件读取 |
| 159 | + "parameter": { |
| 160 | + "path": "${exportdir}", // HDFS 文件路径,使用 ${exportdir} 变量表示动态路径 |
| 161 | + "defaultFS": "hdfs://hadoop102:8020", // HDFS 默认文件系统地址 |
| 162 | + "column": [ // 需要读取的列信息,这里使用通配符 * 表示读取所有列 |
| 163 | + "*" |
| 164 | + ], |
| 165 | + "fileType": "text", // 文件类型为文本文件 |
| 166 | + "encoding": "UTF-8", // 文件编码格式为 UTF-8 |
| 167 | + "fieldDelimiter": "\t", // 字段分隔符为制表符 |
| 168 | + "nullFormat": "\\N" // 空值格式为 \N |
| 169 | + } |
| 170 | +}, |
| 171 | +``` |
| 172 | + |
| 173 | +MySQLWriter: |
| 174 | + |
| 175 | + |
| 176 | + |
| 177 | +```json |
| 178 | +"writer": { |
| 179 | + "name": "mysqlwriter", // 写入数据的插件类型为 MySQL 数据库写入 |
| 180 | + "parameter": { |
| 181 | + "writeMode": "replace", // 写入模式为替换(如果表存在则先删除再写入) |
| 182 | + "username": "root", // 数据库用户名 |
| 183 | + "password": "000000", // 数据库密码 |
| 184 | + "column": [ // 写入的列信息,包括 vin、mon、alarm_count、l1_alarm_count、l2_alarm_count、l3_alarm_count |
| 185 | + "vin", |
| 186 | + "mon", |
| 187 | + "alarm_count", |
| 188 | + "l1_alarm_count", |
| 189 | + "l2_alarm_count", |
| 190 | + "l3_alarm_count" |
| 191 | + ], |
| 192 | + "connection": [ // 数据库连接信息列表,支持多个数据库连接 |
| 193 | + { |
| 194 | + "jdbcUrl": "jdbc:mysql://hadoop102:3306/car_data_report?useSSL=false&allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=utf-8", // MySQL 数据库连接地址,设置了 SSL、公钥检索、Unicode 编码等参数 |
| 195 | + "table": [ // 写入的数据库表列表,这里只写入 ads_alarm_stat_last_month 表 |
| 196 | + "ads_alarm_stat_last_month" |
| 197 | + ] |
| 198 | + } |
| 199 | + ] |
| 200 | + } |
| 201 | +} |
| 202 | +``` |
| 203 | + |
| 204 | +### 2.2 DataX配置文件生成脚本 |
| 205 | + |
| 206 | +将 **TODO(在下载的资料压缩包里)**datax_config_generator拷贝到/opt/module。 |
| 207 | + |
| 208 | +修改/opt/module/datax_config_generator/configuration.properties: |
| 209 | + |
| 210 | +```bash |
| 211 | +mysql.username=root |
| 212 | +mysql.password=000000 |
| 213 | +mysql.host=hadoop102 |
| 214 | +mysql.port=3306 |
| 215 | +mysql.database.import=car_data |
| 216 | +mysql.database.export=car_data_report |
| 217 | +mysql.tables.import= |
| 218 | +mysql.tables.export= |
| 219 | +is.seperated.tables=0 |
| 220 | +hdfs.uri=hdfs://hadoop102:8020 |
| 221 | +import_out_dir=/opt/module/datax/job/import |
| 222 | +export_out_dir=/opt/module/datax/job/export |
| 223 | +``` |
| 224 | + |
| 225 | +执行配置文件生成器: |
| 226 | + |
| 227 | +```bash |
| 228 | +java -jar datax-config-generator-1.0.1-jar-with-dependencies.jar |
| 229 | +``` |
| 230 | + |
| 231 | +观察生成的配置文件: |
| 232 | + |
| 233 | +```bash |
| 234 | +ll /opt/module/datax/job/export/ |
| 235 | + |
| 236 | +总用量 20 |
| 237 | +-rw-rw-r--. 1 atguigu atguigu 961 4月 26 19:47 car_data_report.ads_alarm_stat_last_month.json |
| 238 | +-rw-rw-r--. 1 atguigu atguigu 1095 4月 26 19:47 car_data_report.ads_consume_stat_last_month.json |
| 239 | +-rw-rw-r--. 1 atguigu atguigu 1062 4月 26 19:47 car_data_report.ads_electric_stat_last_month.json |
| 240 | +-rw-rw-r--. 1 atguigu atguigu 939 4月 26 19:47 car_data_report.ads_mileage_stat_last_month.json |
| 241 | +-rw-rw-r--. 1 atguigu atguigu 1083 4月 26 19:47 car_data_report.ads_temperature_stat_last_month.json |
| 242 | +``` |
| 243 | + |
| 244 | +### 2.3 测试生成的DataX配置文件 |
| 245 | + |
| 246 | +以ads_trans_order_stats为例,测试用脚本生成的配置文件是否可用。 |
| 247 | + |
| 248 | +**1)执行DataX同步命令** |
| 249 | + |
| 250 | +```bash |
| 251 | +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 |
| 252 | +``` |
| 253 | + |
| 254 | +**2)观察同步结果** |
| 255 | + |
| 256 | +观察MySQL目标表是否出现数据。 |
| 257 | + |
| 258 | + |
| 259 | + |
| 260 | +### 2.4 编写导出脚本 |
| 261 | + |
| 262 | +创建hdfs_to_mysql.sh |
| 263 | + |
| 264 | +```bash |
| 265 | +vim hdfs_to_mysql.sh |
| 266 | +``` |
| 267 | + |
| 268 | +```bash |
| 269 | +#!/bin/bash |
| 270 | + |
| 271 | +# 设置 DataX 的安装路径 |
| 272 | +DATAX_HOME=/opt/module/datax |
| 273 | + |
| 274 | +# 清理指定路径下的空文件 |
| 275 | +# 参数 $1: 待清理的路径 |
| 276 | +handle_export_path() { |
| 277 | + for file in $(hadoop fs -ls -R "$1" | awk '{print $8}'); do |
| 278 | + # 检查文件是否为空 |
| 279 | + if hadoop fs -test -z "$file"; then |
| 280 | + echo "$file 文件大小为0,正在删除..." |
| 281 | + # 删除空文件 |
| 282 | + hadoop fs -rm -r -f "$file" |
| 283 | + fi |
| 284 | + done |
| 285 | +} |
| 286 | + |
| 287 | +# 导出数据到指定路径 |
| 288 | +# 参数 $1: DataX 配置文件路径 |
| 289 | +# 参数 $2: 导出路径 |
| 290 | +export_data() { |
| 291 | + datax_config="$1" |
| 292 | + export_dir="$2" |
| 293 | + # 调用清理空文件函数 |
| 294 | + handle_export_path "$export_dir" |
| 295 | + # 执行 DataX 导出命令 |
| 296 | + $DATAX_HOME/bin/datax.py -p"-Dexportdir=$export_dir" "$datax_config" |
| 297 | +} |
| 298 | + |
| 299 | +# 主逻辑,根据传入的参数执行数据导出操作 |
| 300 | +case $1 in |
| 301 | + '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') |
| 302 | + # 导出单个表的数据 |
| 303 | + export_data "/opt/module/datax/job/export/car_data_report.$1.json" "/warehouse/car_data/ads/$1" |
| 304 | + ;; |
| 305 | + 'all') |
| 306 | + # 导出所有表的数据 |
| 307 | + 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 |
| 308 | + export_data "/opt/module/datax/job/export/car_data_report.$table.json" "/warehouse/car_data/ads/$table" |
| 309 | + done |
| 310 | + ;; |
| 311 | + *) |
| 312 | + # 未知参数,打印提示信息 |
| 313 | + echo "Usage: $0 {ads_table_name | all}" |
| 314 | + echo "Example: $0 ads_mileage_stat_last_month" |
| 315 | + ;; |
| 316 | +esac |
| 317 | +``` |
| 318 | + |
| 319 | +```bash |
| 320 | +chmod +x hdfs_to_mysql.sh |
| 321 | + |
| 322 | +hdfs_to_mysql.sh all |
| 323 | +``` |
0 commit comments