Java代码调用Shell脚本并传入参数实现DB2数据库表导出到文件
本文通过Java代码调用Shell脚本并传入参数实现DB2数据库表导出到文件,代码如下:
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.HashMap; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import com.*.dmp.bean.AgentConfigInfo;
import com.*.dmp.bean.MapKeys;
import com.*.dmp.bean.RunStatus;
import com.*.dmp.common.SpringUtils; public class ExportDataServiceDB2 { AgentConfigInfo agentConfigInfo = SpringUtils.getContext().getBean(AgentConfigInfo.class);
private Logger LOG = LoggerFactory.getLogger(ExportDataServiceDB2.class);
private StringBuffer resultMsg = new StringBuffer();
String isOK = "0";
private String exportShell = agentConfigInfo.getEXPORT_SHELL();
// private String exportCMD = agentConfigInfo.getEXPORT_CMD();
private StringBuffer exportFilePath = agentConfigInfo.getEXPORT_FILE_PATH(); /**
* @Title: ExportData
* @Description: 调用Shell脚本实现db2数据的导出
* @param dataMap
* @throws IOException 对方法的参数进行描述
* @return HashMap<String,String> 返回类型
*/
public HashMap<String, String> ExportData(HashMap<String, String> dataMap) throws IOException { String dbSchema = dataMap.get("db_schema");
String dbUser = dataMap.get("db_user");
String dbPassword = dataMap.get("db_password");
String tableName = dataMap.get("table_name");
String interFile = dataMap.get("inter_file");
String delimiter = dataMap.get("delimiter");
String exportLimit = dataMap.get("export_limit"); String filePath = mkDirectory(exportFilePath, interFile);
dataMap.put("file_abs_path", filePath); String cmdPara = createExportShellParams(dbSchema, dbUser,
dbPassword, tableName, filePath, delimiter, exportLimit); LOG.info("Export Parameters: " + cmdPara);
resultMsg.append("Export Parameters: " + cmdPara + "\n"); String cmd = exportShell + " " + cmdPara; Process ps = null;
InputStreamReader isr = null;
LineNumberReader input = null;
String line = null; try {
LOG.info("Run Command: " + cmd );
resultMsg.append("Run Command: " + cmd + "\n"); ps = Runtime.getRuntime().exec(cmd);
isr = new InputStreamReader(ps.getInputStream()); // 使用Reader进行输入读取和打印
input = new LineNumberReader(isr); while (null != (line = input.readLine())) {
LOG.info(line);
resultMsg.append(line);
if (line.contains("failed") || line.contains("Failed") || line.contains("FAILED") || line.contains("错误")) {
isOK = RunStatus.EXPORT_FAIL;
dataMap.put("export_status", isOK);
dataMap.put("proc_log", resultMsg.toString());
// dataMap = packageResult(isOK, resultMsg.toString()); // 组装返回的消息
return dataMap;
} else {
isOK = RunStatus.PROC_RUN_SUCCESS;
}
} // if (0 != ps.waitFor()) {
// isOK = RunStatus.EXPORT_FAIL;
// } else {
// isOK = RunStatus.PROC_RUN_SUCCESS;
// } } catch (IOException e) {
LOG.error("Run the Command Exception: " + cmd + ": " + e.getMessage());
resultMsg.append("Run the Command Exception: " + cmd + ": " + e.getMessage() + "\n");
isOK = RunStatus.EXPORT_FAIL;
} finally {
if (null != input) {
input.close();
} if (null != isr) {
isr.close();
} if (null != ps) {
ps.destroy();
ps = null;
}
} dataMap.put("export_status", isOK);
dataMap.put("proc_log", resultMsg.toString());
// dataMap = packageResult(isOK, resultMsg.toString()); // 组装返回的消息 return dataMap; } /**
* @Title: createExportShellParams
* @Description: 组装参数
* @param msgId
* @param dbSchema
* @param dbUser
* @param dbPassword
* @param tableName
* @param filePath
* @param delimiter
* @param exportLimit
* @return String 返回类型
* @throws
*/
private String createExportShellParams(String dbSchema,
String dbUser, String dbPassword, String tableName,
String filePath, String delimiter, String exportLimit) { StringBuilder params = new StringBuilder();
params.append(dbSchema + " ").append(dbUser + " ").append(dbPassword + " ")
.append(tableName + " ").append(filePath + " ").append(delimiter + " ").append(exportLimit); return params.toString();
} /**
* @Title: mkDirectory
* @Description: 根据配置的路径和文件名,判断文件路径是否存在,若不存在,则先创建,拼接导出文件绝对路径。
* @param filePath
* @param interFile
* @return 对方法的参数进行描述
* @return String 返回类型
* @throws
*/
private String mkDirectory(StringBuffer filePath, String interFile) { File file = new File(filePath.toString()); if ( file.isDirectory() ) {
if (filePath.toString().endsWith("/")) {
filePath.append(interFile);
} else {
filePath.append("/").append(interFile);
}
} else {
LOG.info("The file path is not exists, need to be created now. ");
file.mkdir();
if (filePath.toString().endsWith("/")) {
filePath.append(interFile);
} else {
filePath.append("/").append(interFile);
}
}
return filePath.toString();
} /** 返回消息组装结果 */
private HashMap<String, String> packageResult(String isOK, String resultMsg) {
HashMap<String, String> hsmap = new HashMap<String, String>();
hsmap.put(MapKeys.PROC_STATUS, isOK);
hsmap.put(MapKeys.PROC_LOG, resultMsg);
return hsmap;
} }
传入的执行参数放入一个Map(HashMap<String, String> dataMap)中:
/** EXPORT TEST */
map.put("db_schema", "md");
map.put("db_user", "root");
map.put("db_password", "root");
map.put("table_name", "inter_log");
map.put("inter_file", "inter_log_20140915.avl");
map.put("delimiter", "|");
map.put("export_limit", "");
代码执行之后,将执行日志以及执行结果也存入该Map中一起返回:
dataMap.put("export_status", isOK);
dataMap.put("proc_log", resultMsg.toString());
return dataMap;
执行结果界面:

Java代码调用Shell脚本并传入参数实现DB2数据库表导出到文件的更多相关文章
- Shell脚本实现DB2数据库表导出到文件
该Shell脚本用于实现将DB2数据库表导出到文件,将在另一篇博文<Java代码调用Shell脚本并传入参数实现DB2数据库表导出到文件>中通过Java代码实现调用该脚本并传入参数. #! ...
- [Python]在python中调用shell脚本,并传入参数-02python操作shell实例
首先创建2个shell脚本文件,测试用. test_shell_no_para.sh 运行时,不需要传递参数 test_shell_2_para.sh 运行时,需要传递2个参数 test_shell ...
- 用java代码调用shell脚本执行sqoop将hive表中数据导出到mysql
1:创建shell脚本 touch sqoop_options.sh chmod 777 sqoop_options.sh 编辑文件 特地将执行map的个数设置为变量 测试 可以java代码传参数 ...
- Java如何调用shell脚本的
有些时候会碰到这样的场景:java的功能里面要嵌入一个功能点,这个功能是通过是shell脚本实现的.这种时候就需要Java对脚本调用的支持了. 测试环境 Ubuntu16.04 i3-6100,12G ...
- 利用jmeter发起java请求调用shell脚本
1.创建maven项目 在pom文件中加入依赖: 2.在路径src/main/java下创建类,如类名shellclass 3. 创建jmet ...
- 向shell脚本中传入参数
写一个 程序名为 test.sh 可带参数为 start 和 stop 执行 test.sh start执行 start 内容的代码 执行 test.sh stop 执行 stop 内 ...
- shell脚本获取传入参数的个数
ts.sh #!/bin/bash echo $# 输出 [root@redhat6 ~]# ./ts.sh para1 [root@redhat6 ~]# ./ts.sh para1 para2 [ ...
- Java 调用 shell 脚本详解
这一年的项目中,有大量的场景需要Java 进程调用 Linux的bash shell 脚本实现相关功能. 从之前的项目中拷贝的相关模块和网上的例子来看,有个别的“陷阱”造成调用shell 脚本在某些特 ...
- python调用shell脚本时需要切换目录
最近遇到了一个问题,就是python代码调用shell脚本时,发现输入输出的文件,总是和自己预想的有偏差,但是单独在linux下执行命令的时候,却没有错误.后来发现是相对路径的问题,因为执行pytho ...
随机推荐
- settTimeout vs setInterval
setTimeout:过一段固定的时间后,将代码提交到代码队列中排队. setInterval:每隔一段固定的时间,执行一次代码. 他们两都接受两个参数,第一个参数是字符串或者函数,第二个参数是设定的 ...
- 2019-6-23-win10-uwp-解决-SerialDevice.FromIdAsync-返回空
title author date CreateTime categories win10 uwp 解决 SerialDevice.FromIdAsync 返回空 lindexi 2019-6-23 ...
- uni-app学习记录02-属性绑定.for循环
<template> <view class="content"> <text> 我是首页 </text> <!-- 输出纯字 ...
- (二)C#编程基础复习——变量和常量
今天要复习一下C#基础中的变量和常量,所谓变量,就是用来存储特定类型的数据,分为值类型和引类型,可以根据需要随时改变变量中所村存储的数据值,变量必须先声明,然后才能赋值:常量就是固定不变的值,常量的变 ...
- Gyn 100989 "1D Cafeteria (B)"(set+lower_bound)
传送门 •题意 某自助餐厅有 n 张桌子,桌子编号为 1~n,其中第 i 张桌子可容纳 ai 个人: 有两种操作: (1)in x : 有 x 个人来这家餐厅吃饭,需要找一个可容纳 x 人的桌子,并满 ...
- echarts 图表自适应外部盒子大小
项目中用到了echarts,由于页面是自适应的,还得兼容移动, 因此图表还需要根据盒子的大小来变化. 自适应窗口及盒子大小 页面中有一个[放大.缩小]功能,及全屏展示和预览图表 窗口自适应 let m ...
- 2018-8-10-win10-uwp-x_Bind-无法获得资源
title author date CreateTime categories win10 uwp x:Bind 无法获得资源 lindexi 2018-08-10 19:17:19 +0800 20 ...
- H3C TFTP协议介绍
- vue 使用webpack打包后路径报错以及 alias 的使用
一.vue 使用webpack打包后路径报错(两步解决) 1. config文件夹 ==> index.js ==> 把assetsPublicPath的 '/ '改为 './' 2. b ...
- 【record】#10
反正最近就一直在1600分左右徘徊;好想回蓝名啊