一、前言

最近在使用datax同步数据表,由于好多个表,一开始每次都需要等一个执行完再执行下一个,这样明显效率很低,于是写了个Java方法来批量操作;
环境: linux服务器

二、Java代码

1
/**
2
 * @Author: Huang JX
3
 * @Date: 2021/11/11
4
 * @Description: datax 批量执行脚本,日志存放在当前目录的 DataxSyncLogs 中;
5
 * 注意:需要输入 datax 脚本的文件夹路径作为 main 函数的参数,如 home/xxx/datax/job/
6
 * <p>
7
 * 使用说明:
8
 * 1、去掉文件的包名package;
9
 * 2、将此Java文件放到 /datax/bin/目录下;
10
 * 3、javac DataxSyncUtil.java 
11
 * 4、java DataxSyncUtil [参数]datax脚本文件夹路径
12
 */
13
public class DataxSyncUtil {
14
    public static void main(String[] args) throws Exception {
15
        FileOutputStream out = null;
16
        StringBuffer sb = new StringBuffer();
17
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
18
        try {
19
            String jobPath = args[0];
20
            if (jobPath == null && "".equals(jobPath.trim())) {
21
                throw new Exception("请输入datax脚本文件夹路径");
22
            }
23
            String logDir = jobPath + "DataxSyncLogs";
24
            File logDirFile = new File(logDir);
25
            if (!logDirFile.exists()) {
26
                logDirFile.mkdir(); // 创建日志文件夹
27
            }
28
            String logFileName = logDir + "/datax_sync_" + sdf.format(new Date()) + ".log";
29
            File file = new File(logFileName);
30
            if (!file.exists())
31
                file.createNewFile();
32
            out = new FileOutputStream(file, true);
33
            sb.append("==========================================\n");
34
            sb.append("==========Datax Sync Job Start!==========\n");
35
            sb.append("==========================================\n");
36
            File f = new File(jobPath);
37
            if (!f.exists()) {
38
                sb.append(jobPath + " not exists\n");
39
                return;
40
            }
41
            System.out.print(sb.toString());
42
            out.write(sb.toString().getBytes("utf-8"));
43
            String cmdStr;
44
            File fa[] = f.listFiles();
45
            for (int i = 0; i < fa.length; i++) {
46
                sb = new StringBuffer();
47
                File fs = fa[i];
48
                if (!fs.isDirectory()) {
49
                    Process pr = null;
50
                    cmdStr = "python3 datax.py " + jobPath + fs.getName();
51
                    sb.append("start cmd: " + cmdStr + "\n");
52
                    System.out.print("start cmd: " + cmdStr + "\n");
53
                    pr = Runtime.getRuntime().exec(cmdStr);
54
                    BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream()));
55
                    String line;
56
                    while (true) {
57
                        if (!((line = in.readLine()) != null)) break;
58
                        sb.append(line + "\n");
59
                        System.out.print(line + "\n");
60
                    }
61
                    in.close();
62
                    pr.waitFor();
63
                    out.write(sb.toString().getBytes("utf-8"));
64
                }
65
            }
66
            sb.append("==========================================\n");
67
            sb.append("===========Datax Sync Job End!===========\n");
68
            sb.append("==========================================\n");
69
            System.out.print(sb.toString());
70
            out.write(sb.toString().getBytes("utf-8"));
71
        } finally {
72
            out.flush();
73
            out.close();
74
        }
75
    }
76
}

【注意】看一下注释的说明,将job的文件路径做为main函数的参数;