git clone https://github.com/alibaba/DataX.git/opt/dataxJAVA_HOME: export JAVA_HOME=/usr/local/java export PATH=$JAVA_HOME/bin:$PATHplugin/reader 和 plugin/writer 查看。DataX 的工作原理可以分为以下步骤:
python ${DATAX_HOME}/bin/datax.py job.json 启动任务DataX 架构图示意:
+------------------+ | Job | +------------------+ | +---------+---------+ | | +-----v-----+ +-----v-----+ | Reader | | Writer | +-----+-----+ +-----+-----+ | | +-----v-------------------v-----+ | Channel | +--------------------------------+php420 Bytes© 菜鸟-创作你的创作{ "job": { "setting": { "speed": { "channel": 3 } }, "content": [ { "reader": { "name": "mysqlreader", "parameter": { "username": "root", "password": "123456", "column": ["id","name","age"], "splitPk": "id", "connection": [ { "table": ["source_table"], "jdbcUrl": ["jdbc:mysql://127.0.0.1:3306/source_db"] } ] } }, "writer": { "name": "mysqlwriter", "parameter": { "username": "root", "password": "123456", "column": ["id","name","age"], "connection": [ { "table": ["target_table"], "jdbcUrl": "jdbc:mysql://127.0.0.1:3306/target_db" } ] } } } ] }}php945 Bytes© 菜鸟-创作你的创作python ${DATAX_HOME}/bin/datax.py job_mysql_to_mysql.jsonphp57 Bytes© 菜鸟-创作你的创作log 目录where 条件,只读取增量数据replace、insert、update 等模式speed.channel 设置通道数,提高吞吐量datax.py job.json)明白阿杰,我帮你整理一个 DataX MySQL → Hive 的完整实战示例,包含 增量同步、分片设置、日志分析,让你可以直接上手。
mysqlreaderhdfswriter(写入 Hive 可用 HDFS,再建外部表)user_info(MySQL)id, name, age, update_timeuser_info_hive(Hive)/user/hive/warehouse/user_info_hive/update_time > '${last_sync_time}'${last_sync_time}{ "job": { "setting": { "speed": { "channel": 4 // 并行 4 个线程 }, "errorLimit": { "record": 0, "percentage": 0.02 } }, "content": [ { "reader": { "name": "mysqlreader", "parameter": { "username": "root", "password": "123456", "column": ["id","name","age","update_time"], "splitPk": "id", "connection": [ { "table": ["user_info"], "jdbcUrl": ["jdbc:mysql://127.0.0.1:3306/source_db"], "where": "update_time > '${last_sync_time}'" } ] } }, "writer": { "name": "hdfswriter", "parameter": { "defaultFS": "hdfs://namenode:8020", "fileType": "text", "path": "/user/hive/warehouse/user_info_hive/", "fileName": "user_info", "writeMode": "append", "fieldDelimiter": "\t", "column": [ {"name":"id","type":"string"}, {"name":"name","type":"string"}, {"name":"age","type":"string"}, {"name":"update_time","type":"string"} ] } } } ] }}php1.26 KB© 菜鸟-创作你的创作CREATE EXTERNAL TABLE user_info_hive( id STRING, name STRING, age STRING, update_time STRING)ROW FORMAT DELIMITEDFIELDS TERMINATED BY '\t'STORED AS TEXTFILELOCATION '/user/hive/warehouse/user_info_hive/';php225 Bytes© 菜鸟-创作你的创作python ${DATAX_HOME}/bin/datax.py mysql_to_hive_job.jsonphp56 Bytes© 菜鸟-创作你的创作Channel-1: read 1000 records, write 1000 records Channel-2: read 950 records, write 950 records ... Total: 3950 records${DATAX_HOME}/log 下,可查找失败记录或错误信息。${last_sync_time}:where 条件中# 获取上次同步时间LAST_TIME=$(cat /opt/datax/last_sync_time.txt)# 替换 JSON 模板变量sed "s/\${last_sync_time}/$LAST_TIME/g" mysql_to_hive_job_template.json > mysql_to_hive_job.json# 执行 DataXpython ${DATAX_HOME}/bin/datax.py mysql_to_hive_job.json# 同步完成后更新时间date "+%Y-%m-%d %H:%M:%S" > /opt/datax/last_sync_time.txtphp310 Bytes© 菜鸟-创作你的创作${DATAX_HOME}/logTotal Records:总同步记录数Channel-X Records:每个线程读写记录数Error Record:错误记录数errorLimit.record:失败记录数超过该值任务失败errorLimit.percentage:失败占比超过该值任务失败https://www.52runoob.com/archives/5750
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。