搜索
查看: 1311|回复: 0

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

[复制链接]

142

主题

3

回帖

492

积分

版主

积分
492
发表于 2014-10-30 16:42:29 | 显示全部楼层 |阅读模式
本帖最后由 MEI 于 2014-10-30 16:44 编辑

本文通过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. import org.slf4j.Logger;

  7. import org.slf4j.LoggerFactory;



  8. import com.*.dmp.bean.AgentConfigInfo;

  9. import com.*.dmp.bean.MapKeys;

  10. import com.*.dmp.bean.RunStatus;

  11. import com.*.dmp.common.SpringUtils;



  12. public class ExportDataServiceDB2 {



  13.     AgentConfigInfo agentConfigInfo = SpringUtils.getContext().getBean(AgentConfigInfo.class);

  14.     private Logger LOG = LoggerFactory.getLogger(ExportDataServiceDB2.class);

  15.     private StringBuffer resultMsg = new StringBuffer();

  16.     String isOK = "0";

  17.     private String exportShell = agentConfigInfo.getEXPORT_SHELL();

  18. //  private String exportCMD = agentConfigInfo.getEXPORT_CMD();

  19.     private StringBuffer exportFilePath = agentConfigInfo.getEXPORT_FILE_PATH();

  20.    

  21.     /**

  22.     * @Title: ExportData

  23.     * @Description:  调用Shell脚本实现db2数据的导出

  24.     * @param dataMap

  25.     * @throws IOException 对方法的参数进行描述

  26.     * @return HashMap<String,String> 返回类型

  27.     */

  28.     public HashMap<String, String> ExportData(HashMap<String, String> dataMap) throws IOException {



  29.         String dbSchema = dataMap.get("db_schema");

  30.         String dbUser = dataMap.get("db_user");

  31.         String dbPassword = dataMap.get("db_password");

  32.         String tableName = dataMap.get("table_name");

  33.         String interFile = dataMap.get("inter_file");

  34.         String delimiter = dataMap.get("delimiter");

  35.         String exportLimit = dataMap.get("export_limit");

  36.       

  37.         String filePath = mkDirectory(exportFilePath, interFile);

  38.         dataMap.put("file_abs_path", filePath);

  39.       

  40.         String cmdPara = createExportShellParams(dbSchema, dbUser,

  41.                 dbPassword, tableName, filePath, delimiter, exportLimit);



  42.         LOG.info("Export Parameters: " + cmdPara);

  43.         resultMsg.append("Export Parameters: " + cmdPara + "\n");

  44.       

  45.         String cmd = exportShell + " " + cmdPara;

  46.       

  47.         Process ps = null;

  48.         InputStreamReader isr = null;

  49.         LineNumberReader input = null;

  50.         String line = null;



  51.         try {

  52.             LOG.info("Run Command:  " + cmd );

  53.             resultMsg.append("Run Command:  " + cmd +  "\n");

  54.            

  55.             ps = Runtime.getRuntime().exec(cmd);

  56.             isr = new InputStreamReader(ps.getInputStream()); // 使用Reader进行输入读取和打印

  57.             input = new LineNumberReader(isr);



  58.             while (null != (line = input.readLine())) {

  59.                 LOG.info(line);

  60.                 resultMsg.append(line);

  61.                 if (line.contains("failed") || line.contains("Failed") || line.contains("FAILED") || line.contains("错误")) {

  62.                     isOK = RunStatus.EXPORT_FAIL;

  63.                     dataMap.put("export_status", isOK);

  64.                     dataMap.put("proc_log", resultMsg.toString());

  65. //                  dataMap = packageResult(isOK, resultMsg.toString()); // 组装返回的消息

  66.                     return dataMap;

  67.                 } else {

  68.                     isOK = RunStatus.PROC_RUN_SUCCESS;

  69.                 }

  70.             }

  71.            

  72. //              if (0 != ps.waitFor()) {

  73. //                  isOK = RunStatus.EXPORT_FAIL;

  74. //              } else {

  75. //                  isOK = RunStatus.PROC_RUN_SUCCESS;

  76. //              }



  77.         } catch (IOException e) {

  78.             LOG.error("Run the Command Exception: " + cmd + ": " + e.getMessage());

  79.             resultMsg.append("Run the Command Exception: " + cmd  + ": " + e.getMessage() + "\n");

  80.             isOK = RunStatus.EXPORT_FAIL;

  81.         } finally {

  82.             if (null != input) {

  83.                 input.close();

  84.             }



  85.             if (null != isr) {

  86.                 isr.close();

  87.             }



  88.             if (null != ps) {

  89.                 ps.destroy();

  90.                 ps = null;

  91.             }

  92.         }

  93.       

  94.         dataMap.put("export_status", isOK);

  95.         dataMap.put("proc_log", resultMsg.toString());

  96. //      dataMap = packageResult(isOK, resultMsg.toString()); // 组装返回的消息



  97.         return dataMap;



  98.     }



  99.     /**

  100.     * @Title: createExportShellParams

  101.     * @Description:  组装参数

  102.     * @param msgId

  103.     * @param dbSchema

  104.     * @param dbUser

  105.     * @param dbPassword

  106.     * @param tableName

  107.     * @param filePath

  108.     * @param delimiter

  109.     * @param exportLimit

  110.     * @return String 返回类型

  111.     * @throws

  112.     */

  113.     private String createExportShellParams(String dbSchema,

  114.             String dbUser, String dbPassword, String tableName,

  115.             String filePath, String delimiter, String exportLimit) {



  116.         StringBuilder params = new StringBuilder();

  117.         params.append(dbSchema + " ").append(dbUser + " ").append(dbPassword + " ")

  118.             .append(tableName + " ").append(filePath + " ").append(delimiter + " ").append(exportLimit);



  119.         return params.toString();

  120.     }



  121.     /**

  122.     * @Title: mkDirectory

  123.     * @Description:  根据配置的路径和文件名,判断文件路径是否存在,若不存在,则先创建,拼接导出文件绝对路径。

  124.     * @param filePath

  125.     * @param interFile

  126.     * @return 对方法的参数进行描述

  127.     * @return String 返回类型

  128.     * @throws

  129.     */

  130.     private String mkDirectory(StringBuffer filePath, String interFile) {

  131.       

  132.         File file = new File(filePath.toString());

  133.       

  134.         if ( file.isDirectory() ) {

  135.             if (filePath.toString().endsWith("/")) {

  136.                 filePath.append(interFile);

  137.             } else {

  138.                 filePath.append("/").append(interFile);

  139.             }

  140.         } else {

  141.             LOG.info("The file path is not exists, need to be created now. ");

  142.             file.mkdir();

  143.             if (filePath.toString().endsWith("/")) {

  144.                 filePath.append(interFile);

  145.             } else {

  146.                 filePath.append("/").append(interFile);

  147.             }

  148.         }

  149.         return filePath.toString();

  150.     }



  151.     /** 返回消息组装结果 */

  152.     private HashMap<String, String> packageResult(String isOK, String resultMsg) {

  153.         HashMap<String, String> hsmap = new HashMap<String, String>();

  154.         hsmap.put(MapKeys.PROC_STATUS, isOK);

  155.         hsmap.put(MapKeys.PROC_LOG, resultMsg);

  156.         return hsmap;

  157.     }



  158. }
复制代码


传入的执行参数放入一个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. return dataMap;
复制代码

执行结果界面:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

 
 
大数据行业交流
大数据行业交流
大数据求职招聘
大数据求职招聘
站长电话:
15010106923
微信联系:
hb-0310
站长邮箱:
ab12-120@163.com
大数据中国微信

QQ   

版权所有: Discuz! © 2001-2013 大数据.

GMT+8, 2024-5-5 03:19 , Processed in 0.113000 second(s), 24 queries .

快速回复 返回顶部 返回列表