Cod*_*ter 5 python amazon-web-services boto3 aws-glue
我aws cli和boto3安装在我的python 2.7环境。我想执行各种操作,例如获取架构信息,获取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()
Run Code Online (Sandbox Code Playgroud)
我得到的错误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
Run Code Online (Sandbox Code Playgroud)
但是然后我得到这个错误:
AccessDeniedException: An error occurred (AccessDeniedException) when calling the GetDatabases operation: Cross account access is not allowed.
我很确定其中之一或可能两者都是正确的方法来获得我想要得到的东西,但是这里有些东西并没有落入正确的位置。有什么想法可以像我上面尝试的那样在本地使用python 2.7从AWS Glue获取有关架构和数据库表的详细信息吗?
以下代码适用于我,并且使用本地设置的 Zeppelin 笔记本作为开发端点。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()
Run Code Online (Sandbox Code Playgroud)
此外,您可能需要对 Spark 解释器进行一些更改(勾选顶部的“连接到现有进程”选项、主机(localhost)、端口号(9007)。
对于第二部分,您需要aws configure在安装客户端后执行并创建glue客户端boto3。之后,检查您的代理设置是否隐藏在防火墙或公司网络后面。
需要明确的是,boto3客户端对于所有与 AWS 相关的客户端 api 都有帮助,而对于服务器端,Zeppelin 方式是最好的。
希望这可以帮助。