首页 > 其他分享 >spark中生成时间序列数据的函数stack和sequence

spark中生成时间序列数据的函数stack和sequence

时间:2022-11-20 23:56:51浏览次数:38  
标签:11 00 01 sequence 2022 spark stack select

Sequence函数

用Sequence函数生成时间序列函数,真的是非常简便易用,之前因为没找到,所以走了不少弯路。

    println("指定开始和结束数字,生成对应的数字序列,通过第三个参数来控制步长")
    SparkUtil.executeSQL("""
                         |select explode(sequence(1,10,2)) id
                         |""".stripMargin)(spark)

image

    println("指定开始和结束日期,生成对应的日期序列,默认递增1天")
    SparkUtil.executeSQL("""
                           |select explode(sequence(to_date('2022-11-01','yyyy-MM-dd'),to_date('2022-11-10','yyyy-MM-dd'))) dt
                           |""".stripMargin)(spark)

image

    println("指定开始和结束日期,生成对应的日期序列,指定递增间隔天数")
    SparkUtil.executeSQL("""
                           |select explode(sequence(to_date('2022-11-01','yyyy-MM-dd'),to_date('2022-11-10','yyyy-MM-dd'),interval 2 day)) dt
                           |""".stripMargin)(spark)

image

    println("指定开始和结束小时日期,生成小时级别的时间序列(小时级别需要转成时间戳)")
    SparkUtil.executeSQL("""
                           |select explode(sequence(to_timestamp('2022-11-01 01:00:00','yyyy-MM-dd HH:mm:ss'),to_timestamp('2022-11-04 01:00:00','yyyy-MM-dd HH:mm:ss'),interval 1 hours)) dt
                           |""".stripMargin,100)(spark)

image

    println("指定开始和结束小时时间戳,生成小时级别的时间序列,增量直接指定毫秒")
    println("开始毫秒时间戳 1667235600000 -> 2022-11-01 01:00:00")
    println("结束毫秒时间戳 1667581200000 -> 2022-11-05 01:00:00")
    SparkUtil.executeSQL("""
                           |select explode(sequence(1667235600000,1667581200000,3600000)) dt
                           |""".stripMargin,100)(spark)

image

    SparkUtil.executeSQL("""
                    |
                    |with tmp_a as (
                    | select '2022-11-01' as id,'01,02,03' as infos
                    | union all
                    | select '2022-11-02' as id,'04,05,06' as infos
                    |)
                    |select id,info from tmp_a LATERAL VIEW explode(split(infos,',')) t AS info
                    |""".stripMargin)(spark)

image

在spark中向前填充或向后填充的案例,这个其实就是先排下,然后用last或者first取值去替换。
其中last(timeField, true)第二个参数,是是否忽略空值。

    val window = Window.partitionBy(idField).orderBy(timeField).rowsBetween(-1, 0)
    val filled = last(timeField, true).over(window)
    outputDF = outputDF.selectExpr(field1: _*).withColumn(rule.getField, filled)
    //后向填充
    val window = Window.partitionBy(idField).orderBy(timeField).rowsBetween(0, 1)
    val filled = last(timeField, true).over(window)
    outputDF = outputDF.withColumn(rule.getField, filled)

标签:11,00,01,sequence,2022,spark,stack,select
From: https://www.cnblogs.com/30go/p/16910069.html

相关文章

  • spark (六) RDD算子(operator)
    目录1转换算子(transformer)(将旧的RDD包装成新RDD)1.1单值类型1.1.1map1.1.2mapPartition1.1.3mapPartitionsWithIndex1.1.4flatMap1.1.5glom1.1.6groupBy1.1.7f......
  • spark (五) RDD的创建 & 分区
    目录1.RDD的创建方式1.1从内存创建RDD1.2从外部存储(文件)创建RDD1.3从其他的RDD创建1.4直接newRDD2.分区(partition)2.1makeRDD的分区2.2读取文件的分区例子2.2.1......
  • 128. Longest Consecutive Sequence
    Givenanunsortedarrayofintegers,findthelengthofthelongestconsecutiveelementssequence.Forexample,Given [100,4,200,1,3,2],Thelongestconsec......
  • B - Bracket Sequence题解
    B-BracketSequence思路:用一个flag来标记括号的数目,如果括号数目是个偶数的话,就代表当前要执行'+'操作,反之就是'*'操作。对于最外层的数,是没有计算的。所以最后要单独......
  • [LeetCode] 891. Sum of Subsequence Widths
    Thewidthofasequenceisthedifferencebetweenthemaximumandminimumelementsinthesequence.Givenanarrayofintegersnums,returnthesumofthewi......
  • IDEA提交任务到spark standalone集群
    参考文章:在idea里面怎么远程提交spark任务到yarn集群代码注意setJars,提交的代码,要提前打好包。否则会报找不到类的错误个人理解就相当于运行的main方法是起了一个spar......
  • Flink/Spark中ETL的简单模版
    我们往往可以忽略外界的干扰因素,避免焦虑,专心做自己想做的事情,反正焦虑又解决不了问题引言使用flink或者spark的时候,写好固定的模版很重要,对于一下etl的实时任务,只需要执行......
  • 【Python错误】TypeError: sequence item 0: expected str instance, int found【列表
    【错误类型】TypeError:sequenceitem0:expectedstrinstance,intfound前景提要:获得用户输入的以逗号分隔的三个数字,记为a、b、c,以a为起始值,b为前后相邻数的比值,c为......
  • dp好题CF1183H Subsequences (hard version)
    CF1183HSubsequences(hardversion)考虑dp计算本质不同方案数dp[i][j]表示在前i个字符中,长度为j的本质不同的子串数跑pre[i]表示de字母出现的上一个位置pre数组我属......
  • Debug - Method threw 'java.lang.StackOverflowError' exception
     一、问题背景报错信息:Methodthrew'java.lang.StackOverflowError'exception.Cannotevaluatecom.huatai.nats.model.quant.basic.ComparableSecurityMonitoring.......