如何使用Boto3從AWS Glue資料目錄中獲取使用者自定義函式的詳細資訊


讓我們看看使用者如何從AWS Glue資料目錄中獲取指定函式定義的詳細資訊。

示例

獲取資料庫**employee**中名為**insert_employee_record**的函式定義的詳細資訊。

問題陳述:使用Python中的**boto3**庫從AWS Glue資料目錄中獲取指定函式定義的詳細資訊。

解決此問題的步驟/演算法

  • 步驟1:匯入**boto3**和**botocore**異常以處理異常。

  • 步驟2:**database_name**和**function_name**是必需的引數。它獲取給定資料庫中給定**function_name**的定義。

  • 步驟3:使用**boto3庫**建立AWS會話。確保在預設配置檔案中提到了**region_name**。如果未提及,則在建立會話時顯式傳遞**region_name**。

  • 步驟4:為**glue**建立一個AWS客戶端。

  • 步驟5:呼叫**get_user_defined_function**並將**database_name**作為DatabaseName引數,**function_name**作為FunctionName引數傳遞。

  • 步驟6:它返回給定函式的定義。如果找不到給定函式,則會丟擲錯誤。

  • 步驟7:如果檢查函式時出現錯誤,則處理通用異常。

示例程式碼

以下程式碼獲取給定函式的定義:

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)

輸出

{
   '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'
         },
      ],
   }
}

更新於:2021年4月15日

253 次瀏覽

啟動您的職業生涯

完成課程獲得認證

開始
廣告