- toUnixTimestamp
- Convert date in string to time stamp while reserving milliseconds.
- Option 1
importorg.apache.spark.sql.functions.udfimportjava.text.SimpleDateFormatimportjava.util.TimeZonedeftoUnixTimestamp(format:String, timeZone:String)=udf((date:String)=> {valdateFormat=newSimpleDateFormat(format)dateFormat.setTimeZone(TimeZone.getTimeZone(timeZone))dateFormat.parse(date).getTime})valtest=spark.sql("select charge_datetime from temp.data_sql_server limit 3")test.select(test("charge_datetime"), toUnixTimestamp("yyyy-MM-dd HH:mm:ss.SSS","UTC")(test("charge_datetime")).alias("i_regdatetime")).show(false) - Option 2
importjava.text.SimpleDateFormatimportjava.util.TimeZonespark.sqlContext.udf.register("toUnixTimestamp", (date:String, format:String, timeZone:String)=> {valdateFormat=newSimpleDateFormat(format)dateFormat.setTimeZone(TimeZone.getTimeZone(timeZone))dateFormat.parse(date).getTime})spark.sql("select toUnixTimestamp('2017-03-13 15:40:53.147', 'yyyy-MM-dd HH:mm:ss.SSS', 'UTC')").show(false)
- fromUnixTimestamp
- Convert time stamp to date in string while reserving milliseconds.
importjava.text.SimpleDateFormatimportjava.util.TimeZonespark.sqlContext.udf.register("fromUnixTimestamp", (timestamp:Double, format:String, timeZone:String)=> {valdateFormat=newSimpleDateFormat(format)dateFormat.setTimeZone(TimeZone.getTimeZone(timeZone))dateFormat.format(timestamp)})spark.sql("select fromUnixTimestamp(1489419653147, 'yyyy-MM-dd HH:mm:ss.SSS', 'UTC')").show(false)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.