0

I am new to functional-style programming and scala, so my question might seem to be bit primitive.

Is there a specific way to read csv file in scala using functional style? Also, how the inner joins are performed for combining 2 csv files in scala using functional style?

I know spark and generally use data frame but don't have any idea in scala and finding it tough to search on google as well, since don't have much knowledge about it. Also, if anyone knows good links for the functional style programming for scala it would be great help.

ADK
  • 43
  • 4
  • Read Paul Chiusano, Rúnar Bjarnason "Functional Programming in Scala" – Emiliano Martinez Oct 28 '20 at 20:19
  • Some examples from some smart guys - https://stackoverflow.com/questions/7390322/scala-iterate-over-csv-files-in-a-functional-way – jacks Oct 28 '20 at 23:54
  • Thanks EmiCareofCell4 and jacks. – ADK Oct 30 '20 at 16:26
  • Does this answer your question? [Scala: Iterate over CSV files in a functional way?](https://stackoverflow.com/questions/7390322/scala-iterate-over-csv-files-in-a-functional-way) – tgdavies Oct 31 '20 at 12:05

1 Answers1

1

The question is indeed too broad.

Is there a specific way to read csv file in scala using functional style?

So far I don't know of a king's road to parse CSVs completely hassle-free. CSV parsing includes

  • going through the input line-by-line
  • understanding, what to do with the (optional) header
  • accurately parsing each line each line according to the CSV specification
  • turning line parts into a business object

I recommend to

  1. turn your input into Iterator[String]
  2. split each line into parts using a library of your choice (e.g. opencsv)
  3. manually create a desired domain object from the line parts

Here is an simple example which (ignores error handling and potential header)

case class Person(name: String, street: String)
val lineParser = new CSVParserBuilder().withSeparator(',').build()
val lines: Iterator[String] = Source.fromInputStream(new FileInputStream("file.csv")).getLines()
val parsedObjects: Iterator[Person] = lines.map(line => {
  val parts: Array[String] = lineParser.parseLine(line)
  Person(parts(0), parts(1))
})
simpadjo
  • 3,947
  • 1
  • 13
  • 38