从时间戳到日期的Apache Spark行强制转换异常 [英] Apache spark row cast exception from timestamp to date

查看:59
本文介绍了从时间戳到日期的Apache Spark行强制转换异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Apache Spark中有一列带有 java.sql.timestamp 的列.当我从 DF 中提取行列时,它给我Any作为其值.

I have a column with java.sql.timestamp in Apache Spark. When I extract a row column from DF, it gives me Any as its value.

val dfv = filterDF.first().get(0);

其中 DF 是我的数据框.现在 dfv 是ANY,但我知道它是一个时间戳.如何将时间戳按原样提取到变量中?有没有办法将其转换为时间戳或日期?我使用了row的 getDate(),但它不起作用,说这是强制转换异常.

where DF is my dataframe. Now dfv is ANY but I know it's a timestamp. How can I extract timestamp as is into a variable? Is there a way to convert this any to a timestamp or a date? I used getDate() of row but it did not work, saying it's a cast exception.

推荐答案

在Java中,您可以投射对象:

In Java you could cast the object:

((Timestamp)row.get(0))

在Scala中,您可以调用asInstanceOf:

In Scala you can call asInstanceOf:

row.get(0).asInstanceOf[Timestamp]

这篇关于从时间戳到日期的Apache Spark行强制转换异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆