Java调用执行HDFS上的shell脚本

package com.cloudera;


import org.apache.commons.lang3.ArrayUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
import java.io.InputStreamReader;
import java.net.URI;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;


public class ExecSqoopShell {

    public static void main(String[] args) {

        final File file = new File(args[1]);
        try {
            Configuration conf = new Configuration();
            FileSystem fs = FileSystem.get(new URI("hdfs:/10.101.71.41:8020"), conf, args[2]);
            Path srcPath = new Path(args[0]);

            //判断本地文件是否存在,存在则删除
            if(file.exists()){
                file.delete();
            }
            fs.copyToLocalFile(false, srcPath, new Path(args[1]), true);
            fs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        String result = execShell(args[1], args[3], "abc");

        System.out.println(result);

        //判断本地文件是否存在,存在则删除
        if(file.exists()){
            file.delete();
        }
    }


    /**
     * 解决了 参数中包含 空格和脚本没有执行权限的问题
     * @param scriptPath 脚本路径
     * @param para 参数数组
     */
    private static String execShell(String scriptPath, String ... para) {

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
        String date = LocalDateTime.now(ZoneOffset.of("+8")).format(formatter);

        System.out.println("================================执行时间:"+ date +"======================================");

        StringBuilder result = new StringBuilder();

        Process process = null;
        Process ps = null;
        BufferedReader bufrIn = null;
        BufferedReader bufrError = null;

        try {
            System.out.println(scriptPath);
            String[] cmd = new String[]{scriptPath};
            //为了解决参数中包含空格
            cmd = ArrayUtils.addAll(cmd,para);

           //解决脚本没有执行权限
            ProcessBuilder builder = new ProcessBuilder();
            builder.command("/bin/chmod", "755",scriptPath);
            process = builder.start();
            process.waitFor();

            // 执行命令, 返回一个子进程对象(命令在子进程中执行)
            ps = Runtime.getRuntime().exec(cmd);
            // 方法阻塞, 等待命令执行完成(成功会返回0)
            ps.waitFor();

            // 获取命令执行结果, 有两个结果: 正常的输出 和 错误的输出(PS: 子进程的输出就是主进程的输入)
            bufrIn = new BufferedReader(new InputStreamReader(ps.getInputStream(), "UTF-8"));
            bufrError = new BufferedReader(new InputStreamReader(ps.getErrorStream(), "UTF-8"));
            // 读取输出
            String line = null;
            while ((line = bufrIn.readLine()) != null) {
                result.append(line).append('\n');
            }
            while ((line = bufrError.readLine()) != null) {
                result.append(line).append('\n');
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            closeStream(bufrIn);
            closeStream(bufrError);

            // 销毁子进程
            if (process != null) {
                process.destroy();
            }
            if (ps != null) {
                ps.destroy();
            }
        }
        // 返回执行结果
        return result.toString();
    }


    private static void closeStream(Closeable stream) {
        if (stream != null) {
            try {
                stream.close();
            } catch (Exception e) {
                // nothing
            }
        }
    }

}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值