flink将时间戳转为时间
在Flink中,可以使用org.apache.flink.api.common.functions.MapFunction函数将时间戳转换为时间。以下是一个示例代码:
import org.apache.flink.api.common.functions.MapFunction;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TimestampToTime implements MapFunction<Long, String> {
@Override
public String map(Long timestamp) throws Exception {
Date date = new Date(timestamp);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(date);
}
}
在上面的代码中,我们使用SimpleDateFormat将时间戳转换为指定格式的时间字符串。然后,我们可以将此函数应用于DataStream中的每个元素,以将时间戳转换为时间。以下是一个示例代码:
DataStream<Long> stream = ... // 输入流,包含时间戳
DataStream<String> timeStream = stream.map(new TimestampToTime());
在上面的代码中,timeStream是一个新的DataStream,其中包含了转换后的时间字符串
原文地址: https://www.cveoy.top/t/topic/gf32 著作权归作者所有。请勿转载和采集!