View Javadoc
1   /**
2    * This Source Code Form is subject to the terms of the Mozilla Public
3    * License, v. 2.0. If a copy of the MPL was not distributed with this
4    * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5    *
6    * If it is not possible or desirable to put the notice in a particular
7    * file, then You may include the notice in a location (such as a LICENSE
8    * file in a relevant directory) where a recipient would be likely to look
9    * for such a notice.
10  
11   * 
12   */
13   
14  /*  ---------------------------------------------------------------------------
15   *  U.S. Government, Department of the Army
16   *  Army Materiel Command
17   *  Research Development Engineering Command
18   *  Communications Electronics Research Development and Engineering Center
19   *  ---------------------------------------------------------------------------
20   */
21   package org.miloss.fgsms.examples.export;
22  
23  import java.io.FileOutputStream;
24  import java.sql.Connection;
25  import java.sql.Driver;
26  import java.sql.DriverManager;
27  import java.sql.PreparedStatement;
28  import java.sql.ResultSet;
29  import org.miloss.fgsms.common.Constants;
30  import org.miloss.fgsms.common.Utility;
31  
32  public class ExportDataFromDatabase {
33  
34      public static void main(String[] args) throws Exception {
35          if (args.length == 0) {
36              PrintUsage();
37              return;
38          }
39          long maxrecords = Long.parseLong(args[1]);
40          FileOutputStream fos = new FileOutputStream(args[2]);
41  
42          String username = getString("Username", "fgsms");
43          String password = getStringPw();
44          String url = getString("URL", "jdbc:postgresql://localhost:5432/fgsms_performance");
45          Connection con = getConnection(url, username, password);
46          if (args[0].equalsIgnoreCase("transactions")) {
47              PreparedStatement cmd = con.prepareStatement("select * from rawdata limit ?");
48              cmd.setLong(1, maxrecords);
49              ResultSet rs = cmd.executeQuery();
50              while (rs.next()) {
51                  StringBuilder sb = new StringBuilder();
52                  sb.append(rs.getString("transactionid")).append(",").append(rs.getString("uri")).append(",").append(rs.getLong("responsetimems")).append(",").append(rs.getLong("utcdatetime"));
53                  fos.write(sb.toString().getBytes());
54                  fos.write(System.getProperty("line.separator").getBytes(Constants.CHARSET));
55                  sb = null;
56                  //NOTE some of the columns are encrypted, to decrypt run the following
57                  //String clearText = Utility.DE("cipherText");
58              }
59              rs.close();
60              cmd.close();
61          } else if (args[0].equalsIgnoreCase("availability")) {
62              PreparedStatement cmd = con.prepareStatement("select * from availability limit ?");
63              cmd.setLong(1, maxrecords);
64              ResultSet rs = cmd.executeQuery();
65              while (rs.next()) {
66                  StringBuilder sb = new StringBuilder();
67                  sb.append(rs.getString("uri")).append(",").append(rs.getString("message")).append(",").append(rs.getLong("utcdatetime"));
68                  fos.write(sb.toString().getBytes());
69                  fos.write(System.getProperty("line.separator").getBytes(Constants.CHARSET));
70                  sb = null;
71              }
72              rs.close();
73              cmd.close();
74          } else if (args[0].equalsIgnoreCase("aggregate")) {
75              PreparedStatement cmd = con.prepareStatement("select * from agg2");
76              cmd.setLong(1, maxrecords);
77              ResultSet rs = cmd.executeQuery();
78              while (rs.next()) {
79                  StringBuilder sb = new StringBuilder();
80                  sb.append(rs.getString("uri")).append(",").append(rs.getString("soapaction")).append(",").append(rs.getLong("timerange")).append(",").append(rs.getLong("success"));
81                  fos.write(sb.toString().getBytes());
82                  fos.write(System.getProperty("line.separator").getBytes(Constants.CHARSET));
83                  sb = null;
84              }
85              rs.close();
86              cmd.close();
87          }
88          con.close();
89          fos.close();
90      }
91  
92      private static String getString(String field, String defaults) {
93          String s = null;
94          //while (s == null || s.length() == 0) {
95          System.out.print(field + "[" + defaults + "] = ");
96          s = System.console().readLine();
97          //}
98          if (s == null || s.length() == 0) {
99              return defaults;
100         }
101         return s;
102     }
103 
104     private static String getStringPw() {
105         System.out.print("Password = ");
106         return new String(System.console().readPassword());
107     }
108 
109     private static Connection getConnection(String url, String username, String password) throws Exception {
110         //String url = "jdbc:postgresql://localhost:5432/fgsmsPerformance";
111         Driver d = (Driver) Class.forName("org.postgresql.Driver").newInstance();
112         return DriverManager.getConnection(url, username, password);
113 
114     }
115 
116     private static void PrintUsage() {
117         System.out.println("This program will export data from fgsms's databases. It's just a sample application, so feel free to expand on this.");
118         System.out.println("Usage");
119         System.out.println("java -jar ExportDataFromDatabase.jar <datatype> <maxrecords> <outputfile>");
120         System.out.println("datatype can be one of: ");
121         System.out.println("\ttransactions");
122         System.out.println("\tavailability");
123         System.out.println("\taggregate");
124     }
125 }