首页 文章

如何使用本地机器上的python 2.7执行aws胶水脚本?

提问于
浏览
2

我在 python 2.7 环境中安装了 aws cliboto3 . 我想做各种操作,如获取模式信息,获取AWS Glue控制台中存在的所有表的数据库详细信息 . 我试过下面的脚本示例:

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(SparkContext.getOrCreate())
persons = glueContext.create_dynamic_frame.from_catalog(
             database="records",
             table_name="recordsrecords_converted_json")
print "Count: ", persons.count()
persons.printSchema()

我收到了错误 ImportError: No module named awsglue.transforms ,这应该是正确的,因为boto3中没有这样的包,因为我使用命令 dir(boto3) 识别 . 我发现 boto3 通过 awscli 提供了各种客户端调用,我们可以使用 client=boto3.client('glue') 来访问它们 . 因此,为了获得上面的架构信息,我尝试了下面的示例代码:

import sys
import boto3
client=boto3.client('glue')
response = client.get_databases(
    CatalogId='string',
    NextToken='string',
    MaxResults=123
)
print client

但后来我收到了这个错误: AccessDeniedException: An error occurred (AccessDeniedException) when calling the GetDatabases operation: Cross account access is not allowed.

我很确定他们中的任何一个或者他们两个都是正确的方法来获得我想要获得的东西,但有些东西不属于正确的插槽 . 有什么想法在本地使用python 2.7从AWS Glue获取有关模式和数据库表的详细信息,就像我在上面尝试过的那样?

1 回答

  • 2

    以下代码适用于我,并使用本地设置的Zeppelin笔记本,作为开发 endpoints . printschema从数据目录中读取模式 .

    希望你也启用了ssh隧道 .

    %pyspark
    import sys
    from pyspark.context import SparkContext
    from awsglue.context import GlueContext
    from awsglue.transforms import *
    from pyspark.sql.functions import udf
    from pyspark.sql.types import StringType
    
    # Create a Glue context
    glueContext = GlueContext(SparkContext.getOrCreate())
    
    # Create a DynamicFrame using the 'persons_json' table
    medicare_dynamicframe = glueContext.create_dynamic_frame.from_catalog(database="payments", table_name="medicaremedicare_hospital_provider_csv")
    
    # Print out information about this data
    print "Count:  ", medicare_dynamicframe.count()
    medicare_dynamicframe.printSchema()
    

    此外,您可能需要对Spark解释器进行一些更改,(勾选顶部可用的 Connect to existing process 选项和主机(localhost),端口号(9007)) .

    For second part 您需要执行 aws configure ,然后在安装 boto3 client后创建glue客户端 . 在此之后,检查您的代理设置是否隐藏在防火墙或公司网络后面 .

    需要明确的是, boto3 客户端对所有与AWS相关的客户端api都有帮助,对于服务器端,Zeppelin方式是最好的 .

    希望这可以帮助 .

相关问题