Java code invokes shell script and passes in parameter implementation DB2 database table export to file

Source: Internet
Author: User
Tags getmessage


This article calls the shell script through Java code and passes in the parameter implementation DB2 the database table exports to the file, the code is as follows:

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);p Rivate logger log = loggerfactory.getlogger ( Exportdataservicedb2.class);p Rivate stringbuffer resultmsg = new stringbuffer (); string isok =  "0";p Rivate string exportshell = agentconfiginfo.getexport_ SHELL ();//private string exportcmd = agentconfiginfo.getexport_cmd ();p rivate  Stringbuffer exportfilepath = agentconfiginfo.getexport_file_path ();/** *  @Title: exportdata  *  @Description:   invoke shell script to implement DB2 data export  *   @param  dataMap *  @throws  IOException  Description of method parameters  *  @return   hashmap<string,string>  return type  */public hashmap<string, string> exportdata ( HASHMAP&LT;STRING,&NBSP;STRING&GT;&NBSP;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);d atamap.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");p s  = runtime.getruntime (). exec (cmd); Isr = new inputstreamreader (Ps.getinputstream ());  //  using reader for input read and print 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 ("error"))  {isok = runstatus.export_fail;datamap.put ("Export_status",  isOK );d atamap.put ("Proc_log",  resultmsg.tostring ());//datamap = packageresult (isOK,  Resultmsg.tostring ());//  assembly returns the message 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&NBSP;!=&NBSP;ISR)  {isr.close ();} if  (Null != ps)  {ps.destroy ();p s = null;}} Datamap.put ("Export_status",  isok);d atamap.put ("Proc_log",  resultmsg.tostring ());//dataMap =  packageresult (Isok, resultmsg.tostring ());//  assembly returns the message Return datamap;} /** *  @Title: createexportshellparams  *  @Description:   assembly Parameters  *   @param  msgId *  @param  dbSchema *  @param  dbUser *  @param  dbPassword *  @param  tableName *  @param  filePath *  @param   delimiter *  @param  exportLimit *  @return  String  return type  *  @throws  */private string createexportshellparams (string dbschema, string dbuser,  string dbpassword, string tablename,string filepath, string delimiter,  String exportlimit)  {stRingbuilder params = new stringbuilder ();p arams.append (dbschema +  " "). Append (dbuser +  " "). Append (dbpassword +  " "). Append (tablename +  "   "). Append (filepath + "   "). Append (delimiter + "   "). Append (Exportlimit); Return params.tostring ();} /** *  @Title: mkdirectory  *  @Description:   Determine if a file path exists based on the configured path and file name. If not, the absolute path of the exported file is created first.  *  @param  filePath *  @param  interFile *  @return   Describe the parameters of the method  *   @return  String  return type  *  @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 ();} /**  returns the message assembly result  */private hashmap<string, string> packageresult (String isOK, &NBSP;STRING&NBSP;RESULTMSG)  {hashmap<string, string> hsmap = new hashmap <String, String> Hsmap.put (Mapkeys.proc_status, isok); Hsmap.put (mapkeys.proc_log,  RESULTMSG); return hsmap;}}


The incoming execution parameters are placed in a 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", "" ");


After the code executes, the execution log and execution results are also stored in the map to return

Datamap.put ("Export_status", IsOK);d atamap.put ("Proc_log", resultmsg.tostring ()); return dataMap;


Execution result Interface:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/49/AA/wKioL1QYCYfzuZzpAAIgt-iNz48024.jpg "title=" Qq20140916174454.png "alt=" Wkiol1qycyfzuzzpaaigt-inz48024.jpg "/>



This article is from the "forever_love_ing" blog, make sure to keep this source http://dwf07223.blog.51cto.com/8712758/1553422

Java code invokes shell script and passes in parameter implementation DB2 database table export to file

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.