下面这张表,使用scala函数完成以下任务
任务:每个用户截止到每月为止的最大单月访问次数和累计到该月的总访问次数。
思路:
scala> import scala.io.Source
import scala.io.Source
scala> val lines = Source.fromFile("本地文件路径").getLines().toArray
lines: Array[String] = Array(
A,2015-01,5, A,2015-01,15, B,2015-01,5,
A,2015-01,8, B,2015-01,25, A,2015-01,5,
A,2015-02,4, A,2015-02,6, B,2015-02,10,
B,2015-02,5, A,2015-03,16, A,2015-03,22,
B,2015-03,23, B,2015-03,10, B,2015-03,11)
scala> lines.map(x=>{
| var y=x.split(",")
| (y(0),y(1),y(2).toInt)
| })
res0: Array[(String, String, Int)] = Array(
(A,2015-01,5), (A,2015-01,15), (B,2015-01,5),
(A,2015-01,8), (B,2015-01,25), (A,2015-01,5),
(A,2015-02,4), (A,2015-02,6), (B,2015-02,10),
(B,2015-02,5), (A,2015-03,16), (A,2015-03,22),
(B,2015-03,23), (B,2015-03,10), (B,2015-03,11))
scala> lines.map(x=>{
| var y=x.split(",")
| (y(0),y(1),y(2).toInt)
| }).groupBy(x=>x._1)
res1: scala.collection.immutable.Map[String,Array[(String, String, Int)]] = Map(
A -> Array((A,2015-01,5), (A,2015-01,15), (A,2015-01,8), (A,2015-01,5), (A,2015-02,4), (A,2015-02,6), (A,2015-03,16), (A,2015-03,22)),
B -> Array((B,2015-01,5), (B,2015-01,25), (B,2015-02,10), (B,2015-02,5), (B,2015-03,23), (B,2015-03,10), (B,2015-03,11)))
scala> lines.map(x=>{
| var y=x.split(",")
| (y(0),y(1),y(2).toInt)
| }).groupBy(x=>x._1).mapValues(x=>x.groupBy(x=>x._2).
| toArray.sortWith((x,y)=>x._1<y._1).
| map(x=>(x._1,x._2.map(x=>x._3).sum)))
res2: scala.collection.immutable.Map[String,Array[(String, Int)]] = Map(
A -> Array((2015-01,33), (2015-02,10), (2015-03,38)),
B -> Array((2015-01,30), (2015-02,15), (2015-03,44)))
scala> lines.map(x=>{
| var y=x.split(",")
| (y(0),y(1),y(2).toInt)
| }).groupBy(x=>x._1).mapValues(x=>x.groupBy(x=>x._2).
| toArray.sortWith((x,y)=>x._1<y._1).
| map(x=>(x._1,x._2.map(x=>x._3).sum))).
| toArray.map(x=>(x._1,x._2.map(x=>x._1).
| zip(x._2.map(x=>x._2).scan(0)(_+_).tail).
| zip(x._2.map(x=>x._2).scan(0)(_.max(_)).tail)))
res3: Array[(String, Array[((String, Int), Int)])] = Array(
(A,Array(((2015-01,33),33), ((2015-02,43),33), ((2015-03,81),38))),
(B,Array(((2015-01,30),30), ((2015-02,45),30), ((2015-03,89),44))))
import scala.io.Source
object pv{
def main(args: Array[String]): Unit = {
val lines = Source.fromFile("files/03pv/pv.txt").getLines().toArray
lines.map(x=>{
var y=x.split(",")
(y(0),y(1),y(2).toInt)
}).groupBy(x=>x._1).mapValues(x=>x.groupBy(x=>x._2).
toArray.sortWith((x,y)=>x._1<y._1).
map(x=>(x._1,x._2.map(x=>x._3).sum))).
toArray.map(x=>(x._1,x._2.map(x=>x._1).
zip(x._2.map(x=>x._2).scan(0)(_+_).tail).
zip(x._2.map(x=>x._2).scan(0)(_.max(_)).tail))).
foreach(x=>{
x._2.foreach(y=> println(
s"用户:${x._1},日期:${y._1._1},至今为止的最高访问次数:${y._2},到本月的总访问次数:${y._1._2}"))
})
}
}