java导出db2_Java代码调用Shell脚本并传入参数实现DB2数据库表导出到文件

这篇博客展示了如何使用Java代码调用Shell脚本,并传入参数来导出DB2数据库的表内容到文件。文章详细讲解了相关代码实现,包括获取数据库连接信息、构建导出参数、执行Shell脚本以及处理输出结果的过程。
摘要由CSDN通过智能技术生成

本文通过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 返回类型

*/

public HashMap ExportData(HashMap 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 packageResult(String isOK, String resultMsg) {

HashMap hsmap = new HashMap();

hsmap.put(MapKeys.PROC_STATUS, isOK);

hsmap.put(MapKeys.PROC_LOG, resultMsg);

return hsmap;

}

}

传入的执行参数放入一个Map(HashMap 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;

执行结果界面:

390797a998da00ed01a18f78d5587f5a.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值