AWSGlue爬虫在读取空文件时会创建多个表
创始人
2024-09-25 16:01:27
0

可以使用以下代码示例来解决此问题:

import json
import boto3

def lambda_handler(event, context):
    glue = boto3.client('glue')
    
    # 获取所有数据库
    databases = glue.get_databases()
    for database in databases['DatabaseList']:
        
        # 获取每个数据库中的所有表
        tables = glue.get_tables(DatabaseName=database['Name'])
        for table in tables['TableList']:
            
            # 判断表是否为空
            partition_keys = table.get('PartitionKeys')
            if partition_keys and len(partition_keys) == 1 and partition_keys[0]['Name'] == 'year':
                
                # 获取表路径并删除表
                table_uri = table['StorageDescriptor']['Location']
                glue.delete_table(DatabaseName=database['Name'], Name=table['Name'])
                
                # 重新创建空表
                empty_table = {
                    "Name": table['Name'],
                    "StorageDescriptor": {
                        "Columns": [
                            {
                                "Name": "Col1",
                                "Type": "string",
                                "Comment": ""
                            }
                        ],
                        "Location": table_uri,
                        "InputFormat": "org.apache.hadoop.mapred.TextInputFormat",
                        "OutputFormat": "org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat",
                        "Compressed": False,
                        "NumberOfBuckets": -1,
                        "SerdeInfo": {
                            "SerializationLibrary": "org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe",
                            "Parameters": {
                                "serialization.format": "1"
                            }
                        }
                    },
                    "PartitionKeys": [
                        {
                            "Name": "year",
                            "Type": "string"
                        }
                    ],
                    "TableType": "EXTERNAL_TABLE",
                    "Parameters": {
                        "classification": "parquet",
                        "transient_lastDdlTime": str(int(time.time()))
                    }
                }
                
                # 创建新的空表
                glue.create_table(
                    DatabaseName=database['Name'],
                    TableInput=empty_table
                )

这段代码使用AWS Lambda和AWS Glue API来检查所有数据库中的所有表,并删除为空的表。然后,它重新创建空表,该表只包含一个列“Col1”。这可以防止AWS Glue爬虫在读取空文件时创建多个表。

相关内容

热门资讯

9分钟辅助!德普之星私人局辅助... 9分钟辅助!德普之星私人局辅助器,xpoker辅助神器,机巧教程(证实有挂)进入游戏-大厅左侧-新手...
第3分钟辅助!we poker... 第3分钟辅助!we poker辅助器v3.3,wepoker透视功能下载,机巧教程(有挂透视)1、超...
第8分钟辅助!wepoker怎... 第8分钟辅助!wepoker怎么开辅助,黑侠破解wepoker,课程教程(今日头条)1、上手简单,内...
第十分钟辅助!如何下载wpk透... 第十分钟辅助!如何下载wpk透视版,pokerworld软件,策略教程(有挂分享)如何下载wpk透视...
九分钟辅助!wepoker辅助... 九分钟辅助!wepoker辅助真的假的,cloudpoker作弊,手筋教程(证实有挂)1、每一步都需...
第十分钟辅助!wejoker辅... 第十分钟辅助!wejoker辅助器怎么卖,wepoker免费透视脚本,窍门教程(有挂方式)1、进入游...
十分钟辅助!wepoker-h... 十分钟辅助!wepoker-h5下载,pokernow辅助控制,绝活教程(有挂实锤)1、点击下载安装...
5分钟辅助!aapoker辅助... 5分钟辅助!aapoker辅助器怎么用,wepoker模拟器哪个好用,模板教程(有挂辅助)wepok...
八分钟辅助!wpk官网下载链接... 八分钟辅助!wpk官网下载链接,wpk透视怎么安装,教程书教程(有挂分享)该软件可以轻松地帮助玩家将...
9分钟辅助!wepoker怎么... 9分钟辅助!wepoker怎么买辅助,wepoker安装教程,课程教程(有挂方略)1、wepoker...