import
au.com.bytecode.opencsv.CSVWriter import
java.time.format.DateTimeFormatter import
org.apache.commons.io.FilenameUtils import java.io.FileWriter object CSVExport { def exportCSVFile (oracleUser:
String,oraclePassword: String,oracleURL:
String,oracleOutPutFilePath:String,oracleOutPutFileDateFormat:String,oracleQueryFilePath:String):
Unit = { /* Read date part from the date pattern*/ val dateNow: String = DateTimeFormatter.ofPattern(oracleOutPutFileDateFormat).format(java.time.LocalDate.now) /* Reconstruct the new file name*/ val basename =
FilenameUtils.getBaseName(oracleOutPutFilePath) val extension =
FilenameUtils.getExtension(oracleOutPutFilePath) val path =
FilenameUtils.getFullPath(oracleOutPutFilePath)
//println(path,basename,dateNow,extension) val newFileName = path + "\\" +
basename + "_" + dateNow + "." + extension //println(newFileName) /* Read the query from the file*/ val queryString =
scala.io.Source.fromFile(oracleQueryFilePath).mkString //print(queryString) /* Connect to Oracle DB and run the query
and save to results set*/ val conExport =
OracleConnect.connJdbc(oracleUser, oraclePassword, oracleURL) val statementExport =
conExport.createStatement() statementExport.setFetchSize(1000) val resultSet: java.sql.ResultSet =
statementExport.executeQuery(queryString) /* Write the result set to CSV file*/ val csvWriter = new CSVWriter(new
FileWriter(newFileName), ',') csvWriter.writeAll(resultSet, true) csvWriter.close() } } |
Hi All,
Below is the code which can be created as csv.scala file and can be called inside your MAIN Scala function to push the data from Data Frame to CSV files.
your valuable information and time. Please keep updating.
ReplyDeleteMsbi Developer Course
Best Msbi Online Training