You're on your way to the next level! Join the Kudos program to earn points and save your progress.
Level 1: Seed
25 / 150 points
Next: Root
1 badge earned
Challenges come and go, but your rewards stay with you. Do more to earn more!
What goes around comes around! Share the love by gifting kudos to your peers.
Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!
Join now to unlock these features and more
The Atlassian Community can help you and your team get more value out of Atlassian products and practices.
I have referred this document and installed csv driver and gave connection to csv driver.
Previously we have used csv files and to read the data from csv files we have used the following code.
new File("path of the csv file").splitEachLine(",") {
fields ->//fields[0]-->extracted first column from the csv file
}
Now we want to use csv driver database to fetch the data from the csv file. I want to know how to modify the above code to fetch the columns of the csv file and what all the libraries I have to import for the csv driver.
Hi @Sanjana Nalam , welcome on the community. Do you really need to access the CSV as DB tables? I think it is valid for complicated CSV files and I never used it to be honest.
We use following library to read CSV files in groovy:
https://github.com/xlson/groovycsv
It is really simple to use it...
I like opencsv for this:
Here is a simple implementation
import com.opencsv.CSVReader
@Grapes(
@Grab(group='com.opencsv', module='opencsv', version='5.5.2')
)
def reader = new CSVReader(new FileReader(new File('path to csv')))
def csvAsArrayOfMaps = reader.collect { it }.with { rows ->
def header = rows.head()
def dataRows = rows.tail()
dataRows.collect { row ->
[header, row].transpose().collectEntries()
}
}
This this, if you file contains something like this:
colA,colB,colC
1,value1b,value1c
2,value2b,value2c
You can get the total number of rows with "csvAsArrayOfMaps.size()"
And you can get any of the rows with "csvAsArrayOfMaps[rowIndex]"
And you can get a specific column with csvAsArrayOfMaps[0].colB == 'value1b'
There are other fancier methods like "CsvToBeanBuilder" and CSVReaderHeaderAware, but I found the simplest case to be the easier to use.
More info available here: http://opencsv.sourceforge.net/
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.