[Python] I need to extract database Information like schema names, table names, column names...

Discussão em 'Python' iniciado por Stack, Abril 29, 2025.

  1. Stack

    Stack Membro Participativo

    Tried the below code but getting the Lambda handler error. Could someone please help or suggest me the right python code in achieving to get all the database details which I had mentioned in the title.

    I have been using the below python code in AWS Lambda funtion

    import boto3

    import json

    def get_glue_databases():

    """Fetches databases from AWS Glue Data Catalog."""

    glue_client = boto3.client('glue')

    databases = glue_client.get_databases()



    db_list = []

    for db in databases.get('DatabaseList', []):

    db_list.append(db['Name'])



    return db_list


    def get_glue_tables(database_name):

    """Fetches tables and column details for a given Glue database."""

    glue_client = boto3.client('glue')

    tables = glue_client.get_tables(DatabaseName=database_name)



    table_details = {}

    for table in tables.get('TableList', []):

    table_name = table['Name']

    columns = [col['Name'] for col in table['StorageDescriptor']['Columns']]

    table_details[table_name] = columns



    return table_details


    def get_glue_jobs():

    """Fetches Glue ETL job details, including script locations."""

    glue_client = boto3.client('glue')

    response = glue_client.get_jobs()



    jobs = []

    for job in response.get('Jobs', []):

    job_info = {

    "JobName": job.get('Name'),

    "ScriptLocation": job.get('Command', {}).get('ScriptLocation'),

    "Connections": job.get('Connections', {}).get('Connections', []),

    "Arguments": job.get('DefaultArguments', {})

    }

    jobs.append(job_info)



    return jobs


    def extract_join_conditions(script_content):

    """Extracts join conditions from an ETL script (Basic regex-based approach)."""

    import re

    join_patterns = [

    r"JOIN\s+(\w+)\s+ON\s+([\w.=<> ]+)", # SQL-style JOIN

    r"merge\(\s*([\w]+),\s*([\w]+)\s*\).*(==|=|>|<)\s*([\w.]+)", # PySpark merge example

    ]



    joins = []

    for pattern in join_patterns:

    matches = re.findall(pattern, script_content, re.IGNORECASE)

    for match in matches:

    joins.append(match)



    return joins


    def main():

    # Get database names

    databases = get_glue_databases()

    print("\nDatabases:", databases)



    # Get tables and columns for each database

    for db in databases:

    print(f"\nTables in {db}:")

    tables = get_glue_tables(db)

    for table, columns in tables.items():

    print(f" Table: {table}, Columns: {columns}")



    # Get Glue Jobs and their scripts

    jobs = get_glue_jobs()

    for job in jobs:

    print(f"\nETL Job: {job['JobName']}")

    print(f" Script Location: {job['ScriptLocation']}")

    print(f" Connections: {job['Connections']}")



    # Assuming script is stored in S3, you can download and parse it

    if job['ScriptLocation'].startswith("s3://"):

    s3_client = boto3.client('s3')

    bucket, key = job['ScriptLocation'][5:].split("/", 1)

    script_content = s3_client.get_object(Bucket=bucket, Key=key)['Body'].read().decode("utf-8")



    # Extract join conditions

    joins = extract_join_conditions(script_content)

    print(" Join Conditions:", joins)


    if name == "main":

    main()


    And while running the code getting the below error

    Status: Failed Test Event Name: Sample_db_test

    Response: { "errorMessage": "Handler 'lambda_handler' missing on module 'lambda_function'", "errorType": "Runtime.HandlerNotFound", "requestId": "", "stackTrace": [] }

    Function Logs: [ERROR] Runtime.HandlerNotFound: Handler 'lambda_handler' missing on module 'lambda_function' Traceback (most recent call last): INIT_REPORT Init Duration: 302.70 ms Phase: init Status: error Error Type: Runtime.HandlerNotFound [ERROR] Runtime.HandlerNotFound: Handler 'lambda_handler' missing on module 'lambda_function' Traceback (most recent call last): INIT_REPORT Init Duration: 3039.49 ms Phase: invoke Status: timeout START RequestId: aa0300a5-5792-4fe7-89e0-5affedd3f2bc Version: $LATEST 2025-04-29T08:51:13.811Z aa0300a5-5792-4fe7-89e0-5affedd3f2bc Task timed out after 3.10 seconds

    END RequestId: aa0300a5-5792-4fe7-89e0-5affedd3f2bc REPORT RequestId: aa0300a5-5792-4fe7-89e0-5affedd3f2bc Duration: 3101.17 ms Billed Duration: 3000 ms Memory Size: 128 MB Max Memory Used: 21 MB

    Request ID: aa0300a5-5792-4fe7-89e0-5affedd3f2bc

    Continue reading...

Compartilhe esta Página