1 简介

DataX 是阿里巴巴集团内被广泛使用的离线数据同步工具/平台,实现包括 MySQL、SQL Server、Oracle、PostgreSQL、HDFS、Hive、HBase、OTS、ODPS 等各种异构数据源之间高效的数据同步功能。

2 安装及基本使用

https://github.com/alibaba/DataX/blob/master/userGuid.md

如果是python3需更换datax.py文件
支持mysql8以上版本 https://www.cnblogs.com/zifan/p/12550747.html

3.处理版Datax
链接:https://pan.baidu.com/s/1_FpIw76lfb7l0nDLf941zQ 提取码:7ki9

3 Json脚本配置说明
  1. {
  2. "core": {
  3. "transport": {
  4. "channel": {
  5. "speed": {
  6. ## 此处为数据导入的并发度,建议根据服务器硬件进行调优
  7. "channel": 10,
  8. ##此处解除对读取行数的限制
  9. "record": -1,
  10. ##此处解除对字节的限制
  11. "byte": -1,
  12. ##每次读取batch的大小
  13. "batchSize": 204800
  14. }
  15. }
  16. }
  17. },
  18. "job": {
  19. "setting": {
  20. "speed": {
  21. "channel": 10,
  22. "record": -1,
  23. "byte": -1,
  24. "batchSize": 204800
  25. },
  26. "errorLimit": {
  27. "percentage": 0
  28. }
  29. },
  30. "content": [{
  31. "reader": {
  32. "name": "sqlserverreader",
  33. "parameter": {
  34. "username": "sa",
  35. "password": "sasa",
  36. "where": "",
  37. "connection": [{
  38. //querySql 在读之前支持链表读取源数据
  39. "querySql": [
  40. "SELECT a.Id,a.Code,getdate() as Catetime FROM basics.Location a join basics.LocationType b on a.LocationTypeCode = b.Code;"
  41. ],
  42. "jdbcUrl": ["jdbc:sqlserver://127.0.0.1:1433;DatabaseName=Src.Basics.Pro"]
  43. }],
  44. "maxRetries": 3
  45. }
  46. },
  47. "writer": {
  48. "name": "mysqlwriter",
  49. "parameter": {
  50. "username": "root",
  51. "password": "123456",
  52. "dateFormat": "YYYY-MM-dd hh:mm:ss",
  53. "column": [
  54. "id","code","Catetime"
  55. ],
  56. //preSql 支持多个写入前的动作
  57. "preSql": [
  58. "truncate table location_back",
  59. "insert into location_back select * from location",
  60. "truncate table location;"
  61. ],
  62. "connection": [{
  63. //useCompression=true 压缩链接串,减少迁移时间
  64. "jdbcUrl": "jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&useCompression=true",
  65. "table": [
  66. "location"
  67. ]
  68. }]
  69. }
  70. }
  71. }]
  72. }
  73. }
  1. Datax调优和动态传参

https://www.cnblogs.com/hit-zb/p/10940849.html#autoid-0-0-0

  1. Datax结合Python

    1. str = "CHCP 65001"+"&&"
    2. str += "C:"+"&&"
    3. str += "cd C:\\Users\\mechrev\\Desktop\\迁移文件准备\\" + "&&"
    4. str += "python newdatax\\bin\\datax.py "
    5. str += " --jvm=\"-Xms3G -Xmx3G\" " + filename
    6. //主要利用内置函数os.system 执行cmd命令行语句,其他需求在此基础上可任意扩展
    7. cmd = os.system(str)
    8. print(cmd)
  2. Datax结合Net

    1. //利用Process 执行cmd命令行语句,其他需求在此基础上可任意扩展,
    2. //这种方式是阻塞式的,不能够实时观察迁移情况
    3. string strInput = "ipconfig";
    4. Process p = new Process();
    5. //设置要启动的应用程序
    6. p.StartInfo.FileName = "cmd.exe";
    7. //是否使用操作系统shell启动
    8. p.StartInfo.UseShellExecute = false;
    9. // 接受来自调用程序的输入信息
    10. p.StartInfo.RedirectStandardInput = true;
    11. //输出信息
    12. p.StartInfo.RedirectStandardOutput = true;
    13. // 输出错误
    14. p.StartInfo.RedirectStandardError = true;
    15. //不显示程序窗口
    16. p.StartInfo.CreateNoWindow = true;
    17. //设置乱码
    18. p.StartInfo.StandardOutputEncoding = Encoding.UTF8;
    19. //启动程序
    20. p.Start();
    21. //向cmd窗口发送输入信息
    22. p.StandardInput.WriteLine(strInput + "&exit");
    23. p.StandardInput.AutoFlush = true;
    24. //获取输出信息
    25. string strOuput = p.StandardOutput.ReadToEnd();
    26. //等待程序执行完退出进程
    27. p.WaitForExit();
    28. p.Close();
  3. 扩展