如何通过Boto3将现有AWS Glue爬虫指定为Glue 3.0版本?
如何通过Boto3将AWS Glue爬虫设置为使用GlueVersion=3.0?
背景
现有一个连接MySQL数据库的AWS Glue爬虫运行正常,需要迁移至Glue 3.0版本以使用更新的MySQL JDBC驱动(Glue 2.0适配5.1版本,Glue 3.0适配8.0.23版本)。但通过Boto3的update_crawler操作时,未找到GlueVersion参数,控制台也无对应设置;爬虫配置中的Version字段为配置格式版本,设为3.0会报错。
核心说明
AWS Glue爬虫没有独立的GlueVersion配置项,它的运行环境和依赖的JDBC驱动版本,要么由Glue服务默认版本控制,要么通过关联的JDBC连接配置来指定。要使用Glue 3.0对应的MySQL JDBC驱动,可通过以下两种方式实现:
方法1:配置兼容Glue 3.0的官方JDBC连接器
如果使用AWS官方提供的MySQL JDBC连接器,在创建连接器时通过Boto3指定glueVersion参数为3.0,关联的爬虫会自动使用对应版本的驱动。示例代码:
glue_client = boto3.client('glue', region_name=region) # 创建兼容Glue 3.0的MySQL连接器 response = glue_client.create_connector( Name='mysql-connector-glue3', ConnectorType='JDBC', ConnectorConfiguration={ "glueVersion": "3.0", "connectionType": "mysql", "connectionProperties": { "JDBC_CONNECTION_URL": "jdbc:mysql://your-db-endpoint:3306/your-db", "USERNAME": "your-username", "PASSWORD": "your-password" } }, RoleArn=glue_role_arn, Description='MySQL connector for Glue 3.0' ) # 后续更新爬虫时关联该连接器 targets = { "JdbcTargets": [ { "ConnectionName": "mysql-connector-glue3", "Path": "your-db-schema/your-table" } ] } # 原爬虫更新代码保持不变,仅替换Targets为上述配置 configuration= {"Version": 1.0,"Grouping": {"TableGroupingPolicy": "CombineCompatibleSchemas" }} response = glue_client.update_crawler( Name= crawler_name, Role= glue_role_arn, DatabaseName=str(crawler_details['DatabaseName']) + '-' + str(env_suffix), Description=crawler_details['description'], Targets=targets, TablePrefix=crawler_details['TablePrefix'], Schedule=crawler_details['Schedule'], SchemaChangePolicy= crawler_details['SchemaChangePolicy'], Configuration=configuration )
方法2:手动指定自定义JDBC驱动路径
如果需要完全控制驱动版本,可将MySQL JDBC 8.0.23的JAR包上传至S3,然后在爬虫的JDBC目标配置中指定驱动路径和类名。示例代码:
glue_client = boto3.client('glue', region_name=region) # 配置Targets时指定自定义驱动 targets = { "JdbcTargets": [ { "ConnectionName": "your-existing-mysql-connection", "Path": "your-db-schema/your-table", "ExtraConnectionAttributes": "driver_path=s3://your-bucket/drivers/mysql-connector-java-8.0.23.jar,driver_class_name=com.mysql.cj.jdbc.Driver" } ] } # 执行爬虫更新 configuration= {"Version": 1.0,"Grouping": {"TableGroupingPolicy": "CombineCompatibleSchemas" }} response = glue_client.update_crawler( Name= crawler_name, Role= glue_role_arn, DatabaseName=str(crawler_details['DatabaseName']) + '-' + str(env_suffix), Description=crawler_details['description'], Targets=targets, TablePrefix=crawler_details['TablePrefix'], Schedule=crawler_details['Schedule'], SchemaChangePolicy= crawler_details['SchemaChangePolicy'], Configuration=configuration )
验证方式
爬虫运行后,可在CloudWatch日志中搜索JDBC Driver关键词,确认日志中显示的驱动版本是否为8.0.23。
内容的提问来源于stack exchange,提问作者Starbuck
相关产品推荐
相关产品推荐

