• DataX数据同步实战案例


     

    目录

    一、背景

    二、框架设计

    三、核心架构

    核心模块介绍:

    DataX调度流程:

    四、目前支持的数据源清单

    五、案例

    1.从mysql同步全量数据到hive无分区表的json文件配置

    2.从mysql同步增量数据到hive无分区表的json文件配置

    3.从mysql同步全量数据到hive分区表的json文件配置

    4.从hive同步全量数据到mysql的json文件配置

    5.从hive同步增量数据到mysql的json文件配置

    6.从Postgre同步全量数据到hive分区表的json文件配置

    7.从Postgre同步全量数据到hive分区表的json文件配置

    8.从mysql同步数据到doris的json文件配置

    六、执行


    一、背景

    DataX 是阿里云DataWorks数据集成的开源版本,在阿里巴巴集团内被广泛使用的离线数据同步工具/平台。DataX 实现了包括 MySQL、Oracle、OceanBase、SqlServer、Postgre、HDFS、Hive、ADS、HBase、TableStore(OTS)、MaxCompute(ODPS)、Hologres、DRDS 等各种异构数据源之间高效的数据同步功能。

    二、框架设计

    DataX本身作为离线数据同步框架,采用Framework + plugin架构构建。将数据源读取和写入抽象成为Reader/Writer插件,纳入到整个同步框架中。

    • Reader:Reader为数据采集模块,负责采集数据源的数据,将数据发送给Framework。
    • Writer: Writer为数据写入模块,负责不断向Framework取数据,并将数据写入到目的端。
    • Framework:Framework用于连接reader和writer,作为两者的数据传输通道,并处理缓冲,流控,并发,数据转换等核心技术问题。

    三、核心架构

     

     

    核心模块介绍:

    1. DataX完成单个数据同步的作业,我们称之为Job,DataX接受到一个Job之后,将启动一个进程来完成整个作业同步过程。DataX Job模块是单个作业的中枢管理节点,承担了数据清理、子任务切分(将单一作业计算转化为多个子Task)、TaskGroup管理等功能。
    2. DataXJob启动后,会根据不同的源端切分策略,将Job切分成多个小的Task(子任务),以便于并发执行。Task便是DataX作业的最小单元,每一个Task都会负责一部分数据的同步工作。
    3. 切分多个Task之后,DataX Job会调用Scheduler模块,根据配置的并发数据量,将拆分成的Task重新组合,组装成TaskGroup(任务组)。每一个TaskGroup负责以一定的并发运行完毕分配好的所有Task,默认单个任务组的并发数量为5。
    4. 每一个Task都由TaskGroup负责启动,Task启动后,会固定启动Reader—>Channel—>Writer的线程来完成任务同步工作。
    5. DataX作业运行起来之后, Job监控并等待多个TaskGroup模块任务完成,等待所有TaskGroup任务完成后Job成功退出。否则,异常退出,进程退出值非0

     

    DataX调度流程:

    举例来说,用户提交了一个DataX作业,并且配置了20个并发,目的是将一个100张分表的mysql数据同步到odps里面。 DataX的调度决策思路是:

    1. DataXJob根据分库分表切分成了100个Task。
    2. 根据20个并发,DataX计算共需要分配4个TaskGroup。
    3. 4个TaskGroup平分切分好的100个Task,每一个TaskGroup负责以5个并发共计运行25个Task。

     

    四、目前支持的数据源清单

    类型数据源Reader(读)Writer(写)
    RDBMS 关系型数据库MySQLMysqlReaderMysqlWriter
    Oracle    OracleReaderOracleWriter
    OceanBase  oceanbasev10readeroceanbasev10writer
    SQLServerSqlServerReaderSqlServerWriter
    PostgreSQLPostgresqlReaderPostgresqlWriter
    DRDSDrdsReaderDRDSWriter
    Kudukuduwriter
    Clickhouseclickwriter
    通用RDBMS(支持所有关系型数据库)RDBMSReaderRDBMSWriter
    阿里云数仓数据存储ODPSODPSReaderODPSWriter
    ADSADSWriter
    OSSOSSReaderOSSWriter
    OCSOCSWriter
    NoSQL数据存储OTSOTSReader\otsstreamreaderOTSWriter
    Hbase0.94Hbase094XReaderHbase094XWriter
    Hbase1.1Hbase11XReaderHbase11XWriter
    Phoenix4.xhbase11xsqlreaderHBase11xsqlwriter
    Phoenix5.xhbase20xsqlreaderHBase20xsqlwriter
    MongoDBMongoDBReaderMongoDBWriter
    HiveHdfsReaderHdfsWriter
    CassandraCassandraReaderCassandraWriter
    无结构化数据存储TxtFileTxtFileReaderTxtFileWriter
    FTPFtpReaderFtpWriter
    HDFSHdfsReaderHdfsWriter
    ElasticsearchElasticSearchWriter
    时间序列数据库OpenTSDBOpenTSDBReader
    TSDBTSDBReaderTSDBWriter
    TDengineTDengineReaderTDengineWriter

     数据源参考指南:GitHub - alibaba/DataX: DataX是阿里云DataWorks数据集成的开源版本。

    五、案例

    1.从mysql同步全量数据到hive无分区表的json文件配置

    1. {
    2. "job": {
    3. "content": [
    4. {
    5. "reader": {
    6. "name": "mysqlreader",
    7. "parameter": {
    8. "connection": [
    9. {
    10. "jdbcUrl": ["jdbc:mysql://ip:port/db_name?useSSL=false"],
    11. "querySql": ["select * from table_name"],
    12. }
    13. ],
    14. "username": "username",
    15. "password": "password"
    16. }
    17. },
    18. "writer": {
    19. "name": "hdfswriter",
    20. "parameter": {
    21. "defaultFS": "hdfs://ip:port",
    22. "fileType": "text",
    23. "path": "/user/hive/warehouse/db_name.db/hive_table_name_da",
    24. "fileName": "hive_table_name",
    25. "column": [
    26. {"name":"id","type":"int"},
    27. {"name":"name","type":"string"}
    28. ],
    29. "writeMode": "append",
    30. "fieldDelimiter": "\t",
    31. "encoding": "utf-8"
    32. }
    33. }
    34. }],
    35. "setting": {
    36. "speed": {
    37. "channel": "1"
    38. },
    39. "errorLimit": {
    40. "record": 0,
    41. "percentage": 0.02
    42. }
    43. }
    44. }
    45. }

    2.从mysql同步增量数据到hive无分区表的json文件配置

    1. {
    2. "job": {
    3. "content": [
    4. {
    5. "reader": {
    6. "name": "mysqlreader",
    7. "parameter": {
    8. "connection": [
    9. {
    10. "jdbcUrl": ["jdbc:mysql://ip:port/db_name?useSSL=false"],
    11. "querySql": ["select * from mysql_table_name where date(date_created)='${date_create}'"],
    12. }
    13. ],
    14. "username": "username",
    15. "password": "password"
    16. }
    17. },
    18. "writer": {
    19. "name": "hdfswriter",
    20. "parameter": {
    21. "defaultFS": "hdfs://ip:port",
    22. "fileType": "text",
    23. "path": "/user/hive/warehouse/db_name.db/hive_table_name_da",
    24. "fileName": "hive_table_name",
    25. "column": [
    26. {"name":"id","type":"int"},
    27. {"name":"name","type":"string"}
    28. ],
    29. "writeMode": "append",
    30. "fieldDelimiter": "\t",
    31. "encoding": "utf-8"
    32. }
    33. }
    34. }],
    35. "setting": {
    36. "speed": {
    37. "channel": "1"
    38. },
    39. "errorLimit": {
    40. "record": 0,
    41. "percentage": 0.02
    42. }
    43. }
    44. }
    45. }

    3.从mysql同步全量数据到hive分区表的json文件配置

    1. {
    2. "job": {
    3. "content": [
    4. {
    5. "reader": {
    6. "name": "mysqlreader",
    7. "parameter": {
    8. "connection": [
    9. {
    10. "jdbcUrl": ["jdbc:mysql://ip:port/db_name?useSSL=false"],
    11. "querySql": ["select * from mysql_table_name"],
    12. }
    13. ],
    14. "username": "username",
    15. "password": "password"
    16. }
    17. },
    18. "writer": {
    19. "name": "hdfswriter",
    20. "parameter": {
    21. "defaultFS": "hdfs://ip:port",
    22. "fileType": "text",
    23. "path": "/user/hive/warehouse/db_name.db/hive_table_name_ds/ds=2022-09-16",
    24. "fileName": "hive_table_name",
    25. "column": [
    26. {"name":"id","type":"int"},
    27. {"name":"name","type":"string"}
    28. ],
    29. "writeMode": "append",
    30. "fieldDelimiter": "\t",
    31. "encoding": "utf-8"
    32. }
    33. }
    34. }],
    35. "setting": {
    36. "speed": {
    37. "channel": "1"
    38. },
    39. "errorLimit": {
    40. "record": 0,
    41. "percentage": 0.02
    42. }
    43. }
    44. }
    45. }

    4.从hive同步全量数据到mysql的json文件配置

    1. {
    2. "job": {
    3. "setting": {
    4. "speed": {
    5. "channel": 1
    6. },
    7. "errorLimit": {
    8. "record": 0,
    9. "percentage": 0.02
    10. }
    11. },
    12. "content": [{
    13. "reader": {
    14. "name": "hdfsreader",
    15. "parameter": {
    16. "path":"/user/hive/warehouse/db_name.db/hive_table_name",
    17. "defaultFS": "hdfs://ip:port",
    18. "column": [
    19. {
    20. "index": 0,
    21. "type": "long"
    22. },
    23. {
    24. "index": 1,
    25. "type": "string"
    26. },
    27. {
    28. "index": 3,
    29. "type": "long"
    30. }
    31. ],
    32. "fileType": "text",
    33. "encoding": "UTF-8",
    34. "fieldDelimiter": "\t"
    35. }
    36. },
    37. "writer": {
    38. "name": "mysqlwriter",
    39. "parameter": {
    40. "writeMode": "insert",
    41. "username": "username",
    42. "password": "password",
    43. "column": [
    44. "id",
    45. "name",
    46. "age"
    47. ],
    48. "session": [
    49. "set session sql_mode='ANSI'"
    50. ],
    51. "preSql": [
    52. "truncate table mysql_table_name"
    53. ],
    54. "connection": [{
    55. "jdbcUrl": "jdbc:mysql://ip:port/db_name?useUnicode=true&characterEncoding=utf8",
    56. "table": [
    57. "mysql_table_name"
    58. ]
    59. }]
    60. }
    61. }
    62. }]
    63. }
    64. }

    5.从hive同步增量数据到mysql的json文件配置

    1. {
    2. "job": {
    3. "setting": {
    4. "speed": {
    5. "channel": 1
    6. },
    7. "errorLimit": {
    8. "record": 0,
    9. "percentage": 0.02
    10. }
    11. },
    12. "content": [{
    13. "reader": {
    14. "name": "hdfsreader",
    15. "parameter": {
    16. "path":"/user/hive/warehouse/db_name.db/hive_table_name/ds=${ds}",
    17. "defaultFS": "hdfs://ip:port",
    18. "column": [
    19. {
    20. "index": 0,
    21. "type": "long"
    22. },
    23. {
    24. "index": 1,
    25. "type": "string"
    26. },
    27. {
    28. "index": 3,
    29. "type": "long"
    30. }
    31. ],
    32. "fileType": "text",
    33. "encoding": "UTF-8",
    34. "fieldDelimiter": "\t"
    35. }
    36. },
    37. "writer": {
    38. "name": "mysqlwriter",
    39. "parameter": {
    40. "writeMode": "insert",
    41. "username": "username",
    42. "password": "password",
    43. "column": [
    44. "id",
    45. "name",
    46. "age"
    47. ],
    48. "session": [
    49. "set session sql_mode='ANSI'"
    50. ],
    51. "preSql": [
    52. "truncate table mysql_table_name"
    53. ],
    54. "connection": [{
    55. "jdbcUrl": "jdbc:mysql://ip:port/db_name?useUnicode=true&characterEncoding=utf8",
    56. "table": [
    57. "mysql_table_name"
    58. ]
    59. }]
    60. }
    61. }
    62. }]
    63. }
    64. }

    6.从Postgre同步全量数据到hive分区表的json文件配置

    1. {
    2. "job": {
    3. "content": [
    4. {
    5. "reader": {
    6. "name": "postgresqlreader",
    7. "parameter": {
    8. "connection": [
    9. {
    10. "jdbcUrl": ["jdbc:postgresql://ip:port/pg_db_name"],
    11. "querySql": ["select * from pg_table_name"],
    12. }
    13. ],
    14. "username": "username",
    15. "password": "password"
    16. }
    17. },
    18. "writer": {
    19. "name": "hdfswriter",
    20. "parameter": {
    21. "defaultFS": "hdfs://ip:port",
    22. "fileType": "text",
    23. "path": "/user/hive/warehouse/hive_db_name.db/hive_table_name/ds=${ds}",
    24. "fileName": "hive_table_name",
    25. "column": [
    26. {"name":"id","type":"bigint"},
    27. {"name":"name","type":"string"},
    28. {"name":"date_create","type":"string"}
    29. ],
    30. "writeMode": "append",
    31. "fieldDelimiter": "\t",
    32. "encoding": "utf-8"
    33. }
    34. }
    35. }],
    36. "setting": {
    37. "speed": {
    38. "channel": "1"
    39. }
    40. }
    41. }
    42. }

    7.从Postgre同步全量数据到hive分区表的json文件配置

    1. {
    2. "job": {
    3. "content": [
    4. {
    5. "reader": {
    6. "name": "postgresqlreader",
    7. "parameter": {
    8. "connection": [
    9. {
    10. "jdbcUrl": ["jdbc:postgresql://ip:[ort/pg_db_name"],
    11. "querySql": ["select * from pg_table_name where date_create='${date_create}'"],
    12. }
    13. ],
    14. "username": "username",
    15. "password": "password"
    16. }
    17. },
    18. "writer": {
    19. "name": "hdfswriter",
    20. "parameter": {
    21. "defaultFS": "hdfs://ip:port",
    22. "fileType": "text",
    23. "path": "/user/hive/warehouse/hive_db_name.db/hive_table_name/ds=${ds}",
    24. "fileName": "hive_table_name",
    25. "column": [
    26. {"name":"id","type":"bigint"},
    27. {"name":"name","type":"string"},
    28. {"name":"date_create","type":"string"}
    29. ],
    30. "writeMode": "append",
    31. "fieldDelimiter": "\t",
    32. "encoding": "utf-8"
    33. }
    34. }
    35. }],
    36. "setting": {
    37. "speed": {
    38. "channel": "1"
    39. },
    40. "errorLimit": {
    41. "record": 0,
    42. "percentage": 0.02
    43. }
    44. }
    45. }
    46. }

    8.从mysql同步数据到doris的json文件配置

    1. {
    2. "core":{
    3. "transport": {
    4. "channel": {
    5. "speed": {
    6. "byte": 104857600,
    7. "record": 200000
    8. }
    9. }
    10. }
    11. },
    12. "job": {
    13. "setting": {
    14. "speed": {
    15. "channel": 1
    16. },
    17. "errorLimit": {
    18. "record": 0,
    19. "percentage": 0
    20. }
    21. },
    22. "content": [
    23. {
    24. "reader": {
    25. "name": "mysqlreader",
    26. "parameter": {
    27. "username": "username",
    28. "password": "password",
    29. "connection": [
    30. {
    31. "jdbcUrl": [
    32. "jdbc:mysql://ip:port/mysql_db_name"
    33. ],
    34. "querySql": [
    35. "select * from mysql_table_name;"
    36. ]
    37. }
    38. ]
    39. }
    40. },
    41. "writer": {
    42. "name": "doriswriter",
    43. "parameter": {
    44. "username": "username",
    45. "password": "password",
    46. "database": "db_name",
    47. "table": "table_name",
    48. "column": [ "column1","column2","column3"],
    49. "preSql": [],
    50. "postSql": [],
    51. "jdbcUrl": "jdbc:mysql://ip:port/",
    52. "feLoadUrl": ["cdh3:port"],
    53. "beLoadUrl": ["cdh1:port", "cdh2:port", "cdh3:port"],
    54. "loadProps": {
    55. },
    56. "maxBatchRows" : 200000,
    57. "maxBatchByteSize" : 104857600,
    58. "lineDelimiter": "\n"
    59. }
    60. }
    61. }
    62. ]
    63. }
    64. }

    六、执行

    执行命令

    $ python datax.py conf.json

  • 相关阅读:
    【springboot】spring的Aop结合Redis实现对短信接口的限流
    【精品】轻松部署ceph分布式存储集群
    计算机类论文辅导(SCI写作和投稿),毕设辅导,竞赛辅导,升学材料润色
    spring boot 集成 RedisSearch 和 RedisJSON
    企业微信接口测试实战(一)
    Spark与Hadoop相比的优缺点
    第四次考核 Jimmy 学徒考核 Linux安装软件 rnaseq上游分析
    window系统 bat脚本开启和关闭防火墙
    帆软报表-SQL片段报错处理
    【老生谈算法】matlab实现低阶函数的二维图像获取——二维图像获取
  • 原文地址:https://blog.csdn.net/chimchim66/article/details/126895332