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.

4

1 Answer 1

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))
})
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, sorry for the broad question. But how do functional style works in case of reading CSV file in scala? Is there any example for it to refer?
Ok, I added an example

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.