Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.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 从pyspark中的字符串加载jalali日期_Apache Spark_Pyspark_Apache Spark Sql - Fatal编程技术网

Apache spark 从pyspark中的字符串加载jalali日期

Apache spark 从pyspark中的字符串加载jalali日期,apache-spark,pyspark,apache-spark-sql,Apache Spark,Pyspark,Apache Spark Sql,我需要从字符串中加载jalalidate,然后将其作为gregoriandate字符串返回。我正在使用以下代码: def jalali_to_gregorian(col, format=None): if not format: format = "%Y/%m/d" gre = jdatetime.datetime.strptime(col, format=format).togregorian() return gre.strftim

我需要从字符串中加载
jalali
date,然后将其作为
gregorian
date字符串返回。我正在使用以下代码:

def jalali_to_gregorian(col, format=None):
    if not format:
        format = "%Y/%m/d"
    gre = jdatetime.datetime.strptime(col, format=format).togregorian()
    return gre.strftime(format=format)

# register the function
spark.udf.register("jalali_to_gregorian", jalali_to_gregorian, StringType())
# load the date and show it:)
df = df.withColumn("financial_date", jalali_to_gregorian(df.PersianCreateDate))
df.select(['PersianCreateDate', 'financial_date']).show()
它抛出
ValueError:时间数据“列”与格式“%Y/%m/%d”不匹配。
列中的字符串是匹配的,我已经测试过了。这是spark如何将列值发送到我的函数的问题。怎么解决呢

要测试:

df=spark.createDataFrame([('1399/01/02',),('1399/01/01',)],['jalali'])
df = df.withColumn("gre", jalali_to_gregorian(df.jalali))
df.show()
应该导致

+----------+----------+
|    jalali|       gre|
+----------+----------+
|1399/01/02|2020/03/20|
|1399/01/01|2020/03/21|
+----------+----------+
相反,我被以下问题所困扰:

Fail to execute line 2: df = df.withColumn("financial_date",    jalali_to_gregorian(df.jalali))
Traceback (most recent call last):
File "/tmp/zeppelin_pyspark-6468469233020961307.py", line 375, in <module>
exec(code, _zcUserQueryNameSpace)
File "<stdin>", line 2, in <module>
File "<stdin>", line 7, in jalali_to_gregorian
File "/usr/local/lib/python2.7/dist-packages/jdatetime/__init__.py", line 929, in strptime
(date_string, format))
ValueError: time data 'Column<jalali>' does not match format '%Y/%m/%d''%Y/%m/%d'
无法执行第2行:df=df.withColumn(“财务日期”,jalali至公历(df.jalali))
回溯(最近一次呼叫最后一次):
文件“/tmp/zeppelin_pyspark-646846923309096307.py”,第375行,in
exec(代码,ZCUUserQueryNameSpace)
文件“”,第2行,在
文件“”,第7行,jalali_至_gregorian
文件“/usr/local/lib/python2.7/dist packages/jdatetime/_init__.py”,第929行,在strtime中
(日期\字符串,格式))
ValueError:时间数据“列”与格式“%Y/%m/%d”“%Y/%m/%d”不匹配

您的问题是,您试图将函数应用于列,而不是列中的值

您使用的代码:
spark.udf.register(“jalali_to_gregorian”,jalali_to_gregorian,StringType())
注册您的函数以在spark SQL中使用(通过
spark.SQL(…)
,而不是在pyspark中使用)

要获取可在
with column
select
等内部使用的函数,需要创建一个包装函数,该函数通过
udf
函数完成,并且该函数应在
with column
中使用:

从pyspark.sql.functions导入udf
jalali_to_gregorian_udf=udf(jalali_to_gregorian,StringType())
df=带列的df(“gre”,jalali_至gregorian_udf(df.jalali))
>>>df.show()
+----------+----------+
|贾拉利|格雷|
+----------+----------+
|1399/01/02|2020/03/21|
|1399/01/01|2020/03/20|
+----------+----------+
有关更多详细信息,请参阅

时间格式中也有错误-应该是
format=“%Y/%m/d”
而不是
format=“%Y/%m/%d”

另外,如果您运行的是Spark 3.x,那么我建议您查看-它们比通常的UDF快得多,如果您有大量数据,它们将提供更好的性能