【问题标题】:Cumulative count using spark structured streaming使用火花结构化流的累积计数
【发布时间】:2020-02-12 06:10:58
【问题描述】:

我想使用移动窗口计算过去 1 小时内数据框列中值的累积计数。我可以使用 rangeBetween 使用 pyspark(非流式传输)窗口函数获得预期的输出,但我想使用实时数据处理,因此尝试使用 spark 结构化流式传输,以便如果任何新记录/事务进入系统,我将获得所需的输出。

数据是这样的

time,col
2019-04-27 01:00:00,A
2019-04-27 00:01:00,A
2019-04-27 00:05:00,B
2019-04-27 01:01:00,A
2019-04-27 00:08:00,B
2019-04-27 00:03:00,A
2019-04-27 03:03:00,A

使用 pyspark(非流式传输)

from pyspark.sql.window import Window
df = sqlContext.read.format("csv") \
   .options(header='true', inferschema='false',delimiter=',') \
    .load(r'/datalocation')
df=df.withColumn("numddate",unix_timestamp(df.time, "yyyy-MM-dd HH:mm:ss"))
w1=Window.partitionBy("col").orderBy("numddate").rangeBetween(-3600, -1)
df=df.withColumn("B_cumulative_count", count("col").over(w1))

+-------------------+---+----------+------------------+
|               time|col|  numddate|B_cumulative_count|
+-------------------+---+----------+------------------+
|2019-04-27 00:05:00|  B|1556348700|                 0|
|2019-04-27 00:08:00|  B|1556348880|                 1|
|2019-04-27 00:01:00|  A|1556348460|                 0|
|2019-04-27 00:03:00|  A|1556348580|                 1|
|2019-04-27 01:00:00|  A|1556352000|                 2|
|2019-04-27 01:01:00|  A|1556352060|                 3|
|2019-04-27 03:03:00|  A|1556359380|                 0|
+-------------------+---+----------+------------------+

(This is what I required, so getting it by above code)

结构化流,这就是我正在尝试的

userSchema = StructType([
    StructField("time", TimestampType()),
    StructField("col", StringType())
])


lines2 = spark \
    .readStream \
.format('csv')\
.schema(userSchema)\
 .csv("/datalocation")

windowedCounts = lines2.groupBy(
    window(lines2.time, "1 hour"),
    lines2.col
).count()

windowedCounts.writeStream.format("memory").outputMode("complete").queryName("test2").option("truncate","false").start()

spark.table("test2").show(truncate=False)

streaming output:
+------------------------------------------+---+-----+
|window                                    |col|count|
+------------------------------------------+---+-----+
|[2019-04-27 03:00:00, 2019-04-27 04:00:00]|A  |1    |
|[2019-04-27 00:00:00, 2019-04-27 01:00:00]|A  |2    |
|[2019-04-27 01:00:00, 2019-04-27 02:00:00]|A  |2    |
|[2019-04-27 00:00:00, 2019-04-27 01:00:00]|B  |2    |
+------------------------------------------+---+-----+

如何使用 spark 结构化流复制相同的内容?

【问题讨论】:

    标签: pyspark spark-streaming spark-structured-streaming


    【解决方案1】:

    您可以按窗口幻灯片分组并计数。

    结构化流中的字数示例 -

     val lines = spark.readStream
      .format("socket")
      .option("host", host)
      .option("port", port)
      .option("includeTimestamp", true)
      .load()
    
    // Split the lines into words, retaining timestamps
    val words = lines.as[(String, Timestamp)].flatMap(line =>
      line._1.split(" ").map(word => (word, line._2))
    ).toDF("word", "timestamp")
    
    val windowDuration = "10 seconds"
    val slideDuration = "5 seconds"
    
    // Group the data by window and word and compute the count of each group
    val windowedCounts = words.groupBy(
      window($"timestamp", windowDuration, slideDuration), $"word"
    ).count().orderBy("window")
    
    // Start running the query that prints the windowed word counts to the console
        val query = windowedCounts.writeStream
          .outputMode("complete")
          .format("console")
          .option("truncate", "false")
          .start()
    
    query.awaitTermination()
    

    【讨论】:

    • 嗨,你能用我随代码提供的示例数据集获取累积计数吗?
    猜你喜欢
    • 2021-05-31
    • 1970-01-01
    • 2020-10-25
    • 2015-07-06
    • 2018-07-12
    • 1970-01-01
    • 2020-08-18
    • 2019-06-08
    • 2020-02-25
    相关资源
    最近更新 更多