如何使用akka流alpakka编写带有标题的CSV文件? [英] How to write CSV file with headers using akka stream alpakka?

查看:61
本文介绍了如何使用akka流alpakka编写带有标题的CSV文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看不到找到它,因此我转向懈怠地问:是否有一种方法可以使用akka流alpakka编写带有其听觉的csv文件?

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 ?

我唯一看到的是 https://doc.akka.io/docs/alpakka/current/data-transformations/csv.html#csv-formatting

但是没有反向操作以某种方式映射到csv.

But no reverse operation to csv to map somehow.

我的用例是,我需要读取几个csv文件,过滤它们的内容,并将干净的内容写入相应的文件orginalcsvfilename-cleanded.csv中.

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 ?

推荐答案

您可以执行类似的操作

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()) 

这篇关于如何使用akka流alpakka编写带有标题的CSV文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆