Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache spark 在Spark 3.0中将StringType转换为TimestampType_Apache Spark_Pyspark_Apache Spark Sql - Fatal编程技术网

Apache spark 在Spark 3.0中将StringType转换为TimestampType

Apache spark 在Spark 3.0中将StringType转换为TimestampType,apache-spark,pyspark,apache-spark-sql,Apache Spark,Pyspark,Apache Spark Sql,我一直在使用pyspark 3.0。我有一个StringType中带有“time”列的数据帧。我正在尝试将其转换为时间戳。数据帧如下所示 +---------------+ | time| +---------------+ |10:59:46.000 AM| | 6:26:36.000 PM| |11:13:38.000 PM| +---------------+ 我试着同时使用时间戳和unix时间戳 df.withColumn("new_time",

我一直在使用pyspark 3.0。我有一个StringType中带有“time”列的数据帧。我正在尝试将其转换为时间戳。数据帧如下所示

+---------------+
|           time|
+---------------+
|10:59:46.000 AM|
| 6:26:36.000 PM|
|11:13:38.000 PM|
+---------------+
我试着同时使用时间戳和unix时间戳

df.withColumn("new_time", F.to_timestamp(col("time"),"hh:mm:ss.SSS a")).show()

我得到的错误是这个

org.apache.spark.SparkUpgradeException: You may get a different result due to the upgrading of Spark 3.0: Fail to parse '6:26:36.000 PM' in the new parser. You can set spark.sql.legacy.timeParserPolicy to LEGACY to restore the behavior before Spark 3.0, or set to CORRECTED and treat it as an invalid datetime string.
我想知道Spark 3.0在没有设置的情况下是如何实现的

spark.conf.set("spark.sql.legacy.timeParserPolicy","LEGACY")
任何帮助都将不胜感激。谢谢。

不需要填充

您需要更改转换字符串的格式。。从中删除h,然后它就可以工作了

df.withColumn('new_time', f.unix_timestamp(df['Timestamp'], 'h:mm:ss.SSS a'))
格式说明:

'hh:mm:ss.SSS a'
01:00:00.000 pm
11:00:00.000 am

'h:mm:ss.SSS a'
1:00:00.000 pm
11:00:00.000 am
不需要填充

您需要更改转换字符串的格式。。从中删除h,然后它就可以工作了

df.withColumn('new_time', f.unix_timestamp(df['Timestamp'], 'h:mm:ss.SSS a'))
格式说明:

'hh:mm:ss.SSS a'
01:00:00.000 pm
11:00:00.000 am

'h:mm:ss.SSS a'
1:00:00.000 pm
11:00:00.000 am
试试这个-

df.withColumnnew_time,F.to_timestampF.lpadColumn,15,0,hh:mm:ss.SSS a.show 一些解释

一,。lpadcolumn,长度-

此函数检查指定字符串的长度,如果字符串的长度<指定的长度,则将使用字符串_to_左填充

范例

输入行6:26:36.000 PM只有14个字符,由于指定的长度为15,它将保留第3个参数0,使其长度为15。现在o/p od lpad为下午6:26:36.000。这与to_timestamp中指定的格式相匹配

更多说明

试试这个-

df.withColumnnew_time,F.to_timestampF.lpadColumn,15,0,hh:mm:ss.SSS a.show 一些解释

一,。lpadcolumn,长度-

此函数检查指定字符串的长度,如果字符串的长度<指定的长度,则将使用字符串_to_左填充

范例

输入行6:26:36.000 PM只有14个字符,由于指定的长度为15,它将保留第3个参数0,使其长度为15。现在o/p od lpad为下午6:26:36.000。这与to_timestamp中指定的格式相匹配


更多解释

哦,我明白了。非常感谢。我仍然想知道为什么在小时格式中有两个数字,所以用一个“h”代替“hh”。嗨,如果有一个,它就只有一个数字,所以只有一个h。哦,我明白了。非常感谢。我仍然想知道,既然小时格式中有两位数字,为什么会有一个“h”而不是“hh”。嗨,如果只有一位,它就只有一位数字,因此只有一个h。这也行。你能给我简单解释一下这是怎么回事吗?谢谢添加了最基本的解释这也有效。你能给我简单解释一下这是怎么回事吗?谢谢添加了最低限度的解释