class DatabaseManagement modified in order to not make available the result in a file csv for submitquery and samplings operations.

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-access/DatabasesResourcesManager@99051 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Loredana Liccardo 2014-07-29 08:30:42 +00:00
parent 6769c97a2d
commit 21d1bb5a27
1 changed files with 245 additions and 386 deletions

View File

@ -113,24 +113,24 @@ public class DatabaseManagement {
type = type.toLowerCase(); type = type.toLowerCase();
// AnalysisLogger.getLogger() // AnalysisLogger.getLogger()
// .debug("In DatabaseManagement->type: " // .debug("In DatabaseManagement->type: "
// + type); // + type);
String valConverted = val; String valConverted = val;
// AnalysisLogger.getLogger() // AnalysisLogger.getLogger()
// .debug("In DatabaseManagement->val: " // .debug("In DatabaseManagement->val: "
// + valConverted); // + valConverted);
try { try {
// parse to Long // parse to Long
valConverted = "" + Long.parseLong(valConverted); valConverted = "" + Long.parseLong(valConverted);
// AnalysisLogger.getLogger() // AnalysisLogger.getLogger()
// .debug("In DatabaseManagement->parsed value Long: " // .debug("In DatabaseManagement->parsed value Long: "
// + valConverted); // + valConverted);
} }
@ -160,9 +160,9 @@ public class DatabaseManagement {
valConverted = "" + Double.parseDouble(valConverted); valConverted = "" + Double.parseDouble(valConverted);
// // check the parsed value // // check the parsed value
// AnalysisLogger.getLogger().debug( // AnalysisLogger.getLogger().debug(
// "In DatabaseManagement->parsed value Double: " // "In DatabaseManagement->parsed value Double: "
// + valConverted); // + valConverted);
} }
@ -171,24 +171,24 @@ public class DatabaseManagement {
valConverted = "" + Float.parseFloat(valConverted); valConverted = "" + Float.parseFloat(valConverted);
// // check the parsed value // // check the parsed value
// AnalysisLogger.getLogger().debug( // AnalysisLogger.getLogger().debug(
// "In DatabaseManagement->parsed value Float: " // "In DatabaseManagement->parsed value Float: "
// + valConverted); // + valConverted);
} }
if (((type.contains("string")))||(type.contains("varchar")) || (type.contains("char")) if (((type.contains("string")))
|| (type.contains("text")) || (type || (type.contains("varchar"))
.contains("character varying"))) { || (type.contains("char"))
|| (type.contains("text"))
|| (type.contains("character varying"))) {
valConverted= "\"" + valConverted + "\""; valConverted = "\"" + valConverted + "\"";
// // check the parsed value // // check the parsed value
// AnalysisLogger.getLogger().debug( // AnalysisLogger.getLogger().debug(
// "In DatabaseManagement->parsed value string: " // "In DatabaseManagement->parsed value string: "
// + valConverted); // + valConverted);
} }
@ -343,38 +343,22 @@ public class DatabaseManagement {
AnalysisLogger.getLogger().debug( AnalysisLogger.getLogger().debug(
"In DatabaseManagement->store table in a file"); "In DatabaseManagement->store table in a file");
// store table in a file
// writeQueryResultIntoFile(results);
// writeSampleTableIntoFile(results, tableName, schemaName);
String FileName = pathFile + "QueryResult.csv";
// List<String> listColumnNames = getColumnNamesTable(tableName, // List<String> listColumnNames = getColumnNamesTable(tableName,
// schemaName); // schemaName);
// to retrieve datatype columns of a table // to retrieve datatype columns of a table
List<String> DataTypeColumns = null; List<String> DataTypeColumns = null;
// write the result in the file and in the map // // store table in a file
// String FileName = pathFile + "QueryResult.csv";
AnalysisLogger.getLogger().debug( // // write the result in the file and in the map
"In DatabaseManagement->writing the result in the file: " // AnalysisLogger.getLogger().debug(
+ FileName); // "In DatabaseManagement->writing the result in the file: "
// + FileName);
file = new File(FileName); // file = new File(FileName);
// out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream( // file), "UTF-8"));
file), "UTF-8")); // writeTableIntoFile(results, DataTypeColumns);
// String header = null;
// writeTableIntoFile(results, DataTypeColumns, header, tableName,
// schemaName,
// FileName);
writeTableIntoFile(results, DataTypeColumns);
return results; return results;
@ -610,7 +594,8 @@ public class DatabaseManagement {
} }
// Method that returns the estimated number of rows // Method that returns the estimated number of rows
public long getNumberOfRows(String tablename, String schemaName) throws Exception { public long getNumberOfRows(String tablename, String schemaName)
throws Exception {
long rows; long rows;
@ -718,23 +703,7 @@ public class DatabaseManagement {
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream( out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
file), "UTF-8")); file), "UTF-8"));
// mapResult.put("HEADERS", header); // writeTableIntoFile(resultSet, DataTypeColumns);
//
// out.write(header);
// out.newLine();
// writeTableIntoFile(resultSet, DataTypeColumns, header, tableName,
// schemaName,
// FileName);
// writeTableIntoFile(resultSet, DataTypeColumns, tableName,
// schemaName,
// FileName);
writeTableIntoFile(resultSet, DataTypeColumns);
// return resultSet;
} }
/** /**
@ -758,7 +727,6 @@ public class DatabaseManagement {
AnalysisLogger.getLogger().debug( AnalysisLogger.getLogger().debug(
"DatabaseManagement->starting the Sample on table operation"); "DatabaseManagement->starting the Sample on table operation");
AnalysisLogger.getLogger().debug( AnalysisLogger.getLogger().debug(
"DatabaseManagement->retrieving the first 100 rows"); "DatabaseManagement->retrieving the first 100 rows");
@ -778,57 +746,27 @@ public class DatabaseManagement {
AnalysisLogger.getLogger().debug( AnalysisLogger.getLogger().debug(
"In DatabaseManagement->store table in a file"); "In DatabaseManagement->store table in a file");
// store table in a file
// writeSampleTableIntoFile(resultSet, tableName, schemaName);
String FileName = pathFile + "SampleResult.csv";
// to recover columns names list // to recover columns names list
List<String> listColumns = sampler.getListColumns(); List<String> listColumns = sampler.getListColumns();
// String header = "";
for (int i = 0; i < listColumns.size(); i++) { for (int i = 0; i < listColumns.size(); i++) {
if (i != listColumns.size() - 1) { if (i != listColumns.size() - 1) {
header = header + listColumns.get(i) + ", "; header = header + listColumns.get(i) + ", ";
} else { } else {
header = header + listColumns.get(i); header = header + listColumns.get(i);
} }
} }
// write the result in the file and in the map // // store table in a file
// String FileName = pathFile + "SampleResult.csv";
AnalysisLogger.getLogger().debug( // // write the result in the file and in the map
"In DatabaseManagement->writing the result in the file: " // AnalysisLogger.getLogger().debug(
+ FileName); // "In DatabaseManagement->writing the result in the file: "
// + FileName);
file = new File(FileName); // file = new File(FileName);
// out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream( // file), "UTF-8"));
file), "UTF-8")); // writeTableIntoFile(resultSet, DataTypeColumns);
// mapResult.put("HEADERS", header);
//
// out.write(header);
// out.newLine();
// System.out.println("HEADER:" + header);
// writeTableIntoFile(resultSet, DataTypeColumns, header, tableName,
// schemaName,
// FileName);
// writeTableIntoFile(resultSet, DataTypeColumns, tableName, schemaName,
// FileName);
writeTableIntoFile(resultSet, DataTypeColumns);
// return the first 100 rows
// return resultSet;
} }
@ -853,7 +791,6 @@ public class DatabaseManagement {
AnalysisLogger AnalysisLogger
.getLogger() .getLogger()
.debug("DatabaseManagement->starting the Random Sample on table operation"); .debug("DatabaseManagement->starting the Random Sample on table operation");
AnalysisLogger.getLogger().debug( AnalysisLogger.getLogger().debug(
"DatabaseManagement->retrieving 100 rows"); "DatabaseManagement->retrieving 100 rows");
@ -861,7 +798,6 @@ public class DatabaseManagement {
List<String> DataTypeColumns = getDataTypeColumns(tableName, schemaName); List<String> DataTypeColumns = getDataTypeColumns(tableName, schemaName);
if (estimatedRows == 0) { if (estimatedRows == 0) {
estimatedRows = getNumberOfRows(tableName, schemaName); estimatedRows = getNumberOfRows(tableName, schemaName);
} }
@ -878,14 +814,7 @@ public class DatabaseManagement {
AnalysisLogger.getLogger().debug( AnalysisLogger.getLogger().debug(
"In DatabaseManagement->store table in a file"); "In DatabaseManagement->store table in a file");
// store table in a file
// writeSampleTableIntoFile(resultSet, tableName, schemaName);
String FileName = pathFile + "SampleResult.csv";
// to recover columns names list // to recover columns names list
List<String> listColumns = sampler.getListColumns(); List<String> listColumns = sampler.getListColumns();
// //print check // //print check
@ -897,44 +826,23 @@ public class DatabaseManagement {
// String header = ""; // String header = "";
for (int i = 0; i < listColumns.size(); i++) { for (int i = 0; i < listColumns.size(); i++) {
if (i != listColumns.size() - 1) { if (i != listColumns.size() - 1) {
header = header + listColumns.get(i) + ", "; header = header + listColumns.get(i) + ", ";
} else { } else {
header = header + listColumns.get(i); header = header + listColumns.get(i);
} }
} }
// write the result in the file and in the map // // store table in a file
// String FileName = pathFile + "SampleResult.csv";
AnalysisLogger.getLogger().debug( // // write the result in the file and in the map
"In DatabaseManagement->writing the result in the file: " // AnalysisLogger.getLogger().debug(
+ FileName); // "In DatabaseManagement->writing the result in the file: "
// + FileName);
file = new File(FileName); // file = new File(FileName);
// out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream( // file), "UTF-8"));
file), "UTF-8")); // writeTableIntoFile(resultSet, DataTypeColumns);
// mapResult.put("HEADERS", header);
//
// out.write(header);
// out.newLine();
// writeTableIntoFile(resultSet, DataTypeColumns, header, tableName,
// schemaName,
// FileName);
// writeTableIntoFile(resultSet, DataTypeColumns, tableName, schemaName,
// FileName);
writeTableIntoFile(resultSet, DataTypeColumns);
// return resultSet;
} }
@ -943,237 +851,188 @@ public class DatabaseManagement {
// List<String> DataTypeColumns, String tableName, String schemaName, // List<String> DataTypeColumns, String tableName, String schemaName,
// String FileName) throws Exception { // String FileName) throws Exception {
// write the table result in the file and build the map of results // // write the table result in the file and build the map of results
private void writeTableIntoFile(List<Object> result, // private void writeTableIntoFile(List<Object> result,
List<String> DataTypeColumns) throws Exception { // List<String> DataTypeColumns) throws Exception {
// // file that will contain result
// BufferedWriter out;
// //
// // String fileName; // // // file that will contain result
// // BufferedWriter out;
// // // String fileName;
// // // fileName = "./cfg/" + "table.txt";
// // // fileName = "./files/" + "table.txt";
// // // fileName =
// // //
// // "/home/loredana/workspace/DatabasesResourcesManagerAlgorithms/cfg/"
// // // + "SampleOnTable.txt";
// // // fileName = "./files/" + "SmartSampleOnTable.txt";
// // // File file = new File(fileName);
// // file = new File(FileName);
// // out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
// // file), "UTF-8"));
// //
// // fileName = "./cfg/" + "table.txt"; // // to get columns names and result
// // fileName = "./files/" + "table.txt"; // // write headers in the file
// // to recover columns names
// if (header.equals("")) {
// ArrayList<String> listKeys = new ArrayList<String>(
// ((LinkedHashMap<String, Object>) (result.get(0))).keySet());
// //
// // fileName = // for (int i = 0; i < listKeys.size(); i++) {
// // // if (i != listKeys.size() - 1) {
// "/home/loredana/workspace/DatabasesResourcesManagerAlgorithms/cfg/" // header = header + listKeys.get(i) + ", ";
// // + "SampleOnTable.txt"; // } else {
// header = header + listKeys.get(i);
// }
// }
// }
// //
// // fileName = "./files/" + "SmartSampleOnTable.txt"; // // // print check
// // File file = new File(fileName); // // AnalysisLogger.getLogger().debug(
// // // "DatabaseManagement->HEADERS: " + header);
// AnalysisLogger.getLogger().debug(
// "In DatabaseManagement->writing the result in the file: "
// + FileName);
//
// file = new File(FileName);
//
// // FileWriter fw = new FileWriter(file.getAbsoluteFile());
// // out = new BufferedWriter(fw);
//
// out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
// file), "UTF-8"));
//
// // mapResult.put("HEADER", header);
// //
// out.write(header); // out.write(header);
// out.newLine(); // out.newLine();
// mapResult.put("HEADERS", header);
// to get columns names and result //
// // //print check values
// write headers in the file // // AnalysisLogger.getLogger().debug(
// // "DatabaseManagement->columns names: " + listKeys);
// to recover columns names //
// if (result != null && result.size() != 0) {
if (header.equals("")) { // // // write operation in the file
ArrayList<String> listKeys = new ArrayList<String>( // for (int i = 0; i < result.size(); i++) {
((LinkedHashMap<String, Object>) (result.get(0))).keySet()); // String RowString = "";
// Object element = result.get(i);
for (int i = 0; i < listKeys.size(); i++) { //
// // arraylist in which each element is a row result
if (i != listKeys.size() - 1) { // ArrayList<Object> listvalues = new ArrayList<Object>(
// ((LinkedHashMap<String, Object>) element).values());
header = header + listKeys.get(i) + ", "; //
// // // print check
} else { // // AnalysisLogger.getLogger().debug(
// // "DatabaseManagement->values: " + listvalues);
header = header + listKeys.get(i); //
// // each row could have several column values
} // Object[] row = listvalues.toArray();
} // if (row.length >= 1) {
// for (int j = 0; j < row.length; j++) {
} // if (row[j] == null) {
// row[j] = "";
// // print check // }
// AnalysisLogger.getLogger().debug( // // to parse the obtained results in order to align
// "DatabaseManagement->HEADERS: " + header); // // number
// // values with those of postgres
out.write(header); // String original = row[j].toString();
out.newLine(); //
// // // check value
mapResult.put("HEADERS", header); // // AnalysisLogger.getLogger().debug(
// // "In DatabaseManagement->original value: "
// //print check values // // + original);
// AnalysisLogger.getLogger().debug( //
// "DatabaseManagement->columns names: " + listKeys); // String parsed = "" + row[j];
// if (original != "") {
if (result != null && result.size() != 0) { // // convert database datatypes to Java datatypes
// if (DataTypeColumns == null
// // write operation in the file // || DataTypeColumns.size() == 0)
for (int i = 0; i < result.size(); i++) { // parsed = convertToJavaType(row[j].getClass()
// .getName(), parsed);
String RowString = ""; // else
// parsed = convertToJavaType(
Object element = result.get(i); // DataTypeColumns.get(j), parsed);
// }
//arraylist in which each element is a row result //
ArrayList<Object> listvalues = new ArrayList<Object>( // // // check value
((LinkedHashMap<String, Object>) element).values()); // // AnalysisLogger.getLogger().debug(
// // "In DatabaseManagement->row: " + (i + 1)
// // print check // // + " column: " + (j + 1) + " value= "
// AnalysisLogger.getLogger().debug( // // + parsed);
// "DatabaseManagement->values: " + listvalues); //
// // write in a file
//each row could have several column values // if (j != row.length - 1) {
Object[] row = listvalues.toArray(); // // out.write("\"" + parsed + "\"");
// // out.write(",");
if (row.length >= 1) { // out.write(parsed);
// out.write(",");
for (int j = 0; j < row.length; j++) { //
// // System.out.println("write column : " + j);
if (row[j] == null) { // // RowString = RowString + parsed + " ";
row[j] = ""; // if (j == 0) {
} // RowString = parsed;
// } else {
// to parse the obtained results in order to align // RowString = RowString + "," + parsed;
// number // }
// values with those of postgres // }
String original = row[j].toString(); // if (j == row.length - 1) {
// // out.write("\"" + parsed + "\"");
// // check value // // out.newLine();
// AnalysisLogger.getLogger().debug( // out.write(parsed);
// "In DatabaseManagement->original value: " // out.newLine();
// + original); //
// // to add a row to the map
String parsed = "" + row[j]; // if (row.length == 1) {
// RowString = parsed;
if (original != "") { // } else {
// convert database datatypes to Java datatypes // RowString = RowString + "," + parsed;
if (DataTypeColumns == null // }
|| DataTypeColumns.size() == 0) //
parsed = convertToJavaType(row[j].getClass() // // to add a row to the map
.getName(), parsed); // // RowString = RowString + "," + parsed;
else // // mapSampleTableResult.put(String.valueOf(i),
parsed = convertToJavaType( // // RowString);
DataTypeColumns.get(j), parsed); //
} // // // check value row
// // AnalysisLogger.getLogger().debug(
// // check value // // "writing the value: " + RowString + " key: "
// AnalysisLogger.getLogger().debug( // // + String.valueOf(i));
// "In DatabaseManagement->row: " + (i + 1) //
// + " column: " + (j + 1) + " value= " // // mapResult.put(Integer.valueOf(i), RowString);
// + parsed); // mapResult.put(String.valueOf(i), RowString);
// }
// write in a file // }
// }
if (j != row.length - 1) { // // else if (result.size() == 1) {
// //
// out.write("\"" + parsed + "\""); // // // Object RowElement = (Object) result.get(0);
// //
// out.write(","); // // if (row[0] == null) {
out.write(parsed); // // row[0] = "";
out.write(","); // // }
// //
// System.out.println("write column : " + j); // // // to parse the obtained results in order to align
// RowString = RowString + parsed + " "; // // // number
if (j == 0) { // // // values with those of postgres
RowString = parsed; // // String original = row[0].toString();
} else { // //
// // // // check value
RowString = RowString + "," + parsed; // // // AnalysisLogger.getLogger().debug(
} // // // "In DatabaseManagement->original value: "
// // // + original);
} // //
if (j == row.length - 1) { // // String parsed = "" + row[0];
// //
// out.write("\"" + parsed + "\""); // // if (original != "") {
// out.newLine(); // // // convert database datatypes to Java datatypes
// // if (DataTypeColumns == null
out.write(parsed); // // || DataTypeColumns.size() == 0)
out.newLine(); // // parsed = convertToJavaType(row[0].getClass()
// // .getName(), parsed);
// to add a row to the map // // else
if (row.length ==1){ // // parsed = convertToJavaType(DataTypeColumns.get(0),
RowString = parsed; // // parsed);
}else{ // // }
RowString = RowString + "," + parsed; // //
} // // out.write(row[0].toString());
// // out.newLine();
// to add a row to the map // //
// RowString = RowString + "," + parsed; // // // to add a row to the map
// mapSampleTableResult.put(String.valueOf(i), // // mapResult.put(String.valueOf(i), row[0].toString());
// RowString); // //
// // }
// // check value row // }
// AnalysisLogger.getLogger().debug( // }
// "writing the value: " + RowString + " key: " // // close the file
// + String.valueOf(i)); // out.close();
// }
// mapResult.put(Integer.valueOf(i), RowString);
mapResult.put(String.valueOf(i), RowString);
}
}
}
// else if (result.size() == 1) {
//
// // Object RowElement = (Object) result.get(0);
//
// if (row[0] == null) {
// row[0] = "";
// }
//
// // to parse the obtained results in order to align
// // number
// // values with those of postgres
// String original = row[0].toString();
//
// // // check value
// // AnalysisLogger.getLogger().debug(
// // "In DatabaseManagement->original value: "
// // + original);
//
// String parsed = "" + row[0];
//
// if (original != "") {
// // convert database datatypes to Java datatypes
// if (DataTypeColumns == null
// || DataTypeColumns.size() == 0)
// parsed = convertToJavaType(row[0].getClass()
// .getName(), parsed);
// else
// parsed = convertToJavaType(DataTypeColumns.get(0),
// parsed);
// }
//
// out.write(row[0].toString());
// out.newLine();
//
// // to add a row to the map
// mapResult.put(String.valueOf(i), row[0].toString());
//
// }
}
}
// close the file
out.close();
}
// to retrieve datatype columns of a table // to retrieve datatype columns of a table
private List<String> getDataTypeColumns(String tableName, String schemaName) private List<String> getDataTypeColumns(String tableName, String schemaName)