【发布时间】:2020-03-16 21:53:13
【问题描述】:
我的 .avro 文件中有下面的列(TriggeredDateTime),它是字符串类型,我需要以 yyyy-MM-dd HH:mm:ss 格式获取数据(如预期输出所示)使用 Spark-Scala。请让我知道是否有任何方法可以通过编写 UDF 来实现这一点,而不是使用我下面的方法。任何帮助将非常感激。
"TriggeredDateTime": {"dateTime":{"date":{"year":2019,"month":5,"day":16},"time":{"hour":4,"minute":56,"second":19,"nano":480389000}},"offset":{"totalSeconds":0}}
expected output
_ _ _ _ _ _ _ _ _ _
|TriggeredDateTime |
|___________________|
|2019-05-16 04:56:19|
|_ _ _ _ _ _ _ _ _ _|
我的方法:
我正在尝试通过应用架构将 .avro 文件转换为 JSON 格式,然后我可以尝试解析 JSON 以获得所需的结果。
DataFrame 示例数据:
[{"vin":"FU7123456XXXXX","basetime":0,"dtctime":189834,"latitude":36.341587,"longitude":140.327676,"dtcs":[{"fmi":1,"spn":2631,"dtc":"470A01","id":1},{"fmi":0,"spn":0,"dtc":"000000","id":61}],"signals":[{"timestamp":78799,"spn":174,"value":45,"name":"PT"},{"timestamp":12345,"spn":0,"value":10.2,"name":"PT"},{"timestamp":194915,"spn":0,"value":0,"name":"PT"}],"sourceEcu":"MCM","TriggeredDateTime":{"dateTime":{"date":{"year":2019,"month":5,"day":16},"time":{"hour":4,"minute":56,"second":19,"nano":480389000}},"offset":{"totalSeconds":0}}}]
DataFrame 打印架构:
initialDF.printSchema
root
|-- vin: string (nullable = true)
|-- basetime: string (nullable = true)
|-- dtctime: string (nullable = true)
|-- latitude: string (nullable = true)
|-- longitude: string (nullable = true)
|-- dtcs: string (nullable = true)
|-- signals: string (nullable = true)
|-- sourceEcu: string (nullable = true)
|-- dtcTriggeredDateTime: string (nullable = true)
【问题讨论】:
-
能否请您使用您编写的任何代码/udf/json-parser 对其进行更新。
-
从 Avro 读取数据后能否提供输入数据帧
-
@Nikk,我添加了数据帧的示例数据和 printSchema。
标签: scala apache-spark apache-spark-sql user-defined-functions