MaatDeamon
MaatDeamon

Reputation: 9771

How to write CSV file with headers using akka stream alpakka?

I can't see to find it, hence i turn to slack to ask: Is there a way to write a csv file with its heards using akka stream alpakka ?

The only thing i see is https://doc.akka.io/docs/alpakka/current/data-transformations/csv.html#csv-formatting

But no reverse operation to csv to map somehow.

My use case is that i need to read few csv files, filter their content, and write the clean content in a corresponding file orginalcsvfilename-cleanded.csv.

If it is not directly supported, any recommendation ?

Upvotes: 1

Views: 802

Answers (1)

ACO
ACO

Reputation: 295

You can do something like that

def csv_header(elem:T):List[String] = ???
def csv_line(elem:T):List[String] = ???

def firstTrueIterator(): Iterator[Boolean] = (Iterator single true) ++ (Iterator continually false)

def firstTrueSource: Source[Boolean, _] = Source fromIterator firstTrueIterator

def processData(elem: T, firstRun: Boolean): List[List[String]] = {
      if (firstRun) {
        List(
          csv_header(elem),
          csv_line(elem)
        )
      } else {
        List(csv_line(elem))
      }
    }

val finalSource = source
  .zipWith(firstTrueSource)(processData)
  .mapConcat(identity)
  .via(CsvFormatting.format()) 

Upvotes: 1

Related Questions