Aws Glue pyspark UDF 正在抛出错误调用 o104.showString 时发生错误。追溯(最近一次通话最后)
Aws Glue pypark UDF is throwing error An error occurred while calling o104.showString. Traceback (most recent call last)
我需要使用 pyspark 在 aws glue 中获得预期的数据帧,最后显示
#################Initial Dataframe#################
+---+--------------------+-------------------+
|_c0| _c1| time|
+---+--------------------+-------------------+
| 1| null|2020-05-30 19:36:32|
| 2|Mobii5 |2020-05-30 19:36:32|
| 3|Nooft biHi ooFrame 2|2020-05-30 19:36:32|
| 4|Samsung mobile ...|2020-05-30 19:36:32|
| 5|Samsung ppjomes ...|2020-05-30 19:36:32|
| 6| samsung GTP G Tv ne|2020-05-30 19:36:32|
| 7| all mightyPanasoci |2020-05-30 19:36:32|
| 8|Samsung hola .|2020-05-30 19:36:32|
| 9|Mooron phoines Mondo|2020-05-30 19:36:32|
| 10|Samsung Guru .......|2020-05-30 19:36:32|
+---+--------------------+-------------------+
下面是我的代码
time_udf = udf(lambda x: year(x), IntegerType())
timestamp = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
new_df = df.withColumn('time',unix_timestamp(lit(timestamp),'yyyy-MM-dd HH:mm:ss').cast("timestamp"))
print(new_df.show(10))
time.sleep(30)
df1 = new_df.withColumn('Year',time_udf(col("time")))
df1.createOrReplaceTempView("people")
sqlDF = spark.sql("SELECT * FROM people")
sqlDF.show()
print(df1.printSchema())
return(df1)
需要使用 aws UDF pyspark 获得如上所示的输出
###############Expected###################
+---+--------------------+-------------------+----+
|_c0| _c1| time|Year|
+---+--------------------+-------------------+----+
| 1| null|2020-05-29 20:07:58|2020|
| 2|Mobiistar Prime 5...|2020-05-29 20:07:58|2020|
| 3|NTT Hikari i-Frame 2|2020-05-29 20:07:58|2020|
| 4|Samsung SM-P605K ...|2020-05-29 20:07:58|2020|
| 5|Samsung SM-G850W ...|2020-05-29 20:07:58|2020|
| 6|samsung GTP G Tv ne |2020-05-29 20:07:58|2020|
| 7|all mightyPanasoci |2020-05-29 20:07:58|2020|
| 8|Samsung hola .|2020-05-29 20:07:58|2020|
| 9|Mooron phoines Mondo|2020-05-29 20:07:58|2020|
| 10|Samsung Guru .......|2020-05-29 20:07:58|2020|
+---+--------------------+-------------------+----+
我可以使用下面这行
df1 = new_df.withColumn('Year',year(new_df.time))
但是我需要使用UDF作为需求
您不能在 UDF 中使用 year
,因为它是 pyspark 函数。
如果您确实需要使用 UDF,您可以使用常用的 python 日期时间函数来完成:
from datetime import datetime
def extractYear(datestring):
dt = datetime.strptime(datestring, '%Y-%m-%d %H:%M:%S')
return dt.year
time_udf = udf(lambda x: extractYear(x), IntegerType())
但是使用 year
,就像在 .withColumn('Year',year(new_df.time))
中一样,会更容易和更快,所以如果它有效 - 最好坚持下去。
我需要使用 pyspark 在 aws glue 中获得预期的数据帧,最后显示
#################Initial Dataframe#################
+---+--------------------+-------------------+
|_c0| _c1| time|
+---+--------------------+-------------------+
| 1| null|2020-05-30 19:36:32|
| 2|Mobii5 |2020-05-30 19:36:32|
| 3|Nooft biHi ooFrame 2|2020-05-30 19:36:32|
| 4|Samsung mobile ...|2020-05-30 19:36:32|
| 5|Samsung ppjomes ...|2020-05-30 19:36:32|
| 6| samsung GTP G Tv ne|2020-05-30 19:36:32|
| 7| all mightyPanasoci |2020-05-30 19:36:32|
| 8|Samsung hola .|2020-05-30 19:36:32|
| 9|Mooron phoines Mondo|2020-05-30 19:36:32|
| 10|Samsung Guru .......|2020-05-30 19:36:32|
+---+--------------------+-------------------+
下面是我的代码
time_udf = udf(lambda x: year(x), IntegerType())
timestamp = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
new_df = df.withColumn('time',unix_timestamp(lit(timestamp),'yyyy-MM-dd HH:mm:ss').cast("timestamp"))
print(new_df.show(10))
time.sleep(30)
df1 = new_df.withColumn('Year',time_udf(col("time")))
df1.createOrReplaceTempView("people")
sqlDF = spark.sql("SELECT * FROM people")
sqlDF.show()
print(df1.printSchema())
return(df1)
需要使用 aws UDF pyspark 获得如上所示的输出
###############Expected###################
+---+--------------------+-------------------+----+
|_c0| _c1| time|Year|
+---+--------------------+-------------------+----+
| 1| null|2020-05-29 20:07:58|2020|
| 2|Mobiistar Prime 5...|2020-05-29 20:07:58|2020|
| 3|NTT Hikari i-Frame 2|2020-05-29 20:07:58|2020|
| 4|Samsung SM-P605K ...|2020-05-29 20:07:58|2020|
| 5|Samsung SM-G850W ...|2020-05-29 20:07:58|2020|
| 6|samsung GTP G Tv ne |2020-05-29 20:07:58|2020|
| 7|all mightyPanasoci |2020-05-29 20:07:58|2020|
| 8|Samsung hola .|2020-05-29 20:07:58|2020|
| 9|Mooron phoines Mondo|2020-05-29 20:07:58|2020|
| 10|Samsung Guru .......|2020-05-29 20:07:58|2020|
+---+--------------------+-------------------+----+
我可以使用下面这行
df1 = new_df.withColumn('Year',year(new_df.time))
但是我需要使用UDF作为需求
您不能在 UDF 中使用 year
,因为它是 pyspark 函数。
如果您确实需要使用 UDF,您可以使用常用的 python 日期时间函数来完成:
from datetime import datetime
def extractYear(datestring):
dt = datetime.strptime(datestring, '%Y-%m-%d %H:%M:%S')
return dt.year
time_udf = udf(lambda x: extractYear(x), IntegerType())
但是使用 year
,就像在 .withColumn('Year',year(new_df.time))
中一样,会更容易和更快,所以如果它有效 - 最好坚持下去。