2

Aim is to create a groovy script that will read from the database and export to a csv file. I am using the apache commons csv writer. I have been working with this javacode geeks - apache commons tutorial..

This works I have put here to help explain what I am trying to achieve.

fileWriter = new FileWriter(fileName);
csvFilePrinter = new CSVPrinter(fileWriter, csvFileFormat);
csvFilePrinter.printRecord(FILE_HEADER);

//Write a new student object list to the CSV file
for (Student student : students) {
   List studentDataRecord = new ArrayList();
   studentDataRecord.add(String.valueOf(student.getId()));
   studentDataRecord.add(student.getFirstName());
   studentDataRecord.add(student.getLastName());

   csvFilePrinter.printRecord(studentDataRecord);
}

What Im trying to do In groovy/java.

I have created a groovy script calls the DB ok. I can make it iterate and loop over fine and prints the rows. However I dont want to manually write all the 'student.getFirstName' student.getLastName etc calls. If I modify the query in the future it makes more work -- I just want to export it straight to csv having only modified the query.

Something like this is what I have so far.. I am unsure of the syntax etc.

#!/usr/bin/env groovy
import groovy.sql.Sql
import org.apache.commons.csv.CSVFormat

CSVFormat csvFileFormat = CSVFormat.DEFAULT.withRecordSeparator(NEW_LINE_SEPARATOR);
fileWriter = new FileWriter(fileName);
csvFilePrinter = new CSVPrinter(fileWriter, csvFileFormat);
csvFilePrinter.printRecord(FILE_HEADER);

sql.eachRow("Select field1, field2, field3, etc FROM Student") {
    for( keyIndex  k ){
        List studentDataRecord = new ArrayList();
        studentDataRecord.add(k.value)
    }
    csvFilePrinter.printRecord(studentDataRecord);
}
0

1 Answer 1

3

If you are iterating with eachRow, each row is a groovy.sql.GroovyResultSet (strictly speaking, a groovy.sql.GroovyResultSetProxy). Since it does not implement Map interface, you cannot use collect, etc.

Therefore, there are several options:

Get each field by name (the one you want to avoid)

sql.eachRow("select field1, field2, field3, etc from student") { row ->
   csvFilePrinter.printRecord([row.field1, row.field2, row.field3, ...])
}

Get each field by number (I guess you don't like this either)

sql.eachRow("select field1, field2, field3, etc from student") { row ->
   csvFilePrinter.printRecord([row[0], row[1], row[2], ...])
}

Use the underlying ResultSet: get the number of colums and then build a List

sql.eachRow("select field1, field2, field3, etc from student") { row ->
    def record = []
    row.getMetaData().columnCount.times { record << row[it] }
    csvFilePrinter.printRecord(record)
}

Use the rows() method, that returns a List<GroovyRowResult> where GroovyRowResult implements Map and then you can apply collect:

def rows = sql.rows('select * from student').each { row ->
    csvFilePrinter.printRecord(row.collect { entry -> entry.value })
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.