统计每个用户一小时内最大的登录数

package com.wz.udf


import java.util.Date

import org.apache.commons.lang3.time.FastDateFormat
import org.apache.spark.sql.SparkSession

//登录时间  登录账号
//10:31:11  aa
//10:31:20  aa
//10:31:33  aa
//10:32:10  aa
//10:37:11  aa
//10:36:20  aa
//10:35:33  aa
//10:32:53  aa
//
//10:31:11  bb
//10:31:20  cc
//10:31:33  bb
//10:32:10  dd
//10:37:11  dd
//10:36:20  cc
//10:35:33  cc
//10:32:53  cc
//
//需求:统计每个用户一小时内最大的登录数
1、将时间转成毫秒
0-3600 一个小时
/*val sql =
"""
  | select temp.name,max(temp.cn) from (
  |
  |   select  a.name, a.time , count(1) cn
  |	  from logintable a inner join logintable b
  |	    on a.name = b.name
  |	    and formatTime2ms(a.time)- formatTime2ms(b.time)<=3600000
  |	    and formatTime2ms(a.time)- formatTime2ms(b.time)>=0
  |   group by a.time,a.name
  |
  | )temp
  | group by temp.name
""".stripMargin*/
/*+----+-------+
 |name|max(cn)|
   +----+-------+
 |  cc|      2|
   |  bb|      2|
   |  dd|      2|
   |  aa|      8|
   +----+-------+*/
object demo {

  val spark: SparkSession = SparkSession.builder()
    .appName("homework02")
    .master("local[6]")
    .getOrCreate()

  spark.sparkContext.setLogLevel("ERROR")
  import spark.implicits._

  val seq_login =Seq(
    ("10:31:11","aa"),
    ("10:31:20","aa"),
    ("10:31:33","aa"),
    ("10:32:10","aa"),
    ("10:37:11","aa"),
    ("10:36:20","aa"),
    ("10:35:33","aa"),
    ("10:32:53","aa"),
    ("12:32:53","aa"),
    ("12:34:53","aa"),

    ("10:31:11","bb"),
    ("10:31:20","cc"),
    ("10:31:33","bb"),
    ("10:32:10","dd"),
    ("10:37:11","dd"),
    ("10:36:20","cc")
  )

    spark.sparkContext.parallelize(seq_login).toDF("time","name")createOrReplaceTempView("logintable")


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

    //注册udf函数
    spark.udf.register("formatTime2ms",formatTime2ms _)

    val sql =
      """
        | select temp.name,max(temp.cn) from (
        |
        |   select  a.name, a.time , count(1) cn
        |	  from logintable a inner join logintable b
        |	    on a.name = b.name
        |	    and formatTime2ms(a.time)- formatTime2ms(b.time)<=3600000
        |	    and formatTime2ms(a.time)- formatTime2ms(b.time)>=0
        |   group by a.time,a.name
        |
        | )temp
        | group by temp.name
      """.stripMargin

    spark.sql(sql).show()
  }

  /**
    * 将"HH:mm:ss" 格式时间转换毫秒值
    * @param time HH:mm:ss" 格式时间
    * @return
    */
  def formatTime2ms( time:String): Long ={
    //SimpleDateFormat是线程不安全的,解析的时候有些时间会解析错,使用FastDateFormat
    //目标日期格式
    val target_fomat: FastDateFormat =  FastDateFormat.getInstance("HH:mm:ss")
    target_fomat.parse(time).getTime
//    def parse(time:String)={
//      target_fomat.format(new Date(getTime(time)))
//    }

  }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值