ScalaでCSVファイルからIPアドレスを取り出してMapに格納する

下記のようなデータから、timestamp, sourceIP, destIPのファイルを作りたいとする。。

（IPアドレスはランダムに生成）

     1"2019/07/02 02:02:00.839","2019/07/02 02:02:00","2019/07/02 02:02:00","841","255.178.167.132","25846","iy","69.215.218.225","51321","bu","8MP","VX13Gpdkt","drN","deETa","gyAY4gdZ","\
      8","TJoPPQuOKvrxzAjCd11rpqqSqs","912","198","336","769","278","554","rand-pa1"
     2"2019/07/02 02:02:52.006","2019/07/02 02:02:52","2019/07/02 02:02:52","478","104.40.197.93","41214","ol","76.213.36.241","23907","Vu","OYF","H6zQlnN5X","yV3","P2VPw","9D3viFsS","5",\
      "cogAhSIycmvdYl7RaZNjGCsWqj","953","917","636","718","142","607","rand-pa1"
     3"2019/07/02 12:12:16.086","2019/07/02 12:12:16","2019/07/02 12:12:16","17","120.175.225.23","15918","Hk","55.156.36.246","8994","u1","Tt2","hihIKl4xd","OX5","uj8uP","hKwtE4iF","8","\
      9EuzqcBTUrBjAago2vY5MMugKb","157","401","130","109","999","219","rand-pa1"


↓こちらのサイトを参考にする。。。
https://symfoware.blog.fc2.com/blog-entry-1029.html

     8def csv_format(line : String, out : PrintWriter) = {
     9    val list = line split ','
    10
    11    val timestamp = dropQuote(list(0))
    12    val address = dropQuote(list(4)) + "," + dropQuote(list(7))
    13
    14    map = map + (timestamp -> address)
    15
    16    out.write("%s,%s\n" format (timestamp, address))
    17    print("%s,%s\n" format (timestamp, address))
    18}

このような感じで、０列目、４列目、７列目の項目が取り出せる。

Mapを用意する。

     6var map: Map[String, String] = Map()

14行目で、timestamp, {sourceIP, destIP} の組をmapに追加する。

    14    map = map + (timestamp -> address)

Mapの要素を列挙するにはこのようにする。

    39  map.foreach { e =>        
    40             println(e._1 + "->" + "{" + e._2 + "}")
    41             }

コードを見てみる。。

     1import scala.io.Source
     2import java.io.PrintWriter
     3
     4object csv {
     5
     6var map: Map[String, String] = Map()
     7
     8def csv_format(line : String, out : PrintWriter) = {
     9    val list = line split ','
    10
    11    val timestamp = dropQuote(list(0))
    12    val address = dropQuote(list(4)) + "," + dropQuote(list(7))
    13
    14    map = map + (timestamp -> address)
    15
    16    out.write("%s,%s\n" format (timestamp, address))
    17    print("%s,%s\n" format (timestamp, address))
    18}
    19
    20// 前後のダブルクォーテーションを除去    21def dropQuote(str : String) : String = {
    22    str drop 1 dropRight 1
    23}
    24
    25  def main(args: Array[String]): Unit = {
    26
    27  // 入力ファイル
    28  // val source = Source.fromFile("random_data.txt", "ms932")
    29  val source = Source.fromFile(args(0), "ms932")
    30  // 出力ファイル
    31  val out = new PrintWriter("random_data_output.txt", "utf8")
    32
    33  // ファイルを読み込んでループ
    34  val lines = source.getLines
    35  lines.foreach(line => csv_format(line, out))
    36  source.close               
    37  out.close                  
    38                             
    39  map.foreach { e =>     
    40             println(e._1 + "->" + "{" + e._2 + "}")
    41             }
    42  }
    43}

https://github.com/RuoAndo/qiita/blob/master/csv.scala

実行してみる。。。

<pre>
$ scala csv.scala random_data.txt 
2019/07/02 02:02:00.839,255.178.167.132,69.215.218.225
2019/07/02 02:02:52.006,104.40.197.93,76.213.36.241
2019/07/02 12:12:16.086,120.175.225.23,55.156.36.246
2019/07/02 02:02:00.839->{255.178.167.132,69.215.218.225}
2019/07/02 02:02:52.006->{104.40.197.93,76.213.36.241}
2019/07/02 12:12:16.086->{120.175.225.23,55.156.36.246}
</pre>

（｀ー´）b