【问题标题】:Unable to run scripts properly in AWS Glue PySpark Dev Endpoint无法在 AWS Glue PySpark 开发终端节点中正确运行脚本
【发布时间】:2018-08-10 19:17:50
【问题描述】:

我已经配置了一个 AWS Glue 开发端点,并且可以在 pyspark REPL shell 中成功连接到它 - 就像这样 https://docs.aws.amazon.com/glue/latest/dg/dev-endpoint-tutorial-repl.html

与 AWS 文档中给出的示例不同,我在开始会话时收到警告,后来在 AWS Glue DynamicFrame 结构上的各种操作失败。这是启动会话的完整日志 - 请注意有关 spark.yarn.jars 和 PyGlue.zip 的错误:

Python 2.7.12 (default, Sep  1 2016, 22:14:00)
[GCC 4.8.3 20140911 (Red Hat 4.8.3-9)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Setting default log level to "WARN".
To adjust logging level use sc.setLogLevel(newLevel). For SparkR, use setLogLevel(newLevel).
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/usr/share/aws/glue/etl/jars/glue-assembly.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/usr/lib/spark/jars/slf4j-log4j12-1.7.16.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
18/03/02 14:18:58 WARN Client: Neither spark.yarn.jars nor spark.yarn.archive is set, falling back to uploading libraries under SPARK_HOME.
18/03/02 14:19:03 WARN Client: Same path resource file:/usr/share/aws/glue/etl/python/PyGlue.zip added multiple times to distributed cache.
18/03/02 14:19:13 WARN ObjectStore: Failed to get database global_temp, returning NoSuchObjectException
Welcome to
      ____              __
     / __/__  ___ _____/ /__
    _\ \/ _ \/ _ `/ __/  '_/
   /__ / .__/\_,_/_/ /_/\_\   version 2.1.0
      /_/

Using Python version 2.7.12 (default, Sep  1 2016 22:14:00)
SparkSession available as 'spark'.
>>>

许多操作都按我的预期工作,但我也收到了一些不受欢迎的异常,例如我可以从我的 Glue 目录加载数据检查其结构和其中的数据,但我无法对其应用 Map 或将其转换为一个DF。这是我的完整执行运行日志(除了最长的错误消息)。前几个命令和设置都运行良好,但最后两个操作失败:

>>> import sys
>>> from awsglue.transforms import *
>>> from awsglue.utils import getResolvedOptions
>>> from pyspark.context import SparkContext
>>> from awsglue.context import GlueContext
>>> from awsglue.job import Job
>>>
>>> glueContext = GlueContext(spark)
>>> # Receives a string of the format yyyy-mm-dd hh:mi:ss.nnn and returns the first 10 characters: yyyy-mm-dd
... def TruncateTimestampString(ts):
...   ts = ts[:10]
...   return ts
...
>>> TruncateTimestampString('2017-03-05 06:12:08.376')
'2017-03-05'
>>>
>>> # Given a record with a timestamp property returns a record with a new property, day, containing just the date portion of the timestamp string, expected to be yyyy-mm-dd.
... def TruncateTimestamp(rec):
...   rec[day] = TruncateTimestampString(rec[timestamp])
...   return rec
...
>>> # Get the history datasource - WORKS WELL BUT LOGS log4j2 ERROR
>>> datasource_history_1 = glueContext.create_dynamic_frame.from_catalog(database = "dev", table_name = "history", transformation_ctx = "datasource_history_1")
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
>>> # Tidy the history datasource - WORKS WELL
>>> history_tidied = datasource_history_1.drop_fields(['etag', 'jobmaxid', 'jobminid', 'filename']).rename_field('id', 'history_id')
>>> history_tidied.printSchema()
root
|-- jobid: string
|-- spiderid: long
|-- timestamp: string
|-- history_id: long

>>> # Trivial observation of the SparkSession objects
>>> SparkSession
<class 'pyspark.sql.session.SparkSession'>
>>> spark
<pyspark.sql.session.SparkSession object at 0x7f8668f3b650>
>>> 
>>> 
>>> # Apply a mapping to the tidied history datasource. FAILS
>>> history_mapped = history_tidied.map(TruncateTimestamp)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/mnt/tmp/spark-1f0341db-5de6-4008-974f-a1d194524a86/userFiles-6a67bdee-7c44-46d6-a0dc-9daa7177e7e2/PyGlue.zip/awsglue/dynamicframe.py", line 101, in map
File "/mnt/tmp/spark-1f0341db-5de6-4008-974f-a1d194524a86/userFiles-6a67bdee-7c44-46d6-a0dc-9daa7177e7e2/PyGlue.zip/awsglue/dynamicframe.py", line 105, in mapPartitionsWithIndex
File "/usr/lib/spark/python/pyspark/rdd.py", line 2419, in __init__
    self._jrdd_deserializer = self.ctx.serializer
AttributeError: 'SparkSession' object has no attribute 'serializer'
>>> history_tidied.toDF()
ERROR
Huge error log and stack trace follows, longer than my console can remember. Here's how it finishes:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/mnt/tmp/spark-1f0341db-5de6-4008-974f-a1d194524a86/userFiles-6a67bdee-7c44-46d6-a0dc-9daa7177e7e2/PyGlue.zip/awsglue/dynamicframe.py", line 128, in toDF
  File "/usr/lib/spark/python/lib/py4j-0.10.4-src.zip/py4j/java_gateway.py", line 1133, in __call__
  File "/usr/lib/spark/python/pyspark/sql/utils.py", line 79, in deco
    raise IllegalArgumentException(s.split(': ', 1)[1], stackTrace)
pyspark.sql.utils.IllegalArgumentException: u"Error while instantiating 'org.apache.spark.sql.hive.HiveSessionState':"

我认为我正在遵循 Amazon 在其 Dev Endpoint REPL 说明中给出的说明,但是由于这些相当简单的操作(DynamicFrame.join 和 DynamicFrame.toDF)失败了,当我想运行真正的工作(这似乎成功了,但我的 DynamicFrame.printSchema() 和 DynamicFrame.show() 命令没有显示在 CloudWatch 日志中以供执行)。

有谁知道我需要做什么来修复我的 REPL 环境,以便我可以正确测试 pyspark AWS Glue 脚本?

【问题讨论】:

    标签: amazon-web-services pyspark aws-glue


    【解决方案1】:

    是在Windows还是Unix环境下配置的开发端点,看起来下载的文件没有复制到linux的正确位置,或者代码sn-p找不到jar的路径。请确保文件在那里。

    我已经在我的 Windows 机器上设置了 Zeppelin 笔记本,并且能够成功连接到胶水目录。也许你也可以试试,如果你需要任何帮助,请告诉我。

    【讨论】:

    • 谢谢尤瓦。我可以很好地连接,您能否在您的 Zeppelin 笔记本中检查您是否能够运行任何导致我出现问题的操作:DynamicFrame.toDF() 或 DynamicFrame.map(someMapFunction)?
    • 不确定 Yuva Windows 与 Unix 对 Dev Endpoint 有何影响?此处未指定操作系统:eu-west-1.console.aws.amazon.com/glue/home#addEditDevEndpoint: - 我只是通过 SSH 连接到 Amazon 为我设置的实例。所有代码执行等都发生在 REPL shell 的那个实例中。我将尝试自己设置 Zeppelin 来尝试一下,看看我想出了什么。
    • 您能否参考此 SO 回复:stackoverflow.com/questions/42942620/…。我认为那里提供的建议/解决方案也应该对您有用。
    • 对于 Windows 笔记本电脑上的 zeppelin 设置,此链接 (hernandezpaul.wordpress.com/2016/11/14/…) 应该对您有所帮助。
    • 抱歉 Yuva Kumar,我不能接受这个答案。我不知道这是否是一个解决方案。我可以很好地连接到我的 AWS Glue 端点,但我发现我无法针对它运行一些命令。您提到了安装路径和 Windows 与 Unix,但开发端点是由 Amazon 自动创建的。我考虑设置 Zeppelin,但这是一项很大的工作,超出了我的能力范围 - 我只想能够直接针对 Dev 端点运行来自 Amazon 示例和文档的所有 Python 命令。
    【解决方案2】:

    AWS Support 终于回复了我对此问题的询问。回复如下:

    在进一步研究中,我发现这是 PySpark shell 的一个已知问题,并且胶水服务团队已经在处理它。该修复程序应该很快就会部署,但是目前我没有可以与您分享的 ETA。

    同时这里有一个解决方法:在初始化 Glue 上下文之前,您可以这样做

    >> newconf = sc._conf.set("spark.sql.catalogImplementation", "in-memory")
    >> sc.stop()
    >> sc = sc.getOrCreate(newconf)
    

    然后从那个sc实例化glueContext。

    我可以确认这对我有用,这是我能够运行的脚本:

    import sys
    from awsglue.transforms import *
    from awsglue.utils import getResolvedOptions
    from pyspark.context import SparkContext
    from awsglue.context import GlueContext
    from awsglue.job import Job
    
    # New recommendation from AWS Support 2018-03-22
    newconf = sc._conf.set("spark.sql.catalogImplementation", "in-memory")
    sc.stop()
    sc = sc.getOrCreate(newconf)
    # End AWS Support Workaround
    
    glueContext = GlueContext(sc)
    spark = glueContext.spark_session
    job = Job(glueContext)
    
    datasource_history_1 = glueContext.create_dynamic_frame.from_catalog(database = "dev", table_name = "history", transformation_ctx = "datasource_history_1")
    
    def DoNothingMap(rec):
      return rec
    
    history_mapped = datasource_history_1.map(DoNothingMap)
    history_df = history_mapped.toDF()
    history_df.show()
    history_df.printSchema()
    

    以前.map().toDF() 调用会失败。

    我已要求 AWS Support 在此问题解决后通知我,以便不再需要解决方法。

    【讨论】:

    • 看起来这与这个 spark bug issues.apache.org/jira/browse/SPARK-26794 有关。它似乎已在此补丁中得到修复,该补丁仅将包含在 Spark 3.0.0 版本(仍是候选版本)中,并且可能在 AWS Glue 支持该版本之前很长时间,请参阅 github.com/apache/spark/pull/23709 和补丁 github.com/apache/spark/commit/… GlueContext 自动支持与 Glue 目录“外部 Hive 目录”的集成,而不是内存中的 Derby DB,所以这个解决方法很奇怪
    • 在您的脚本中,您在声明之前指的是 SparkContext sc,这是可能的,因为它是由 spark shell 使用您的 spark 配置创建的全局,我想知道您的配置是否指定hive 作为spark.sql.catalogImplementation ?
    猜你喜欢
    • 2020-11-06
    • 1970-01-01
    • 1970-01-01
    • 2019-06-12
    • 2018-01-30
    • 2022-11-02
    • 2018-02-07
    • 1970-01-01
    • 2014-05-01
    相关资源
    最近更新 更多