本文通过Java代码调用Shell脚本并传入参数实现DB2数据库表导出到文件,代码如下:

  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.io.LineNumberReader;
  5. import java.util.HashMap;
  6.  
  7. import org.slf4j.Logger;
  8. import org.slf4j.LoggerFactory;
  9.  
  10. import com.*.dmp.bean.AgentConfigInfo;
  11. import com.*.dmp.bean.MapKeys;
  12. import com.*.dmp.bean.RunStatus;
  13. import com.*.dmp.common.SpringUtils;
  14.  
  15. public class ExportDataServiceDB2 {
  16.  
  17. AgentConfigInfo agentConfigInfo = SpringUtils.getContext().getBean(AgentConfigInfo.class);
  18. private Logger LOG = LoggerFactory.getLogger(ExportDataServiceDB2.class);
  19. private StringBuffer resultMsg = new StringBuffer();
  20. String isOK = "0";
  21. private String exportShell = agentConfigInfo.getEXPORT_SHELL();
  22. // private String exportCMD = agentConfigInfo.getEXPORT_CMD();
  23. private StringBuffer exportFilePath = agentConfigInfo.getEXPORT_FILE_PATH();
  24.  
  25. /**
  26. * @Title: ExportData
  27. * @Description: 调用Shell脚本实现db2数据的导出
  28. * @param dataMap
  29. * @throws IOException 对方法的参数进行描述
  30. * @return HashMap<String,String> 返回类型
  31. */
  32. public HashMap<String, String> ExportData(HashMap<String, String> dataMap) throws IOException {
  33.  
  34. String dbSchema = dataMap.get("db_schema");
  35. String dbUser = dataMap.get("db_user");
  36. String dbPassword = dataMap.get("db_password");
  37. String tableName = dataMap.get("table_name");
  38. String interFile = dataMap.get("inter_file");
  39. String delimiter = dataMap.get("delimiter");
  40. String exportLimit = dataMap.get("export_limit");
  41.  
  42. String filePath = mkDirectory(exportFilePath, interFile);
  43. dataMap.put("file_abs_path", filePath);
  44.  
  45. String cmdPara = createExportShellParams(dbSchema, dbUser,
  46. dbPassword, tableName, filePath, delimiter, exportLimit);
  47.  
  48. LOG.info("Export Parameters: " + cmdPara);
  49. resultMsg.append("Export Parameters: " + cmdPara + "\n");
  50.  
  51. String cmd = exportShell + " " + cmdPara;
  52.  
  53. Process ps = null;
  54. InputStreamReader isr = null;
  55. LineNumberReader input = null;
  56. String line = null;
  57.  
  58. try {
  59. LOG.info("Run Command: " + cmd );
  60. resultMsg.append("Run Command: " + cmd + "\n");
  61.  
  62. ps = Runtime.getRuntime().exec(cmd);
  63. isr = new InputStreamReader(ps.getInputStream()); // 使用Reader进行输入读取和打印
  64. input = new LineNumberReader(isr);
  65.  
  66. while (null != (line = input.readLine())) {
  67. LOG.info(line);
  68. resultMsg.append(line);
  69. if (line.contains("failed") || line.contains("Failed") || line.contains("FAILED") || line.contains("错误")) {
  70. isOK = RunStatus.EXPORT_FAIL;
  71. dataMap.put("export_status", isOK);
  72. dataMap.put("proc_log", resultMsg.toString());
  73. // dataMap = packageResult(isOK, resultMsg.toString()); // 组装返回的消息
  74. return dataMap;
  75. } else {
  76. isOK = RunStatus.PROC_RUN_SUCCESS;
  77. }
  78. }
  79.  
  80. // if (0 != ps.waitFor()) {
  81. // isOK = RunStatus.EXPORT_FAIL;
  82. // } else {
  83. // isOK = RunStatus.PROC_RUN_SUCCESS;
  84. // }
  85.  
  86. } catch (IOException e) {
  87. LOG.error("Run the Command Exception: " + cmd + ": " + e.getMessage());
  88. resultMsg.append("Run the Command Exception: " + cmd + ": " + e.getMessage() + "\n");
  89. isOK = RunStatus.EXPORT_FAIL;
  90. } finally {
  91. if (null != input) {
  92. input.close();
  93. }
  94.  
  95. if (null != isr) {
  96. isr.close();
  97. }
  98.  
  99. if (null != ps) {
  100. ps.destroy();
  101. ps = null;
  102. }
  103. }
  104.  
  105. dataMap.put("export_status", isOK);
  106. dataMap.put("proc_log", resultMsg.toString());
  107. // dataMap = packageResult(isOK, resultMsg.toString()); // 组装返回的消息
  108.  
  109. return dataMap;
  110.  
  111. }
  112.  
  113. /**
  114. * @Title: createExportShellParams
  115. * @Description: 组装参数
  116. * @param msgId
  117. * @param dbSchema
  118. * @param dbUser
  119. * @param dbPassword
  120. * @param tableName
  121. * @param filePath
  122. * @param delimiter
  123. * @param exportLimit
  124. * @return String 返回类型
  125. * @throws
  126. */
  127. private String createExportShellParams(String dbSchema,
  128. String dbUser, String dbPassword, String tableName,
  129. String filePath, String delimiter, String exportLimit) {
  130.  
  131. StringBuilder params = new StringBuilder();
  132. params.append(dbSchema + " ").append(dbUser + " ").append(dbPassword + " ")
  133. .append(tableName + " ").append(filePath + " ").append(delimiter + " ").append(exportLimit);
  134.  
  135. return params.toString();
  136. }
  137.  
  138. /**
  139. * @Title: mkDirectory
  140. * @Description: 根据配置的路径和文件名,判断文件路径是否存在,若不存在,则先创建,拼接导出文件绝对路径。
  141. * @param filePath
  142. * @param interFile
  143. * @return 对方法的参数进行描述
  144. * @return String 返回类型
  145. * @throws
  146. */
  147. private String mkDirectory(StringBuffer filePath, String interFile) {
  148.  
  149. File file = new File(filePath.toString());
  150.  
  151. if ( file.isDirectory() ) {
  152. if (filePath.toString().endsWith("/")) {
  153. filePath.append(interFile);
  154. } else {
  155. filePath.append("/").append(interFile);
  156. }
  157. } else {
  158. LOG.info("The file path is not exists, need to be created now. ");
  159. file.mkdir();
  160. if (filePath.toString().endsWith("/")) {
  161. filePath.append(interFile);
  162. } else {
  163. filePath.append("/").append(interFile);
  164. }
  165. }
  166. return filePath.toString();
  167. }
  168.  
  169. /** 返回消息组装结果 */
  170. private HashMap<String, String> packageResult(String isOK, String resultMsg) {
  171. HashMap<String, String> hsmap = new HashMap<String, String>();
  172. hsmap.put(MapKeys.PROC_STATUS, isOK);
  173. hsmap.put(MapKeys.PROC_LOG, resultMsg);
  174. return hsmap;
  175. }
  176.  
  177. }

  

  传入的执行参数放入一个Map(HashMap<String, String> dataMap)中:

  1. /** EXPORT TEST */
  2. map.put("db_schema", "md");
  3. map.put("db_user", "root");
  4. map.put("db_password", "root");
  5. map.put("table_name", "inter_log");
  6. map.put("inter_file", "inter_log_20140915.avl");
  7. map.put("delimiter", "|");
  8. map.put("export_limit", "");

  

  代码执行之后,将执行日志以及执行结果也存入该Map中一起返回:

  1. dataMap.put("export_status", isOK);
  2. dataMap.put("proc_log", resultMsg.toString());
  3.  
  4. return dataMap;

  

  执行结果界面:

Java代码调用Shell脚本并传入参数实现DB2数据库表导出到文件的更多相关文章

  1. Shell脚本实现DB2数据库表导出到文件

    该Shell脚本用于实现将DB2数据库表导出到文件,将在另一篇博文<Java代码调用Shell脚本并传入参数实现DB2数据库表导出到文件>中通过Java代码实现调用该脚本并传入参数. #! ...

  2. [Python]在python中调用shell脚本,并传入参数-02python操作shell实例

    首先创建2个shell脚本文件,测试用. test_shell_no_para.sh 运行时,不需要传递参数 test_shell_2_para.sh 运行时,需要传递2个参数  test_shell ...

  3. 用java代码调用shell脚本执行sqoop将hive表中数据导出到mysql

    1:创建shell脚本 touch sqoop_options.sh chmod 777 sqoop_options.sh 编辑文件  特地将执行map的个数设置为变量  测试 可以java代码传参数 ...

  4. Java如何调用shell脚本的

    有些时候会碰到这样的场景:java的功能里面要嵌入一个功能点,这个功能是通过是shell脚本实现的.这种时候就需要Java对脚本调用的支持了. 测试环境 Ubuntu16.04 i3-6100,12G ...

  5. 利用jmeter发起java请求调用shell脚本

    1.创建maven项目 在pom文件中加入依赖:     2.在路径src/main/java下创建类,如类名shellclass                     3.      创建jmet ...

  6. 向shell脚本中传入参数

    写一个 程序名为    test.sh    可带参数为 start 和 stop 执行  test.sh start执行  start 内容的代码 执行 test.sh stop 执行 stop 内 ...

  7. shell脚本获取传入参数的个数

    ts.sh #!/bin/bash echo $# 输出 [root@redhat6 ~]# ./ts.sh para1 [root@redhat6 ~]# ./ts.sh para1 para2 [ ...

  8. Java 调用 shell 脚本详解

    这一年的项目中,有大量的场景需要Java 进程调用 Linux的bash shell 脚本实现相关功能. 从之前的项目中拷贝的相关模块和网上的例子来看,有个别的“陷阱”造成调用shell 脚本在某些特 ...

  9. python调用shell脚本时需要切换目录

    最近遇到了一个问题,就是python代码调用shell脚本时,发现输入输出的文件,总是和自己预想的有偏差,但是单独在linux下执行命令的时候,却没有错误.后来发现是相对路径的问题,因为执行pytho ...

随机推荐

  1. 洛谷P1288 取数游戏II 题解 博弈论

    题目链接:https://www.luogu.org/problem/P1288 首先,如果你的一边的边是 \(0\) ,那么你肯定走另一边. 那么你走另一边绝对不能让这条边有剩余,因为这条边有剩余的 ...

  2. jq常用动画fade slide

    https://www.cnblogs.com/sandraryan/ hide(); <div class="box">big box</div> $(' ...

  3. UVa11400 - Lighting System Design——[动态规划]

    题干略. 题意分析: 很容易理解一类灯泡要么全部换要么全不换,其实费用节省的主要原因是由于替换灯泡类型而排除了低压电压源,于是我们就可以推断出灯泡类型替换的原则: 对于两类灯泡a1和a2,a1可以被a ...

  4. zoj 2954 Hanoi Tower

    Hanoi Tower Time Limit: 2 Seconds Memory Limit: 65536 KB You all must know the puzzle named "Th ...

  5. html(四)数据库curd操作与分页查询

    数据库操作curd : 1.首先要建立项目处理好自己逻辑包: 其中util工具包中建立两个工具类 jdbc连接和page分页 DBUtil.java: db工具类就是用于连接数据库的jdbc架包,里面 ...

  6. [C++] 烦人的error LNK2019和error LNK2001

    常见原因: 没有正确的设置引用的lib,新手常犯这个错误,这是最容易解决的问题. extern "C"的问题.如果C++写的dll要给C程序用,那么就要注意extern " ...

  7. 【React】 百度地图API

    百度地图 开发文档 :http://lbsyun.baidu.com/index.php?title=jspopular 调用接口 需要 内置加载一个 百度api文件    使用自己的ak  申请一个 ...

  8. java 反射的概念

    反射的引入: Object obj = new Student(); 若程序运行时接收到外部传入的一个对象,该对象的编译类型是Object,但程序又需要调用该对象运行类型的方法: 1.若编译和运行类型 ...

  9. P1016 高精度除法

    题目描述 给你两个很大的正整数A和B,你需要计算A除以B的商和余数. 输入格式 输入一行包含两个正整数A和B,以一个空格分隔(A和B的位数都不超过 \(10^5\)) 输出格式 输出一行包含两个整数, ...

  10. HDU 1087 Super Jumping....(动态规划之最大递增子序列和)

    Super Jumping! Jumping! Jumping! Problem Description Nowadays, a kind of chess game called “Super Ju ...