I have a database containing a high amount of data. I want my app to filter and select only some of it, and save it into a CSV file or JSON.
For the moment I have this method which takes the data from the database and puts it in a CSV:
void viewAllCounters (String tableName, long startDate, long endDate, Connection c) throws SQLException {
Statement stmt = null;
String query = "SELECT receivedtime, source, numulethbytesinter, numdlethbytesinter, numulethbytessent, numdlethbytessent, "
+ "numuniquegtpteids, numulbytessenttoaccl, numdlbytessenttoaccl "
+ " FROM " + tableName + " WHERE CAST(receivedtime AS integer) BETWEEN "+ startDate +" AND " + endDate +" ORDER BY receivedtime;";
try {
stmt = c.createStatement();
ResultSet rs = stmt.executeQuery(query);
int i = 0;
double last = 0;
DecimalFormat decFor = new DecimalFormat("0.000");
SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT);
try {
File file = new File("KPI-"+tableName+"-"+ endDate +".csv");
FileWriter fileWriter = new FileWriter(file);
fileWriter.append("Date");
fileWriter.append(',');
fileWriter.append("Source");
fileWriter.append(',');
fileWriter.append("AUIDR");
fileWriter.append(',');
fileWriter.append("ADIDR");
fileWriter.append(',');
fileWriter.append("AUEDR");
fileWriter.append(',');
fileWriter.append("ADEDR");
fileWriter.append(',');
fileWriter.append("AUTPG");
fileWriter.append(',');
fileWriter.append("ADTPG");
fileWriter.append(',');
fileWriter.append("AST");
fileWriter.append('\n');
while (rs.next()) {
if (last != 0){
String receivedtime = rs.getString("receivedtime");
String source = rs.getString("source");
double numulethbytesinter = Double.parseDouble(rs.getString("numulethbytesinter"));
double numdlethbytesinter = Double.parseDouble(rs.getString("numdlethbytesinter"));
double numulethbytessent = Double.parseDouble(rs.getString("numulethbytessent"));
double numdlethbytessent = Double.parseDouble(rs.getString("numdlethbytessent"));
double numuniquegtptieds = Double.parseDouble(rs.getString("numuniquegtpteids"));
double numulbytessenttoaccl = Double.parseDouble(rs.getString("numulbytessenttoaccl"));
double numdlbytessenttoaccl = Double.parseDouble(rs.getString("numdlbytessenttoaccl"));
//period between this entry and the last one (in second)
if (Double.parseDouble(receivedtime) > last) {
double period = Double.parseDouble(receivedtime) - last;
double AUIDR = (8*numulethbytesinter)/(1000000*period);
double ADIDR = (8*numdlethbytesinter)/(1000000*period);
double AUEDR = (8*numulethbytessent)/(1000000*period);
double ADEDR = (8*numdlethbytessent)/(1000000*period);
double AUTPG, ADTPG;
if (numuniquegtptieds != 0){
AUTPG = (8*numulbytessenttoaccl)/(numuniquegtptieds*1000000*period);
ADTPG = (8*numdlbytessenttoaccl)/(numuniquegtptieds*1000000*period);
}else{
AUTPG = 0;
ADTPG = 0;
}
double AST = (8*(numdlbytessenttoaccl+numulbytessenttoaccl))/(1000000*period);
String dateReceived = formatter.format(new Date (Long.parseLong(receivedtime)*1000));
fileWriter.append(dateReceived);
fileWriter.append(',');
fileWriter.append(source);
fileWriter.append(',');
fileWriter.append("" + AUIDR);
fileWriter.append(',');
fileWriter.append("" + ADIDR);
fileWriter.append(',');
fileWriter.append("" +AUEDR);
fileWriter.append(',');
fileWriter.append("" + ADEDR);
fileWriter.append(',');
fileWriter.append("" + AUTPG);
fileWriter.append(',');
fileWriter.append("" +ADTPG);
fileWriter.append(',');
fileWriter.append("" + AST);
fileWriter.append('\n');
i++;
}
} else {
last = Double.parseDouble(rs.getString("receivedtime"));
}
}
fileWriter.flush();
fileWriter.close();
} catch (IOException e) {
System.err.println(e.getClass().getName()+": "+e.getMessage() + "\n\n\n");
}
} catch (SQLException e ) {
System.err.println(e.getClass().getName()+": "+e.getMessage() + "\n\n\n"); }
finally {
if (stmt != null) { stmt.close(); }
}
}
This method is in a class called Dbfunctions
which is called in the main method of my main class. I could add more fields to get in the future.
As you can see I create a CSV file and add all the data. But I am wondering, is there any way to make it faster or at least more readable?
I thought to add all this data in a List
and return this list to create this. I guess the best for me is a Queue
? Fast to insert fast to remove and first in first out. I could create a Queue<Queue<String>>
so I will have:
Queue(QueueIterration1{[String DateIterration1],[String SourceIterration1]...}, QueueIterration2{...}, ...).
So I will pop the QueueIterration1
then a for
loop to pop each element. Then pop QueueIterration2
etc...
So my question is: is it a good idea? Speaking about performances, time to write all the data. And do you have any suggestions on how to improve my code?
(Now I have 4 methods like this one to take different data from the database.)