当前位置: 首页 > ops >正文

DataX从Mysql导数据到Hive分区表案例

0、下载DataX并解压到对应目录
DataX安装包,开箱即用,无需配置。
https://datax-opensource.oss-cn-hangzhou.aliyuncs.com/202308/datax.tar.gz
相关参考文档
https://github.com/alibaba/DataX/blob/master/hdfswriter/doc/hdfswriter.md

1、Hive分区表DDL样例
注意分隔符号要和后续的DataX配置保持一致,同时在此将贴源层数据类型统一为String。

CREATE TABLE datax.fin_transaction_flow (transaction_id     STRING COMMENT '交易唯一ID(主键)',account_no         STRING COMMENT '账户号(外键 -> account_info.account_no)',transaction_code   STRING COMMENT '交易类型编码(外键 -> transaction_reference.transaction_code)',amount             STRING COMMENT '交易金额(格式:整数部分18位,小数2位)',currency           STRING COMMENT '币种(如CNY/USD)',counterparty_account STRING COMMENT '对手账户(外键 -> account_info.account_no)',transaction_time   STRING COMMENT '交易时间(格式:yyyy-MM-dd HH:mm:ss)',status             STRING COMMENT '交易状态(成功/失败)',channel            STRING COMMENT '交易渠道(ATM/网银)'
)
PARTITIONED BY (dt STRING)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\b'
STORED AS ORC ;

2、DataX Json配置样例
创建以下Json文件(mysql-hive.json)并放置到DataX节点合适目录下。

{"job": {"setting": {"speed": {"channel": 4}},"content": [{"reader": {"name": "mysqlreader","parameter": {"username": "root","password": "pwd","connection": [{"querySql": ["select  transaction_id,account_no,transaction_code,amount,currency,counterparty_account,transaction_time,status,channel from fin_transaction_flow where dt='20250416';"],"jdbcUrl": ["jdbc:mysql://chdp01:3306/bg2025"]}]}},"writer": {"name": "hdfswriter","parameter": {"defaultFS": "hdfs://chdp01:9000","fileType": "orc","path": "/user/hive/warehouse/datax.db/fin_transaction_flow/dt=20250416","fileName": "xxxx","column": [{"name": "transaction_id","type": "STRING"},{"name": "account_no","type": "STRING"},{"name": "transaction_code","type": "STRING"},{"name": "amount","type": "STRING"},{"name": "currency","type": "STRING"},{"name": "counterparty_account","type": "STRING"},{"name": "transaction_time","type": "STRING"},{"name": "status","type": "STRING"},{"name": "channel","type": "STRING"}],"writeMode": "append","fieldDelimiter": "\b","compress": "NONE"}}}]}
}

3、手动创建对应分区目录

hadoop fs -mkdir /user/hive/warehouse/datax.db/fin_transaction_flow/dt=20250416

4、执行DataX

./bin/datax.py ../mysql-hive.json

看最终状态显示成功
在这里插入图片描述
hdfs目标目录里也有了对应文件
在这里插入图片描述

5、添加分区信息
经过上述操作还不能直接从hive表里查询出数据,因为元数据信息尚未构建起来。

ALTER TABLE datax.fin_transaction_flow ADD IF NOT EXISTS PARTITION (dt='20250416');

6、验证数据
在这里插入图片描述

7、问题:发现count数据为0

select count(*) from datax.fin_transaction_flow;

这个是因为hive未及时构建表分析信息导致,手动执行如下表分析sql即可

analyze table datax.fin_transaction_flow compute statistics;
http://www.xdnf.cn/news/6001.html

相关文章:

  • html5+css3实现傅里叶变换的动态展示效果(仅供参考)
  • DeepSeek 赋能 VR/AR:开启智能交互新纪元
  • 密西根大学新作——LightEMMA:自动驾驶中轻量级端到端多模态模型
  • Python面向对象编程精解:从两大编程范式到类与对象实战
  • 16S18S_分析步骤(2)
  • C++.神经网络与深度学习(赶工版)(会二次修改)
  • PostgREST:无需后端 快速构建RESTful API服务
  • ISP有感自发
  • Spring Boot 博客项目深度分析报告
  • Step1
  • 编程题 03-树2 List Leaves【PAT】
  • 单向循环链表C语言实现实现(全)
  • 旋变信号数据转换卡 旋变解码模块 汽车永磁同步电机维修工具
  • 用PyTorch在超大规模下训练深度学习模型:并行策略全解析
  • synchronized关键字详解
  • 国产ETL数据集成软件和Informatica 相比如何
  • 鸿蒙OSUniApp开发支持多语言的国际化组件#三方框架 #Uniapp
  • iOS WebView和WKWebView怎么调试?
  • 计算机网络:移动通信蜂窝网络指的是什么?
  • centos服务器,疑似感染phishing家族钓鱼软件的检查
  • 捕捉Unix信号
  • css 左右布局
  • 业务中台-典型技术栈选型(微服务、容器编排、分布式数据库、消息队列、服务监控、低代码等)
  • 鸿蒙OSUniApp 实现的二维码扫描与生成组件#三方框架 #Uniapp
  • STM32 实时时钟(RTC)详解
  • 【​​HTTPS基础概念与原理​】TLS握手过程详解​​
  • 常见相机焦段的分类及其应用
  • java加强 -stream流
  • 如何开发一款 Chrome 浏览器插件
  • 纯css实现蜂窝效果