java执行脚本

import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date; /**
* @Auther:liDeHui
* @Date: 2019/7/11 14:23
* @Description:TODO
* String start_time,String end_time,String current_lac,String result,String call_type,String date,String id
* @Version:1.0
*/
public class TestShell2 {
private static final String basePath = "/data"; // 记录Shell执行状况的日志文件的位置(绝对路径)
private static final String executeShellLogFile = basePath
+ "executeShell.log"; // 发送文件到Kondor系统的Shell的文件名(绝对路径)
private static final String sendKondorShellName = basePath
+ "a.sh"; public int executeShell(String shellCommand)
throws IOException {
System.out.println("shellCommand:"+shellCommand);
int success = 0;
StringBuffer stringBuffer = new StringBuffer();
BufferedReader bufferedReader = null;
// 格式化日期时间,记录日志时使用
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS "); try {
stringBuffer.append(dateFormat.format(new Date()))
.append("准备执行Shell命令 ").append(shellCommand)
.append(" \r\n");
Process pid = null;
//String[] cmd = { "/bin/sh", "-c", shellCommand };
//给shell传递参数
String[] cmd = { "/bin/sh", "-c", shellCommand+" 20190628000006"+" 20190628010558"+" 22397"+
" 0"+" 0"+" 2019-06-28"+" 3" };
// 执行Shell命令
pid = Runtime.getRuntime().exec(cmd);
if (pid != null) {
stringBuffer.append("进程号:").append(pid.toString())
.append("\r\n");
// bufferedReader用于读取Shell的输出内容
bufferedReader = new BufferedReader(new InputStreamReader(pid.getInputStream()), 1024);
pid.waitFor();
} else {
stringBuffer.append("没有pid\r\n");
}
stringBuffer.append(dateFormat.format(new Date())).append(
"Shell命令执行完毕\r\n执行结果为:\r\n");
String line = null;
// 读取Shell的输出内容,并添加到stringBuffer中
while (bufferedReader != null
&& (line = bufferedReader.readLine()) != null) {
stringBuffer.append(line).append("\r\n");
}
System.out.println("stringBuffer:"+stringBuffer);
} catch (Exception ioe) {
stringBuffer.append("执行Shell命令时发生异常:\r\n").append(ioe.getMessage())
.append("\r\n");
} finally {
if (bufferedReader != null) {
OutputStreamWriter outputStreamWriter = null;
try {
bufferedReader.close();
// 将Shell的执行情况输出到日志文件中
OutputStream outputStream = new FileOutputStream(executeShellLogFile);
outputStreamWriter = new OutputStreamWriter(outputStream, "UTF-8");
outputStreamWriter.write(stringBuffer.toString());
System.out.println("stringBuffer.toString():"+stringBuffer.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
outputStreamWriter.close();
}
}
success = 1;
}
return success;
} public static void main(String[] args) {
try {
new TestShell2().executeShell(sendKondorShellName);
} catch (IOException e) {
e.printStackTrace();
}
} }

java执行hive相关命令

import java.io.*;
import java.util.ArrayList;
import java.util.List; /**
* @Auther:liDeHui
* @Date: 2019/7/11 14:23
* @Description:TODO String start_time,String end_time,String current_lac,String
* result,String call_type,String date,String id @Version:1.0
*
*/
public class TestShell3 {
public static void executeShell(String tableName,String sliceTime,String hdfsPath) throws IOException, InterruptedException { List<String> command1 = new ArrayList<String>();
List<String> command2 = new ArrayList<String>(); command1.add("beeline");
command1.add("-e");
command1.add("alter table default."+tableName+" add partition(slicetime="+sliceTime+") location '"+hdfsPath+"'"); command2.add("beeline");
command2.add("-e");
command2.add("alter table default."+tableName+" drop partition(slicetime="+sliceTime+")");
// command1.add("select * from wxwzzx_hive_db.DIM_CFG_LTE_FLAG where type_n='newest' limit 10"); ProcessBuilder hiveProcessBuilder = new ProcessBuilder(command2);
Process hiveProcess = hiveProcessBuilder.start();
int status = hiveProcess.waitFor();
System.out.println("drop paritions status:" + status); ProcessBuilder hiveProcessBuilder1 = new ProcessBuilder(command1);
Process hiveProcess1 = hiveProcessBuilder1.start();
int status1 = hiveProcess1.waitFor();
System.out.println("add paritions status:" + status1); } /*
* alter table O_NRM_ZJ add partition(slicetime=20190808000000) location 'hdfs://ns2/jc_wxwzzx/o_data_dir/kaitong/NRM_ZJ/NRM_ZJ/20190808000000';
*/ public static void main(String[] args) throws Exception {
String tableName = "O_AAA_ZJ";
String sliceTime = "20190808000000";
String hdfsPath = "hdfs://NRM_ZJ/NRM_ZJ/20190808000000";
executeShell(tableName,sliceTime,hdfsPath); } }

java执行hive命令或者脚本的更多相关文章

  1. JAVA执行远端服务器的脚本

    JAVA执行远端服务器的脚本 问题描述 实现思路 技术要点 代码实现 问题描述 工作中遇到这样一个问题,我们的应用为了实现高可用会采取双机部署,拓扑图大致如下: 这种方案可以简单的保证高可用,即便应用 ...

  2. java执行cmd命令并获取输出结果

    1.java执行cmd命令并获取输出结果 import java.io.BufferedReader; import java.io.InputStreamReader; import org.apa ...

  3. Java执行cmd命令、bat脚本、linux命令,shell脚本等

    1.Windows下执行cmd命令 如复制 D:\tmp\my.txt 到D:\tmp\my_by_only_cmd.txt 现文件如图示: 执行代码: private static void run ...

  4. Hadoop概念学习系列之Java调用Shell命令和脚本,致力于hadoop/spark集群(三十六)

    前言 说明的是,本博文,是在以下的博文基础上,立足于它们,致力于我的大数据领域! http://kongcodecenter.iteye.com/blog/1231177 http://blog.cs ...

  5. java执行Shell命令

    java程序中要执行linux命令主要依赖2个类:Process和Runtime首先看一下Process类:ProcessBuilder.start() 和 Runtime.exec 方法创建一个本机 ...

  6. 如何使用Java执行cmd命令

    用JAVA代码实现执行CMD命令的方法! Runtime rt = Runtime.getRuntime(); Process p = rt.exec(String[] cmdarray);     ...

  7. Java 执行linux命令(转)

    转自 http://blog.csdn.net/a19881029/article/details/8063758 java程序中要执行linux命令主要依赖2个类:Process和Runtime 首 ...

  8. java基础/java调用shell命令和脚本

    一.项目需求: 从某一机构获取证书,证书机构提供小工具,执行.sh脚本即可启动服务,本地调用该服务即可获取证书. 问题:linux服务器启动该服务,不能关闭.一旦关闭,服务即停止. 解决方案:java ...

  9. Linux远程执行Shell命令或脚本

    ## 远程执行shell命令 ssh [user]@[server] '[command]' # eg. ssh root@192.168.1.1 'uptime' ## 远程执行本地shell脚本 ...

随机推荐

  1. Django 模版语言

    传入变量 {{ 变量名 }} 在 view.py 的函数返回 render 时在html文件名后传入一个字典,字典的 key 对应html文件中的变量名,value 为传入的值 views.py: d ...

  2. django 报错Reverse for 'detail' with keyword arguments '{'pk': '2'}' not found. 1 pattern(s) tried: ['$post/(?P<pk>[0-9]+)/$']

    Django报错:Reverse for 'detail' with keyword arguments '{'pk': '2'}' not found. 1 pattern(s) tried: [' ...

  3. 10.12 csp-s模拟测试70 木板+打扫卫生+骆驼

    T1 木板 求$[\sqrt{n},n)$间有多少个数的平方是n的倍数 通过打表可以发现(我没带脑子我看不出来),符合条件的数构成一个等差数列,公差为首项 而首项就是将n质因数分解后每个质因数出现次数 ...

  4. [探究] 用舞蹈链(DLX)解决一类数独问题

    考虑精准覆盖问题的本质--我们把行看做决策,把列看做任务,那么其实质就是通过决策来完成任务. 那么我们来考虑数独问题的本质,对于一个\(n^2\cdot n^2\)的数独而言,他的目标函数有四个: 1 ...

  5. Java 常用正则表达式搜集ing

    MAC地址: ^[a-fA-F0-9]{2}+:[a-fA-F0-9]{2}+:[a-fA-F0-9]{2}+:[a-fA-F0-9]{2}+:[a-fA-F0-9]{2}+:[a-fA-F0-9]{ ...

  6. ubuntu18.04下安装无线网卡驱动心得

    联想Lenovo的笔记本,装完系统wifi显示找不到适配器. lspci | grep Wireless 显示无线网卡类型为博通的BCM43162. 网上一查,果然有问题. apt install f ...

  7. 物联网架构成长之路(32)-SpringBoot集成MQTT客户端

    一.前言 这里虽然是说MQTT客户端.其实对于服务器来说,这里的一个具有超级权限的MQTT客户端,就可以做很多事情.比如手机APP或者网页或者第三方服务需要发送数据到设备,但是这些又不是设备,又不能让 ...

  8. 【Oracle】rman基于时间点恢复

    rman基于时间点恢复 场景: 由于某研究的误操作,导致财务模块的数据丢失,如何使用rman基于时间点恢复数据. 思路 1.克隆数据库的虚拟机,直接对数据库的数据进行恢复 RMAN> shutd ...

  9. nginx代理tcp请求

    1.概述 ngx_stream_core_module 这个module在nginx1.90后开始支持.开启nginx的tcp代理支持--with-stream=dynamic --with-stre ...

  10. 【题解】ADAGRAFT - Ada and Graft [SP33331]

    [题解]ADAGRAFT - Ada and Graft [SP33331] 传送门:\(\text{Ada and Graft}\) \(\text{[SP33331]}\) [题目描述] 给出一颗 ...