As part of our project we had to convert String timestamp "25-02-2018 12:35:00TZ" to unixtimestamp. I just used the following UDFs to convert string timestamp to uniztimestamp in Scala way.

--------------------------------------------
def convertToUnixtimestamp =  udf[String, String]((eventts :String) =>  {
          Instant.parse(eventts).getEpochSecond().toString
        })

--------------------------------------------

def convertToUnixtimestamp =  udf[String, String]((eventts :String) =>  {
          val uts = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").parse(eventts).getTime().toString
          uts
        })           
--------------------------------------------                     

def timestamp = udf[Long](() =>  {

          System.currentTimeMillis

        })
---------------------------------------------
 unix_timestamp($"eventtime", "yyyy-MM-dd'T'HH:mm:ss'Z'").cast(TimestampType)   

---------------------------------------------

Post a Comment

Previous Post Next Post