Java执行Datax脚本-MySQL写入Doris

该代码段展示了如何使用Java调用DataX生成JSON配置模板,然后进行数据源的读写参数组装,最后执行DataX作业进行数据迁移。主要涉及MySQL到Doris的数据迁移,包括设置用户、密码、数据库连接和SQL查询等信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、生成DataX json模板

    public static void josnTemplate(String source,String target,String datasetId) {
        String classPath = "D:/software/datax";
        String jobFilePath = "D:/software/datax/job/";
        System.setProperty("datax.home", classPath);

        String[] arguments = new String[]{"python", classPath+"/bin/datax.py", "-r", source.toLowerCase()+"reader", "-w", target.toLowerCase()+"writer"};
        try {
            Process process = Runtime.getRuntime().exec(arguments);
            BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"));
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(jobFilePath + "dataset_"+datasetId), "UTF-8"));

            String line = null;
            int count = 0;
            while ((line = br.readLine()) != null) {
                count++;
                if (count > 15) {
                    //System.out.println(line);

                    bw.write(line);
                    bw.newLine();
                    bw.flush();
                }
            }
            bw.close();
            br.close();
            //java代码中的process.waitFor()返回值为0表示我们调用python脚本成功,
            //返回值为1表示调用python脚本失败,这和我们通常意义上见到的0与1定义正好相反
            int re = process.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

2、组装DataX json模板

    public static String getJson(String filePath) {
        String json = "";
        try {
            File file = new File(filePath);
            FileReader reader = new FileReader(file);
            FileInputStream inputStream = new FileInputStream(file);
            Reader stream = new InputStreamReader(inputStream, Charset.defaultCharset());

            int ch = 0;
            StringBuilder sb = new StringBuilder();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            reader.close();
            stream.close();

            JSONObject jobj = JSONObject.parseObject(sb.toString());
            json = jobj.toJSONString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }
    public static String getDataXJson(String filePath) throws FileNotFoundException {
        String json = getJson(filePath);
        JSONObject jobj = JSONObject.parseObject(json);
        //组装reader
        JSONObject reader = jobj.getJSONObject("job").getJSONArray("content").getJSONObject(0).getJSONObject("reader");
        JSONObject rp = reader.getJSONObject("parameter");
        rp.put("username", "root");
        rp.put("password", "123456");
        rp.remove("column");//删除column元素

        JSONObject rc = rp.getJSONArray("connection").getJSONObject(0);
        JSONArray jdbcUrl = new JSONArray();
        jdbcUrl.add("jdbc:mysql://IP:3306/dataset?useUnicode=true&characterEncoding=utf-8&useSSL=false&autoReconnect=true&allowMultiQueries=true&serverTimezone=Asia/Shanghai");
        rc.put("jdbcUrl", jdbcUrl);
        rc.remove("table");//删除table元素
        JSONArray querySql = new JSONArray();
        querySql.add("select uuid() as fusion_uuid,a,b,c,d,e,f,h from t_number");
        rc.put("querySql", querySql);
        //组装writer
        JSONObject writer = jobj.getJSONObject("job").getJSONArray("content").getJSONObject(0).getJSONObject("writer");
        JSONObject wp = writer.getJSONObject("parameter");
        wp.put("username", "root");
        wp.put("password", "123456");
        JSONArray column = new JSONArray();
        column.add("*");
        wp.put("column", column);
        JSONArray preSql = new JSONArray();
        preSql.add("truncate table dataset_1619888763184439297");
        wp.put("preSql", preSql);
        JSONArray loadUrl = new JSONArray();
        loadUrl.add("IP:8030");
        wp.put("loadUrl", loadUrl);

        JSONObject wc = wp.getJSONArray("connection").getJSONObject(0);
        wc.put("jdbcUrl", "jdbc:mysql://IP:9030/fusion");
        JSONArray table = new JSONArray();
        table.add("dataset_1619888763184439297");
        wc.put("table", table);
        wc.put("selectedDatabase", "fusion");
        //组装setting
        JSONObject setting = jobj.getJSONObject("job").getJSONObject("setting");
        JSONObject speed = setting.getJSONObject("speed");
        speed.put("channel", "1");
		//组装errorLimit
        JSONObject errorlimit = new JSONObject();
        errorlimit.put("percentage",0.02);
        errorlimit.put("record",0);
        setting.put("errorLimit",errorlimit);
        //写入文本
        PrintStream stream= new PrintStream(filePath,"UTF-8");
        //stream.println(jobj);
        
        return jobj.toString();
    }

3、执行DataX job

    public static void execute(String filePath) throws Exception {
        String dataxHome = "D:/software/datax";
        System.setProperty("datax.home", dataxHome);

        String dataXJson = getDataXJson(filePath);
        //PrintStream stream= new PrintStream(filePath);
        //stream.println(dataXJson);

        //String[] datxArgs = {"-job", getClasspath() + "/datax/stream.json", "-mode", "standalone", "-jobid", "-1"};
        String[] dataXArgs = {"-job", filePath, "-mode", "standalone", "-jobid", "-1"};

        try {
            Engine.entry(dataXArgs);
        }catch (DataXException e){
            e.printStackTrace();
        }
        catch (Throwable throwable) {
            /**
             * 拿到异常信息入库.
             */
            log.error("\n\n经DataX智能分析,该任务最可能的错误原因::\n" + ExceptionTracker.trace(throwable));
        }

    }
    public static void main(String[] args) throws Exception {
        josnTemplate("mysql","doris","1619879625037537281");
        getDataXJson("D:/software/datax/job/dataset_1619879625037537281");
        execute("D:/software/datax/job/dataset_1619879625037537281");

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值