You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@spark.apache.org by Tal Rozen <ta...@scaleka.com> on 2015/07/21 18:04:10 UTC

Timestamp functions for sqlContext

Hi,

I'm running a query with sql context where one of the fields is of type
java.sql.Timestamp. I'd like to set a function similar to DATEDIFF in
mysql, between the date given in each row, and now. So If I was able to use
the same syntax as in mysql it would be:

val date_diff_df = sqlContext.sql("select DATEDIFF(curdate(), rowTimestamp)
date_diff from tableName")

What are the relevant key words to replace curdate(), and DATEDIFF?

Thanks

回复:Timestamp functions for sqlContext

Posted by 鹰 <98...@qq.com>.
Hi Rozen,
you  can  get current time by call a java API and then get rowTimestamp by sql;
val currentTimeStamp=System.currentTimeMillis()val  rowTimestatm = sqlContext.sql("select  rowTimestamp  from tableName")

and then you can wirte a function like this

def diffDate(currentTimeStamp,rowTimeStamp  ):INT={

here is something you need to do;
}





------------------ 原始邮件 ------------------
发件人: "Tal Rozen";<ta...@scaleka.com>;
发送时间: 2015年7月22日(星期三) 凌晨0:04
收件人: "user"<us...@spark.apache.org>; 

主题: Timestamp functions for sqlContext



Hi, 

I'm running a query with sql context where one of the fields is of type java.sql.Timestamp. I'd like to set a function similar to DATEDIFF in mysql, between the date given in each row, and now. So If I was able to use the same syntax as in mysql it would be:


val date_diff_df = sqlContext.sql("select DATEDIFF(curdate(), rowTimestamp) date_diff from tableName")


What are the relevant key words to replace curdate(), and DATEDIFF?


Thanks

Re: Timestamp functions for sqlContext

Posted by Romi Kuntsman <ro...@totango.com>.
Hi Tal,

I'm not sure there is currently a built-in function for it, but you can
easily define a UDF (user defined function) by extending
org.apache.spark.sql.api.java.UDF1, registering it
(sparkContext.udf().register(...)), and then use it inside your query.

RK.



On Tue, Jul 21, 2015 at 7:04 PM Tal Rozen <ta...@scaleka.com> wrote:

> Hi,
>
> I'm running a query with sql context where one of the fields is of type
> java.sql.Timestamp. I'd like to set a function similar to DATEDIFF in
> mysql, between the date given in each row, and now. So If I was able to use
> the same syntax as in mysql it would be:
>
> val date_diff_df = sqlContext.sql("select DATEDIFF(curdate(),
> rowTimestamp) date_diff from tableName")
>
> What are the relevant key words to replace curdate(), and DATEDIFF?
>
> Thanks
>
>
>
>
>
>