How to get the details of a user-defined function in a database from AWS Glue Data catalog using Boto3


Let's see how a user can get the details of a specified function definition from AWS Glue Data Catalog.

Example

Get the details of a function definition named as insert_employee_record in database employee.

Problem Statement: Use boto3 library in Python to get the details of a specified function definition from AWS Glue Data Catalog.

Approach/Algorithm to solve this problem

  • Step 1: Import boto3 and botocore exceptions to handle exceptions.

  • Step 2: database_name and function_name are the required parameters. It fetches the definition of a given function_name in a given database.

  • Step 3: Create an AWS session using boto3 lib. Make sure region_name is mentioned in the default profile. If it is not mentioned, then explicitly pass the region_name while creating the session.

  • Step 4: Create an AWS client for glue.

  • Step 5: Call get_user_defined_function and pass the database_name as DatabaseName and function_name as FunctionName parameter.

  • Step 6: It returns the definition of a given function. If it doesn't find the given function, it throws an error.

  • Step 7: Handle the generic exception if something went wrong while checking the function.

Example Code

The following code fetches the definition of a given function −

import boto3
from botocore.exceptions import ClientError

def get_function_definition(database_name, function_name):
   session = boto3.session.Session()
   glue_client = session.client('glue')
   try:
      response = glue_client.get_user_defined_function(DatabaseName=database_name, FunctionName=function_name)
      return response
   except ClientError as e:
      raise Exception("boto3 client error in get_function_definition: " + e.__str__())
   except Exception as e:
      raise Exception("Unexpected error in get_function_definition: " + e.__str__())
a = get_function_definition('employee', 'insert_employee_record')
print(a)

Output

{
   'UserDefinedFunctions':{
      'FunctionName': 'insert_employee_record',
      'DatabaseName': 'employee',
      'ClassName': 'InsertEmployee',
      'OwnerName': 'string',
      'OwnerType': 'USER'|'ROLE'|'GROUP',
      'CreateTime': datetime(2021,03,15),
      'ResourceUris':[
         {
            'ResourceType': 'JAR'|'FILE'|'ARCHIVE',
            'Uri': 'string'
         },
      ],
   }
}

Updated on: 15-Apr-2021

203 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements