Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,300,565
Community Members
 
Community Events
165
Community Groups

How to make changes in the script so that it can read data from csv file using csv driver

I have referred this document and installed csv driver and gave connection to csv driver.

Resources (adaptavist.com)

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.

 

 

2 answers

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...

0 votes

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/

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira Service Management

Improving the Create Issue Experience in Jira Service Management Cloud

Hello everyone!  We are very excited to announce some much needed changes to the issue create experience in JSM (the blue "create" button) at the top of the screen.  We have just starte...

179 views 7 5
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you