1. 程式人生 > >利用groovy把表中資料匯出成txt或csv

利用groovy把表中資料匯出成txt或csv

膠水語言就是膠水,寫起也挺快的.這個指令碼主要是從資料庫中把表中的資料匯出來生成檔案.而不用每次都開啟資料庫編輯器去手工收集.然後結合強大的指令碼(shell,bat)等基本就可以實現定時生成最新資料文字了.

目前這個groovy指令碼只要輸入sql語句,便會自動去反射表中的欄位和型別.
指令碼名稱:export.groovy
  1. importgroovy.sql.Sql;
  2. try
  3. {
  4. if(this.args.size()<1)
  5. {
  6. println"格式錯誤!請參照下面的正確格式"
  7. println"export[sql:可以正確執行的sql語句]"
  8. return;
  9. }
  10. }
  11. catch(Exceptione)
  12. {
  13. println"格式錯誤!請參照下面的正確格式"
  14. println"export[sql:可以正確執行的sql語句]"
  15. return;
  16. }
  17. /*
  18. 連線物件
  19. 可以根據你自己的需要調整
  20. */
  21. sql=Sql.newInstance("jdbc:oracle:thin:@10.220.51.40:1521:ORA10G","使用者名稱","密碼","oracle.jdbc.driver.OracleDriver");
  22. /*SQL語句可以根據自己需要調整*/
  23. strSql=this.args[0];
  24. //方法_獲取表名
  25. defgetTableName={strSql->
  26. pattern=~"FROM.*"
  27. matcher=strSql.toUpperCase()=~pattern
  28. tableName="";
  29. while(matcher.find()){
  30. tableName=matcher.group()
  31. }
  32. returntableName.split('')[1];
  33. }
  34. //方法判斷字串裡面是否有中文
  35. defisChinaese={str->
  36. if(str==null)
  37. return;
  38. for(iin0..str.size()-1)
  39. {
  40. if(str.charAt(i)>127)
  41. {
  42. returntrue;
  43. }
  44. }
  45. returnfalse;
  46. }
  47. deffile_tableName=getTableName(strSql);//表名
  48. defresult_columnCount=0;//列數
  49. defresult_columnName=[];
    //列名
  50. defresult_columnType=[];//列型別
  51. defresult_rowsData=[];//行資料
  52. defrows_count=0;
  53. //在該指令碼的同級目錄下生成已表名命名的csv檔案
  54. file_csv=newFile("${file_tableName}.txt");
  55. //判斷檔案是否存在,如果存在寫刪除
  56. if(file_csv.exists()){
  57. println"在當前目錄下發現已經存在${file_tableName},程式已經刪除檔案"
  58. file_csv.delete()
  59. }
  60. println"準備生成${file_tableName}.csv"
  61. sql.eachRow(strSql,
  62. {
  63. result_columnCount=it.getColumnCount();
  64. println"*********${file_tableName}表結構**********"
  65. println":本次匯出共生成${result_columnCount}個欄位";
  66. for(iin1..result_columnCount){//不同與java,groovy下標從1開始
  67. printlnit.getColumnName(i)+"|"+it.getColumnTypeName(i);
  68. result_columnName<<it.getColumnName(i);
  69. result_columnType<<it.getColumnTypeName(i);
  70. }
  71. println"*********${file_tableName}表結構**********"
  72. file_csv<<result_columnName.join(",")+System.getProperty("line.separator");
  73. println"開始生成資料,請耐心等待......"
  74. },
  75. {
  76. rows_count++;
  77. result_rowsData=[];//清空物件
  78. for(jin0..result_columnCount-1)
  79. {
  80. if(result_columnType[j]=="VARCHAR2"||result_columnType[j]=="VARCHAR"){//針對VARCHAR型別進行處理,加上"號
  81. Stringstrtemp="";
  82. //先要判斷是否含有中文,有中文就不加"號
  83. /*if(isChinaese(it[j]))
  84. {
  85. strtemp=it[j]
  86. }
  87. else
  88. {
  89. strtemp="\""+it[j]+"\"";
  90. }
  91. */
  92. strtemp="\""+it[j]+"\"";
  93. result_rowsData<<strtemp;
  94. }
  95. else//其他型別不做處理
  96. {
  97. result_rowsData<<it[j];
  98. }
  99. }
  100. file_csv<<result_rowsData.join(",")+System.getProperty("line.separator");
  101. }
  102. );
  103. println"${file_tableName}資料生成完畢(匯出資料行數:${rows_count})\n"


然後再給這個groovy指令碼寫幾個對應的shell\bat執行指令碼
比如:
檔名:生成基站.bat
  1. groovyexport"select*fromshw.chal_cell_base_info"

如圖:



那麼當每次都要生成最新資料的時候,直接去雙擊這些bat檔案即可.或者你直接把這些bat配置到作業系統的執行任務當中去即可.