首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >AWS胶黏剂

AWS胶黏剂
EN

Stack Overflow用户
提问于 2018-03-08 18:17:15
回答 1查看 4.1K关注 0票数 3

在AWS Glue中,我需要转换一个浮点值(摄氏为华氏温度),并使用一个UDF。

以下是我的UDF:

代码语言:javascript
运行
复制
toFahrenheit = udf(lambda x: '-1' if x in not_found else x * 9 / 5 + 32, StringType())

我使用的UDF如下,在星星之火数据文件中:

代码语言:javascript
运行
复制
weather_df.withColumn("new_tmax", toFahrenheit(weather_df["tmax"])).drop("tmax").withColumnRenamed("new_tmax","tmax")

当我运行代码时,收到的错误消息如下:

代码语言:javascript
运行
复制
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的新手,我的新列模式没有创建,而且是空的。

用于上述示例的代码片段如下:

代码语言:javascript
运行
复制
%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)

的模式

代码语言:javascript
运行
复制
 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

错误跟踪:

代码语言:javascript
运行
复制
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): "

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-12 09:02:08

上述问题的解决办法(Celcius to Fahrenheit),仅供参考:

代码语言:javascript
运行
复制
#UDF to convert the air temperature from celsius to fahrenheit
toFahrenheit = udf(lambda x: x * 9 / 5 + 32, StringType())

weather_in_Fahrenheit = weather_df.withColumn("new_tmax", toFahrenheit(weather_df["tmax"])).withColumn("new_tmin", toFahrenheit(weather_df["tmin"])).drop("tmax").drop("tmin").withColumnRenamed("new_tmax","tmax").withColumnRenamed("new_tmin","tmin")

weather_in_Fahrenheit.show(5)

原始数据样本:

代码语言:javascript
运行
复制
+-----------+--------------------+---------+--------+---------+----+----+----+----+----------+
|    station|                name|elevation|latitude|longitude|prcp|snow|tmax|tmin|      date|
+-----------+--------------------+---------+--------+---------+----+----+----+----+----------+
|USW00094846|CHICAGO OHARE INT...|    201.8|  41.995| -87.9336| 0.0| 0.0|  25|  11|2013-01-01|
|USW00094846|CHICAGO OHARE INT...|    201.8|  41.995| -87.9336| 0.0| 0.0|  30|  10|2013-01-02|
|USW00094846|CHICAGO OHARE INT...|    201.8|  41.995| -87.9336| 0.0| 0.0|  29|  18|2013-01-03|
|USW00094846|CHICAGO OHARE INT...|    201.8|  41.995| -87.9336| 0.0| 0.0|  36|  13|2013-01-04|
|USW00094846|CHICAGO OHARE INT...|    201.8|  41.995| -87.9336|0.03| 0.4|  39|  18|2013-01-05|
|USW00094846|CHICAGO OHARE INT...|    201.8|  41.995| -87.9336| 0.0| 0.0|  36|  18|2013-01-06|
|USW00094846|CHICAGO OHARE INT...|    201.8|  41.995| -87.9336| 0.0| 0.0|  41|  15|2013-01-07|
|USW00094846|CHICAGO OHARE INT...|    201.8|  41.995| -87.9336| 0.0| 0.0|  44|  22|2013-01-08|
|USW00094846|CHICAGO OHARE INT...|    201.8|  41.995| -87.9336| 0.0| 0.0|  50|  27|2013-01-09|
|USW00094846|CHICAGO OHARE INT...|    201.8|  41.995| -87.9336|0.63| 0.0|  45|  22|2013-01-10|
+-----------+--------------------+---------+--------+---------+----+----+----+----+----------+

在应用了UDF toFahrenheit之后:

代码语言:javascript
运行
复制
+-----------+--------------------+--------+---------+---------+----------+-----+----+----+----+----+
|    station|                name|latitude|longitude|elevation|      date| awnd|prcp|snow|tmax|tmin|
+-----------+--------------------+--------+---------+---------+----------+-----+----+----+----+----+
|USW00094846|CHICAGO OHARE INT...|  41.995| -87.9336|    201.8|2013-01-01|  8.5| 0.0| 0.0|  77|  51|
|USW00094846|CHICAGO OHARE INT...|  41.995| -87.9336|    201.8|2013-01-02| 8.05| 0.0| 0.0|  86|  50|
|USW00094846|CHICAGO OHARE INT...|  41.995| -87.9336|    201.8|2013-01-03|11.41| 0.0| 0.0|  84|  64|
|USW00094846|CHICAGO OHARE INT...|  41.995| -87.9336|    201.8|2013-01-04| 13.2| 0.0| 0.0|  96|  55|
|USW00094846|CHICAGO OHARE INT...|  41.995| -87.9336|    201.8|2013-01-05| 9.62|0.03| 0.4| 102|  64|
+-----------+--------------------+--------+---------+---------+----------+-----+----+----+----+----+
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49179884

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档