【发布时间】:2018-03-08 18:17:15
【问题描述】:
在 AWS Glue 中,我需要将浮点值(摄氏度到华氏度)转换为 UDF。
以下是我的 UDF:
toFahrenheit = udf(lambda x: '-1' if x in not_found else x * 9 / 5 + 32, StringType())
我在 spark 数据框中使用 UDF,如下所示:
weather_df.withColumn("new_tmax", toFahrenheit(weather_df["tmax"])).drop("tmax").withColumnRenamed("new_tmax","tmax")
当我运行代码时,我收到错误消息:
IllegalArgumentException: u"requirement failed: The number of columns doesn't match.\nOld column names (11): station, name, latitude, longitude, elevation, date, awnd, prcp, snow, tmin, tmax\nNew column names (0): "
不确定如何调用 UDF,因为我是 python / pyspark 的新手,并且我的新列架构没有创建,并且是空的。
用于上述示例的代码片段是:
%pyspark
import sys
from pyspark.context import SparkContext
from awsglue.context import GlueContext
from awsglue.context import DynamicFrame
from awsglue.transforms import *
from awsglue.utils import getResolvedOptions
from awsglue.job import Job
from pyspark.sql import SparkSession
from pyspark.sql.functions import udf
from pyspark.sql.types import StringType
glueContext = GlueContext(SparkContext.getOrCreate())
weather_raw = glueContext.create_dynamic_frame.from_catalog(database = "ohare-airport-2006", table_name = "ohare_intl_airport_2006_08_climate_csv")
print "cpnt : ", weather_raw.count()
weather_raw.printSchema()
weather_raw.toDF().show(10)
#UDF to convert the air temperature from celsius to fahrenheit (For sample transformation)
#toFahrenheit = udf((lambda c: c[1:], c * 9 / 5 + 32)
toFahrenheit = udf(lambda x: '-1' if x in not_found_cat else x * 9 / 5 + 32, StringType())
#Apply the UDF to maximum and minimum air temperature
wthdf = weather_df.withColumn("new_tmin", toFahrenheit(weather_df["tmin"])).withColumn("new_tmax", toFahrenheit(weather_df["tmax"])).drop("tmax").drop("tmin").withColumnRenamed("new_tmax","tmax").withColumnRenamed("new_tmin","tmin")
wthdf.toDF().show(5)
架构
weather_df:
root
|-- station: string
|-- name: string
|-- latitude: double
|-- longitude: double
|-- elevation: double
|-- date: string
|-- awnd: double
|-- fmtm: string
|-- pgtm: string
|-- prcp: double
|-- snow: double
|-- snwd: long
|-- tavg: string
|-- tmax: long
|-- tmin: long
错误跟踪:
Traceback (most recent call last):
File "/tmp/zeppelin_pyspark-3684249459612979499.py", line 349, in <module>
raise Exception(traceback.format_exc())
Exception: Traceback (most recent call last):
File "/tmp/zeppelin_pyspark-3684249459612979499.py", line 342, in <module>
exec(code)
File "<stdin>", line 3, in <module>
File "/usr/lib/spark/python/pyspark/sql/dataframe.py", line 1558, in toDF
jdf = self._jdf.toDF(self._jseq(cols))
File "/usr/lib/spark/python/lib/py4j-0.10.4-src.zip/py4j/java_gateway.py", line 1133, in __call__
answer, self.gateway_client, self.target_id, self.name)
File "/usr/lib/spark/python/pyspark/sql/utils.py", line 79, in deco
raise IllegalArgumentException(s.split(': ', 1)[1], stackTrace)
IllegalArgumentException: u"requirement failed: The number of columns doesn't match.\nOld column names (11): station, name, latitude, longitude, elevation, date, awnd, prcp, snow, tmin, tmax\nNew column names (0): "
谢谢
【问题讨论】:
-
你用的是什么版本的python?
-
它的 Python2.7,使用 AWS Glue 的开发端点
-
您能否尝试提供足够的资源来创建minimal reproducible example?例如,
not_found是什么? How to make good reproducible apache spark dataframe examples. -
另外,您不需要删除该列。您可以使用
withColumn()就地修改列。有关最近的问题,请参阅 my answer。 -
谢谢 pault,我已经用代码 sn-p 更新了我的问题,我使用的数据帧的架构详细信息,供您参考。认为我在 UDF 及其用法上出错了。请建议正确的 UDF 和用法。