简体   繁体   中英

Flink scala Case class

I want to know how to replace x._1._2, x._1._3 by the name of the field using case class

def keyuid(l:Array[String]) : (String,Long,String) ={
  //val l=s.split(",")
  val ip=l(3).split(":")(1)
  val values=Array("",0,0,0)
  val uid=l(1).split(":")(1)
  val timestamp=l(2).split(":")(1).toLong*1000
  val impression=l(4).split(":")(1)
  return (uid,timestamp,ip)
}

val cli_ip = click.map(_.split(","))
  .map(x => (keyuid(x), 1.0)).assignAscendingTimestamps(x=>x._1._2)
  .keyBy(x => x._1._3)
  .timeWindow(Time.seconds(10))
  .sum(1)

Use Scala pattern matching when writing lambda functions using curly braces and case keyword.

val cli_ip = click.map(_.split(","))
  .map(x => (keyuid(x), 1.0)).assignAscendingTimestamps { 
    case ((_, timestamp, _), _) => timestamp 
  }
  .keyBy { elem => elem match {
      case ((_, _, ip), _) => ip
    }
  }
  .timeWindow(Time.seconds(10))
  .sum(1)

More information on Tuples and their pattern matching syntax here: https://docs.scala-lang.org/tour/tuples.html

Pattern Matching is indeed a good idea and makes the code more readable. To answer your question, to make keyuid function returns a case class, you first need to define it, for instance:

case class Click(string uid, long timestamp, string ip)

Then instead of return (uid,timestamp,ip) you need to do return Click(uid,timestamp,ip)

Case class are not related to flink, but to scala: https://docs.scala-lang.org/tour/case-classes.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM