파이썬을 통한 Azure Data Lake Storage 조작

Azure Data Lake Storage는 대량의 구조화되지 않은 데이터를 저장하고 처리하기 위한 확장 가능한 파일 시스템입니다. 이 기능은 클라우드 환경에서 Big Data 작업을 할 때 매우 유용합니다. 이번 블로그 포스트에서는 파이썬을 사용하여 Azure Data Lake Storage를 조작하는 방법을 알아보겠습니다.

Azure SDK 설치

Azure Data Lake Storage를 조작하기 위해서는 Azure SDK를 설치해야 합니다. 아래와 같이 파이썬 패키지 관리자인 pip를 사용하여 설치할 수 있습니다.

pip install azure-storage-file-datalake

인증 정보 구성

Azure Data Lake Storage에 접근하기 위해서는 인증 정보가 필요합니다. Azure Portal에서 데이터 레이크 저장소에 대한 액세스 키를 생성하거나 Azure Active Directory를 통해 인증해야 합니다. 인증 방법에 따라 다음과 같이 인증 정보를 구성할 수 있습니다.

액세스 키를 사용한 인증

from azure.storage.filedatalake import DataLakeStoreAccount
account_name = "<your_account_name>"
account_key = "<your_account_key>"
account = DataLakeStoreAccount(account_name=account_name, account_key=account_key)

Azure Active Directory를 사용한 인증

from azure.identity import DefaultAzureCredential
from azure.storage.filedatalake import DataLakeStoreAccount
account_name = "<your_account_name>"
credential = DefaultAzureCredential()
account = DataLakeStoreAccount(account_name=account_name, credential=credential)

데이터 읽기 및 쓰기

Azure Data Lake Storage에서 파일을 읽거나 쓰기 위해서는 readwrite 메서드를 사용할 수 있습니다. 아래는 간단한 예제 코드입니다.

데이터 읽기

file_path = "/path/to/file.txt"
file_contents = account.read(file_path)
print(file_contents)

데이터 쓰기

file_path = "/path/to/file.txt"
file_contents = "Hello, Azure Data Lake!"
account.write(file_path, file_contents)

디렉토리 조작

Azure Data Lake Storage에서 디렉토리를 생성,삭제, 이동하는 등의 작업을 할 수 있습니다.

디렉토리 생성

dir_path = "/path/to/directory"
account.create_directory(dir_path)

디렉토리 삭제

dir_path = "/path/to/directory"
account.delete_directory(dir_path)

디렉토리 이동 (이름 변경)

old_path = "/path/to/old_directory"
new_path = "/path/to/new_directory"
account.move_directory(old_path, new_path)

마치며

이번에는 파이썬을 사용하여 Azure Data Lake Storage를 조작하는 방법에 대해 알아보았습니다. Azure SDK를 설치하고 인증 정보를 구성한 뒤 데이터를 읽고 쓰는 작업 및 디렉토리 조작을 수행할 수 있습니다. Azure Data Lake Storage의 다양한 기능을 활용하여 대용량 데이터 처리를 해보세요.

#Azure #파이썬