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
}
}
}
}
Java调用执行HDFS上的shell脚本
最新推荐文章于 2024-05-11 17:09:39 发布