简体   繁体   English

Apache Spark function to_timestamp() 无法在 Databricks 上使用 PySpark

[英]Apache Spark function to_timestamp() not working with PySpark on Databricks

I'm getting NULL output when I execute the code to_timestamp()当我执行代码 to_timestamp() 时,我得到 NULL output

The code that I'm executing is as follows:我正在执行的代码如下:

.withColumn("LAST_MODIFICATION_DT", to_timestamp(concat(col('LAST_MOD_DATE'), lit(' '), col('LAST_MOD_TIME')), 'yyy-MM-dd HH:mm:ss'))

The schema for the fields LAST_MOD_DATE & LAST_MOD_TIME is as follows: LAST_MOD_DATE 和 LAST_MOD_TIME 字段的架构如下: 在此处输入图像描述

I'm getting the output 'NULL' for the column 'LAST_MODIFICATION_DT'我得到列“LAST_MODIFICATION_DT”的 output“NULL”

Any thoughts?有什么想法吗?

In Spark SQL concat doesn't convert null to '' ;在 Spark SQL 中, concat不会将 null 转换为'' any null argument will cascade into a null result.任何 null 参数都会级联成 null 结果。 It's often easier to write these kind of expressions in python and register them as UDFs, eg在 python 中编写这些类型的表达式并将它们注册为 UDF 通常更容易,例如

from pyspark.sql.types import StringType

def concat2_(s1, s2) -> str:
  return str(s1) + ' ' + str(s2)

concat2 = spark.udf.register("concat2", concat2_, StringType())

Then you can use it in Spark queries in built in python,然后你可以在 python 内置的 Spark 查询中使用它,

from pyspark.sql.functions import col
df = spark.sql('select 1 a, 2 b').withColumn("c",concat2(col('a'),col('b')))
display(df)

or SQL或 SQL

%sql
with q as
(select 1 a, 2 b)
select a,b,concat2(a,b) c 
from q

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用 PySpark 与 Databricks 上的 apache spark function to_timestamp() 连接并添加一列 - Unable to concatenate with apache spark function to_timestamp() on Databricks using PySpark and add a column 如何使用 Databricks 在 Apache Spark 上编译 PySpark 中的 While 循环语句 - How to Compile a While Loop statement in PySpark on Apache Spark with Databricks 试图写一个 pyspark function 连接到 SQL 服务器与 Databricks 在 Apache Spart - Attempting to write a pyspark function to connect to SQL Server with Databricks on Apache Spart 使用 Apache Spark 在 Databricks 中使用 SQL 查询进行 CASTING 问题 - CASTING issue with SQL query in Databricks with Apache Spark Redshift to_timestamp 与时区偏移量 - Redshift to_timestamp with timezone offset 使用 Databricks(和 Apache Spark)从 AWS Redshift 读取 - Read from AWS Redshift using Databricks (and Apache Spark) 如何使用 Databricks 的 Apache Spark 从 SQL 表中获取 stream 数据 - How to stream data from SQL Table with Apache Spark with Databricks Apache Spark Streaming 连接字符串错误,Databricks 连接到 Azure 事件中心 - Apache Spark Streaming Connection String error with Databricks connection to Azure Event Hub 在Databricks中,SQL使用spark? - In Databricks, SQL uses spark? 使用 Databricks 上的 Apache Spark 将文件写入 delta lake 会产生与读取 Data Frame 不同的结果 - Writing out file to delta lake produces different results from Data Frame read using Apache Spark on Databricks
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM