Skip to main content

Posts

Showing posts from September, 2012

Upload CSV file using opencsv with Play 2.0.2, Scala and MongoDB

Hi Everyone... Today I'm going to explain you that how we could upload a CSV file with list of products to the MongoDB table. The way I'm describing here might me inefficient but still it is useful. We know that Scala is running on JVM and so that we can use any java library with Scala . I will use OpenCSV as a CSV parser for this program. OpenCSV is a CSV parser library for Java. No matter it is for java here we will use it with Scala. First we should have MongoDB up and running. Include following opencsv dependency in your build file. I'm using SBT as building tool. "net.sf.opencsv" % "opencsv" % "2.1" Here next I will create view file. Name of this file will be 'index.scala.html'. @(message: String)(implicit request: RequestHeader, flash:Flash, l: Lang=Lang("en")) @import play.api.i18n._ @import views.html.helper._ @main(message, null) { //print messages @if(flash.data.contains("message")){ ...

CSV Parsing with Scala

Good new about Scala is, actually not only about Scala but all other JVM based languages is that they can take advantages of Java libraries. Since the Scala is a JVM based programming language its easy to use Java libraries and classes with it. I needed to parse complex CSV file with Scala and create MongoDB object to represent an each row of the CSV file. This would have been easy if I was asked to create MongoDB objects using a JSON file because the JSON is highly supported by the MongoDB. But here I only got complex CSV file with loads of data. First I found some good tutorial about Scala CSV parsing and started my work. Yes I'm using Play framework 2.0.2 with Scala and MongoDB. I found opencsv is the best solution for parsing CSV files with Scala. Opencsv is for Java but as we already know that Scala is a JVM based language we can go with it. First I updated my build configuration file to reflect the application dependencies for opencsv. add this dependencies to your ...