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();
}
}
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);
ps.waitFor();
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) {
}
}
}
}