다음을 통해 공유


자습서: Databricks를 사용하여 컨테이너 통계 계산

이 자습서에서는 Azure Databricks와 함께 Azure Blob Storage 인벤토리를 사용하여 컨테이너에 대한 통계를 수집하는 방법을 보여 줍니다.

이 자습서에서는 다음을 하는 방법을 알아볼 수 있습니다.

  • 인벤토리 보고서 생성
  • Azure Databricks 작업 영역, 클러스터, Notebook 만들기
  • Blob 인벤토리 파일 읽기
  • Blob, 스냅샷 및 버전의 수 및 총 크기 가져오기
  • Blob 형식 및 콘텐츠 형식별 Blob 수 가져오기

필수 조건

인벤토리 보고서 생성

스토리지 계정에 Blob 인벤토리 보고서를 사용하도록 설정. Azure Storage Blob 인벤토리 보고서 사용을 참조하세요.

다음 구성 설정을 사용합니다.

설정
규칙 이름 blobinventory
컨테이너 <컨테이너의 이름>
인벤토리에 대한 개체 유형 Blob
Blob 형식 블록 Blob, 페이지 Blob 및 추가 Blob
하위 유형 Blob 버전 포함, 스냅샷 포함, 삭제된 Blob 포함
Blob 인벤토리 필드 모두
인벤토리 빈도 일간
내보내기 형식 CSV

첫 번째 보고서를 생성하기 위해 인벤토리 보고서를 사용하도록 설정한 후 최대 24시간을 기다려야 할 수 있습니다.

Azure Databricks 구성

이 섹션에서는 Azure Databricks 작업 영역, 클러스터, Notebook을 만듭니다. 이 자습서의 뒷부분에서 코드 조각을 Notebook 셀에 붙여넣은 다음 실행하여 컨테이너 통계를 수집합니다.

  1. Azure Databricks 작업 영역 만들기 Azure Databricks 작업 영역 만들기를 참조하세요.

  2. 클러스터를 만듭니다. 클러스터 만들기를 참조하세요.

  3. Notebook을 만들고 Python을 Notebook의 기본 언어로 선택합니다. Notebook 만들기를 참조하세요.

Blob 인벤토리 파일 읽기

  1. 다음 코드 블록을 복사하여 첫 번째 셀에 붙여넣습니다. 하지만 이 코드를 아직 실행하지 마십시오.

    from pyspark.sql.types import StructType, StructField, IntegerType, StringType
    import pyspark.sql.functions as F  
       storage_account_name = "<storage-account-name>"
       storage_account_key = "<storage-account-key>"
       container = "<container-name>"
       blob_inventory_file = "<blob-inventory-file-name>" 
       hierarchial_namespace_enabled = False
    
    if hierarchial_namespace_enabled == False:
      spark.conf.set("fs.azure.account.key.{0}.blob.core.windows.net".format(storage_account_name), storage_account_key)
      df = spark.read.csv("wasbs://{0}@{1}.blob.core.windows.net/{2}".format(container, storage_account_name, blob_inventory_file), header='true', inferSchema='true')
    
     else:
      spark.conf.set("fs.azure.account.key.{0}.dfs.core.windows.net".format(storage_account_name), storage_account_key)
      df = spark.read.csv("abfss://{0}@{1}.dfs.core.windows.net/{2}".format(container, storage_account_name, blob_inventory_file), header='true', inferSchema='true')     
    
  2. 이 코드 블록에서 다음 값을 바꿉니다.

    • <storage-account-name> 자리 표시자 값을 스토리지 계정 이름으로 바꿉니다.

    • <storage-account-key> 자리 표시자 값을 스토리지 계정 키로 바꿉니다.

    • <container-name> 자리 표시자 값을 인벤토리 보고서를 포함하는 컨테이너로 바꿉니다.

    • <blob-inventory-file-name> 자리 표시자를 인벤토리 파일의 정규화된 이름으로 바꿉니다(예: 2023/02/02/02-16-17/blobinventory/blobinventory_1000000_0.csv).

    • 계정에 계층 구조 네임스페이스가 있는 경우 hierarchical_namespace_enabled 변수를 True로 설정합니다.

  3. 이 블록에서 코드를 실행하려면 SHIFT + ENTER 키를 누릅니다.

Blob 수 및 크기 가져오기

  1. 새 셀에 다음 코드를 붙여넣습니다.

    print("Number of blobs in the container:", df.count())
    print("Number of bytes occupied by blobs in the container:", df.agg({'Content-Length': 'sum'}).first()['sum(Content-Length)'])
    
  2. 셀을 실행하려면 Shift+Enter를 누릅니다.

    Notebook은 컨테이너의 Blob 수와 컨테이너의 Blob에서 차지하는 바이트 수를 표시합니다.

    Screenshot of results that appear when you run the cell showing the number of blobs and the size of blobs in the container.

스냅샷 개수 및 크기 가져오기

  1. 새 셀에 다음 코드를 붙여넣습니다.

    from pyspark.sql.functions import *
    
    print("Number of snapshots in the container:", df.where(~(col("Snapshot")).like("Null")).count())
    dfT = df.where(~(col("Snapshot")).like("Null"))
    print("Number of bytes occupied by snapshots in the container:", dfT.agg({'Content-Length': 'sum'}).first()['sum(Content-Length)'])
    
  2. 셀을 실행하려면 Shift+Enter를 누릅니다.

    Notebook은 스냅샷 수와 Blob 스냅샷이 차지하는 총 바이트 수를 표시합니다.

    Screenshot of results that appear when you run the cell showing the number of snapshots and the total combined size of snapshots.

버전 수 및 크기 가져오기

  1. 새 셀에 다음 코드를 붙여넣습니다.

    from pyspark.sql.functions import *
    
    print("Number of versions in the container:", df.where(~(col("VersionId")).like("Null")).count())
    dfT = df.where(~(col("VersionId")).like("Null"))
    print("Number of bytes occupied by versions in the container:", dfT.agg({'Content-Length': 'sum'}).first()['sum(Content-Length)'])
    
  2. 셀을 실행하려면 Shift+Enter를 누릅니다.

    Notebook은 Blob 버전 수와 Blob 버전에서 차지하는 총 바이트 수를 표시합니다.

    Screenshot of results that appear when you run the cell showing the number of versions and the total combined size of versions.

Blob 유형별 Blob 개수 가져오기

  1. 새 셀에 다음 코드를 붙여넣습니다.

    display(df.groupBy('BlobType').count().withColumnRenamed("count", "Total number of blobs in the container by BlobType"))
    
  2. 셀을 실행하려면 Shift+Enter를 누릅니다.

    Notebook은 형식별로 Blob 형식의 수를 표시합니다.

    Screenshot of results that appear when you run the cell showing the number of blob types by type.

콘텐츠 형식별 Blob 수 가져오기

  1. 새 셀에 다음 코드를 붙여넣습니다.

    display(df.groupBy('Content-Type').count().withColumnRenamed("count", "Total number of blobs in the container by Content-Type"))
    
  2. 셀을 실행하려면 Shift+Enter를 누릅니다.

    Notebook에는 각 콘텐츠 형식과 연결된 Blob 수가 표시됩니다.

    Screenshot of results that appear when you run the cell showing the number of blobs by content-type.

클러스터 종료

불필요한 청구를 방지하려면 클러스터를 종료해야 합니다. 클러스터 종료를 참조하세요.

다음 단계