Computer >> 컴퓨터 >  >> 프로그래밍 >> Python

Python Boto3로 AWS Glue 데이터 카탈로그에서 분류자(Classifier) 세부 정보 조회하기

문제 상황

Python의 boto3 라이브러리를 사용하여 AWS Glue 데이터 카탈로그에 등록된 분류자(Classifier)의 세부 정보를 조회해 보겠습니다. 예를 들어, 'xml-test'라는 이름의 분류자 정보를 가져오는 것이 목표입니다.

해결 접근 방식

1단계 – boto3와 botocore 예외 처리 모듈을 임포트합니다.

2단계 – 조회하려는 분류자의 이름을 classifier_name 매개변수로 전달합니다.

3단계 – boto3 라이브러리를 사용하여 AWS 세션을 생성합니다. 기본 프로파일에 region_name이 설정되어 있는지 확인하고, 설정되어 있지 않다면 세션 생성 시 명시적으로 리전 이름을 지정해야 합니다.

4단계 – Glue 서비스를 위한 AWS 클라이언트를 생성합니다.

5단계get_classifier 함수를 호출하고 분류자 이름을 Name 파라미터로 전달합니다.

6단계 – 해당 분류자의 세부 정보를 응답으로 받아옵니다.

7단계 – 작업 수행 중 오류가 발생할 경우를 대비해 일반 예외도 함께 처리합니다.

예제 코드

아래 코드를 사용하면 AWS Glue 데이터 카탈로그에서 분류자의 세부 정보를 가져올 수 있습니다.

import boto3
from botocore.exceptions import ClientError

def get_classifier_details(classifier_name):
    session = boto3.session.Session()
    glue_client = session.client('glue')
    try:
        response = glue_client.get_classifier(Name=classifier_name)
        return response
    except ClientError as e:
        raise Exception("boto3 client error in get_classifier_details: " + e.__str__())
    except Exception as e:
        raise Exception("Unexpected error in get_classifier_details: " + e.__str__())

print(get_classifier_details("xml-test"))

실행 결과

{'Classifier': {'GrokClassifier': {'Name': 'xml-test', 'Classification': 'xml',
'CreationTime': datetime.datetime(2018, 6, 21, 4, 7, 4,
tzinfo=tzlocal()), 'LastUpdated': datetime.datetime(2018, 6, 21, 4, 7,
11, tzinfo=tzlocal()), 'Version': 2, 'GrokPattern': 'SYSLOGTIMESTAMP
%{MONTH} +%{MONTHDAY} %{TIME}'}}, 'ResponseMetadata': {'RequestId':
'c291cce2-………………..-3552077ddefd', 'HTTPStatusCode': 200, 'HTTPHeaders':
{'date': 'Sun, 21 Feb 2021 07:58:09 GMT', 'content-type':
'application/x-amz-json-1.1', 'content-length': '218', 'connection':
'keep-alive', 'x-amzn-requestid': 'c291cce2-……….-3552077ddefd'},
'RetryAttempts': 0}}