使用schema从文件中读取时间戳

eqoofvh9  于 2021-07-14  发布在  Spark
关注(0)|答案(1)|浏览(413)

我在做一个结构化的流媒体工作。
我从文件中读取的数据包含时间戳(毫秒)、设备ID和该设备报告的值。多个设备报告数据。
我正在尝试编写一个作业,将所有设备发送的值聚合(求和)到1分钟的滚动窗口中。
我遇到的问题是时间戳。
当我试图将“timestamp”解析为long时,window函数抱怨它需要“timestamp type”。当我试图解析为timestamptype时,我得到了 .MatchError 异常(下面可以看到完整的异常),我正在努力找出原因以及正确的处理方法

// Create schema
StructType readSchema = new StructType().add("value" , "integer")
                                        .add("deviceId", "long")
                                        .add("timestamp", new TimestampType());

// Read data from file
Dataset<Row> inputDataFrame = sparkSession.readStream()
                                          .schema(readSchema)
                                          .parquet(path);

Dataset<Row> aggregations = inputDataFrame.groupBy(window(inputDataFrame.col("timestamp"), "1 minutes"),
                                                  inputDataFrame.col("deviceId"))
                                          .agg(sum("value"));

例外情况:

org.apache.spark.sql.types.TimestampType@3eeac696 (of class org.apache.spark.sql.types.TimestampType)
scala.MatchError: org.apache.spark.sql.types.TimestampType@3eeac696 (of class org.apache.spark.sql.types.TimestampType)
    at org.apache.spark.sql.catalyst.encoders.RowEncoder$.externalDataTypeFor(RowEncoder.scala:215)
    at org.apache.spark.sql.catalyst.encoders.RowEncoder$.externalDataTypeForInput(RowEncoder.scala:212)
    at org.apache.spark.sql.catalyst.expressions.objects.ValidateExternalType.<init>(objects.scala:1692)
    at org.apache.spark.sql.catalyst.encoders.RowEncoder$.$anonfun$serializerFor$3(RowEncoder.scala:175)
    at scala.collection.TraversableLike.$anonfun$flatMap$1(TraversableLike.scala:245)
    at scala.collection.IndexedSeqOptimized.foreach(IndexedSeqOptimized.scala:36)
    at scala.collection.IndexedSeqOptimized.foreach$(IndexedSeqOptimized.scala:33)
    at scala.collection.mutable.ArrayOps$ofRef.foreach(ArrayOps.scala:198)
    at scala.collection.TraversableLike.flatMap(TraversableLike.scala:245)
    at scala.collection.TraversableLike.flatMap$(TraversableLike.scala:242)
    at scala.collection.mutable.ArrayOps$ofRef.flatMap(ArrayOps.scala:198)
    at org.apache.spark.sql.catalyst.encoders.RowEncoder$.serializerFor(RowEncoder.scala:171)
    at org.apache.spark.sql.catalyst.encoders.RowEncoder$.apply(RowEncoder.scala:66)
    at org.apache.spark.sql.Dataset$.$anonfun$ofRows$1(Dataset.scala:92)
    at org.apache.spark.sql.SparkSession.withActive(SparkSession.scala:763)
    at org.apache.spark.sql.Dataset$.ofRows(Dataset.scala:89)
    at org.apache.spark.sql.streaming.DataStreamReader.load(DataStreamReader.scala:232)
    at org.apache.spark.sql.streaming.DataStreamReader.load(DataStreamReader.scala:242)
    at org.apache.spark.sql.streaming.DataStreamReader.parquet(DataStreamReader.scala:450)
omhiaaxx

omhiaaxx1#

通常,当时间戳作为 long 你可以把它转换成 timestamp 键入如下所示:

// Create schema and keep column 'timestamp' as long
StructType readSchema = new StructType()
    .add("value", "integer")
    .add("deviceId", "long")
    .add("timestamp", "long");

// Read data from file
Dataset<Row> inputDataFrame = sparkSession.readStream()
                                          .schema(readSchema)
                                          .parquet(path);

// convert timestamp column into a proper timestamp type
Dataset<Row> df1 = inputDataFrame.withColumn("new_timestamp", expr("timestamp/1000").cast(DataTypes.TimestampType));

df1.show(false)

+-----+--------+-------------+-----------------------+
|value|deviceId|timestamp    |new_timestamp          |
+-----+--------+-------------+-----------------------+
|1    |1337    |1618836775397|2021-04-19 14:52:55.397|
+-----+--------+-------------+-----------------------+

df1.printSchema();

root
 |-- value: integer (nullable = true)
 |-- deviceId: long (nullable = true)
 |-- timestamp: long (nullable = true)
 |-- new_timestamp: timestamp (nullable = true)

相关问题