java epoch times_Converting Timestamp to epoch in Spark (Java)

I think you are looking at using: unix_timestamp()

Which you can import from:

import static org.apache.spark.sql.functions.unix_timestamp;

And use like:

df = df.withColumn(

"epoch",

unix_timestamp(col("date")));

And here is a full example, where I tried to mimic your use-case:

package net.jgp.books.spark.ch12.lab990_others;

import static org.apache.spark.sql.functions.col;

import static org.apache.spark.sql.functions.from_unixtime;

import static org.apache.spark.sql.functions.unix_timestamp;

import java.util.ArrayList;

import java.util.List;

import java.util.Random;

import org.apache.spark.sql.Dataset;

import org.apache.spark.sql.Row;

import org.apache.spark.sql.RowFactory;

import org.apache.spark.sql.SparkSession;

import org.apache.spark.sql.types.DataTypes;

import org.apache.spark.sql.types.StructField;

import org.apache.spark.sql.types.StructType;

/**

* Use of from_unixtime() and unix_timestamp().

*

* @author jgp

*/

public class EpochTimestampConversionApp {

/**

* main() is your entry point to the application.

*

* @param args

* @throws InterruptedException

*/

public static void main(String[] args) {

EpochTimestampConversionApp app = new EpochTimestampConversionApp();

app.start();

}

/**

* The processing code.

*/

private void start() {

// Creates a session on a local master

SparkSession spark = SparkSession.builder()

.appName("expr()")

.master("local")

.getOrCreate();

StructType schema = DataTypes.createStructType(new StructField[] {

DataTypes.createStructField(

"event",

DataTypes.IntegerType,

false),

DataTypes.createStructField(

"original_ts",

DataTypes.StringType,

false) });

// Building a df with a sequence of chronological timestamps

List rows = new ArrayList<>();

long now = System.currentTimeMillis() / 1000;

for (int i = 0; i < 1000; i++) {

rows.add(RowFactory.create(i, String.valueOf(now)));

now += new Random().nextInt(3) + 1;

}

Dataset df = spark.createDataFrame(rows, schema);

df.show();

df.printSchema();

// Turning the timestamps to Timestamp datatype

df = df.withColumn(

"date",

from_unixtime(col("original_ts")).cast(DataTypes.TimestampType));

df.show();

df.printSchema();

// Turning back the timestamps to epoch

df = df.withColumn(

"epoch",

unix_timestamp(col("date")));

df.show();

df.printSchema();

// Collecting the result and printing ou

List timeRows = df.collectAsList();

for (Row r : timeRows) {

System.out.printf("[%d] : %s (%s)n",

r.getInt(0),

r.getAs("epoch"),

r.getAs("date"));

}

}

}

And the output is:

...

[994] : 1551997326 (2019-03-07 14:22:06)

[995] : 1551997329 (2019-03-07 14:22:09)

[996] : 1551997330 (2019-03-07 14:22:10)

[997] : 1551997332 (2019-03-07 14:22:12)

[998] : 1551997333 (2019-03-07 14:22:13)

[999] : 1551997335 (2019-03-07 14:22:15)

Hopefully this helps.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值