等效于Flink-SQL中的“ from_unixtime(bigint unixtime)”

时间:2018-09-06 15:43:55

标签: apache-flink flink-sql

我正在搜索与功能 from_unixtime(bigint unixtime)等效的功能,该功能存在于Spark-SQL和Flink-SQL中。

我的目标是转换这种格式:1439799094

转换为这种格式:2015-05-18 05:43:37

2 个答案:

答案 0 :(得分:1)

只需使用UDF!

https://ci.apache.org/projects/flink/flink-docs-stable/dev/table/udfs.html

样品用量

add_filter( 'woocommerce_default_address_fields' ,'custom_override_default_address_fields' ); $answer = $_POST['payment_method']; if ( $answer == "cppec_paypal" ) { function custom_override_default_address_fields( $address_fields ) { $address_fields['address_1']['required'] = false; return $address_fields; } ...

test.csv

creation_date|key 1535816823|1 1536392928|2 1536272308|3

EpochTimeConverter.scala

import java.time.format.DateTimeFormatter import java.time.{Instant, LocalDateTime, ZoneId} import org.apache.flink.table.functions.ScalarFunction class EpochTimeConverter extends ScalarFunction { def eval(epochTime: Int): String = { // For performance, you may cache `DateTimeFormatter` in real life val timePattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") timePattern.format(LocalDateTime.ofInstant(Instant.ofEpochSecond(epochTime), ZoneId.systemDefault())) } }

UdfExample.scala

如果运行import org.apache.flink.api.scala.{ExecutionEnvironment, _} import org.apache.flink.table.api.scala._ import org.apache.flink.table.api.{TableEnvironment, Types} import org.apache.flink.table.sources.CsvTableSource import org.apache.flink.types.Row object UdfExample { def main(args: Array[String]): Unit = { val env = ExecutionEnvironment.getExecutionEnvironment val tableEnv = TableEnvironment.getTableEnvironment(env) val csvData = CsvTableSource .builder() .path("test.csv") .ignoreFirstLine() .fieldDelimiter("|") .field("creation_date", Types.INT) .field("key", Types.INT) .build() tableEnv.registerTableSource("temp_table", csvData) println("Without udf:") tableEnv.sqlQuery("SELECT creation_date, key FROM temp_table").toDataSet[Row].print() tableEnv.registerFunction("from_unixtime", new EpochTimeConverter()) println() println("With udf:") tableEnv.sqlQuery("select from_unixtime(creation_date),key from temp_table").toDataSet[Row].print() } } ,它将产生类似以下的输出:

UdfExample.scala

答案 1 :(得分:-1)

我认为您正在寻找DATE_FORMAT(timestamp, '%Y-%m-%d %H:%i:%s')

有关更多信息,请参见built-in functionsdate format specifiers上的文档。