如何使用scala在read csv文件中创建pivot

时间:2018-02-08 10:25:07

标签: java scala csv pivot

我遇到了问题。我将创建代码scala读取csv文件,而不是在pivot中创建结果。任何解决方案.. ??

文件csv:

Div,Person
A,Excel
A,Job
A,Bob
B,Alice
B,Anna

输出:

Div|Person
A|Excel,Job,Bob
B|Alice,Anna

我的代码:

object Tes {

  def main(args: Array[String]): Unit = {

    var result : List[Person] = Nil
    var strings = StringBuilder.newBuilder

    val bufferedSource = io.Source.fromFile("src/main/resources/config/person.csv")
    for (line <- bufferedSource.getLines) {
      val cols = line.split(",")
      val div = cols.apply(0)
      val name = cols.apply(1)

    }
    bufferedSource.close
  }

}

型号:

case class Person(div: String, person: String)

如何创建代码scala之类的输出.. ??感谢。

2 个答案:

答案 0 :(得分:1)

你可以这样做

val source = scala.io.Source.fromFile("src/main/resources/config/person.csv")

source.getLines()
  .map(_.split(","))
  .foldLeft(Map.empty[String, Seq[String]])({
    case (map, Array(div, name)) =>
      map + (div -> (map.get(div) match {
        case None => Seq(name)
        case Some(seq) => seq :+ name
      }))
  })
  .foreach({ case (div, names) => println(div + "|" + names.mkString(",")) })

source.close()

如果您想提高效率,可以在折叠过程中使用可变集合。

答案 1 :(得分:0)

Oke我将获得在模型中编写csv的完成代码。在感谢@Nils之前。

我的代码:

import java.io.{File, PrintWriter}
import scala.io.Source.fromFile

object Tes {

  //Model
  case class Person(div: String, person: String)

  def main(args: Array[String]): Unit = {

    val csv_path = "src/main/resources/config/person.csv"
    val filePath = "src/main/resources/config/result-person.csv"

    val process = pivot(csv_path)
    println(process)

    val result = saveFlattenToFile(process,filePath)

  }

  def pivot(path: String): List[Person] = {

    //Initial
    var result : List[Person] = Nil

    val source = fromFile(path)

    source.getLines().drop(1)
      .map(_.split(","))
      .foldLeft(Map.empty[String, Seq[String]])({
        case (map, Array(div, name)) =>
          map + (div -> (map.get(div) match {
            case None => Seq(name)
            case Some(seq) => seq :+ name
          }))
      })
      .foreach({ case (div, name) =>
        result = result ::: List(Person(div,name.mkString(",")))
        //println(channelName + "|" + channel.mkString(","))
      })

    source.close()
    //Output
    return result

  }

  def saveFlattenToFile(channels: List[Person], filePath: String): Boolean = {

    //Initial
    var result = false

    //Process
    if (channels.size > 0) {
      val writer = new PrintWriter(new File(filePath))
      writer.write("Div|Person\n")
      for (model <- channels) writer.write(model.div + "|" + model.person +  "\n")
      writer.close()
      result = true
    }

    //Output
    return result
  }

}