Meningkatkan manajemen datastore ke SDK v2

Azure Pembelajaran Mesin Datastores menyimpan informasi koneksi dengan aman ke penyimpanan data Anda di Azure, sehingga Anda tidak perlu mengodekannya dalam skrip Anda. Konsep V2 Datastore sebagian besar tidak berubah dibandingkan dengan V1. Perbedaannya adalah kami tidak akan mendukung sumber data seperti SQL melalui Azure Pembelajaran Mesin Datastores. Kami akan mendukung sumber data seperti SQL melalui fungsionalitas impor&ekspor data Azure Pembelajaran Mesin.

Artikel ini memberikan perbandingan skenario di SDK v1 dan SDK v2.

Membuat datastore dari kontainer Azure Blob melalui account_key

  • SDK v1

    blob_datastore_name='azblobsdk' # Name of the datastore to workspace
    container_name=os.getenv("BLOB_CONTAINER", "<my-container-name>") # Name of Azure blob container
    account_name=os.getenv("BLOB_ACCOUNTNAME", "<my-account-name>") # Storage account name
    account_key=os.getenv("BLOB_ACCOUNT_KEY", "<my-account-key>") # Storage account access key
    
    blob_datastore = Datastore.register_azure_blob_container(workspace=ws, 
                                                             datastore_name=blob_datastore_name, 
                                                             container_name=container_name, 
                                                             account_name=account_name,
                                                             account_key=account_key)
    
  • SDK v2

    from azure.ai.ml.entities import AzureBlobDatastore
    from azure.ai.ml import MLClient
    
    ml_client = MLClient.from_config()
    
    store = AzureBlobDatastore(
        name="blob-protocol-example",
        description="Datastore pointing to a blob container using wasbs protocol.",
        account_name="mytestblobstore",
        container_name="data-container",
        protocol="wasbs",
        credentials={
            "account_key": "XXXxxxXXXxXXXXxxXXXXXxXXXXXxXxxXxXXXxXXXxXXxxxXXxxXXXxXxXXXxxXxxXXXXxxxxxXXxxxxxxXXXxXXX"
        },
    )
    
    ml_client.create_or_update(store)
    

Membuat datastore dari kontainer Azure Blob melalui sas_token

  • SDK v1

    blob_datastore_name='azblobsdk' # Name of the datastore to workspace
    container_name=os.getenv("BLOB_CONTAINER", "<my-container-name>") # Name of Azure blob container
    sas_token=os.getenv("BLOB_SAS_TOKEN", "<my-sas-token>") # Sas token
    
    blob_datastore = Datastore.register_azure_blob_container(workspace=ws, 
                                                             datastore_name=blob_datastore_name, 
                                                             container_name=container_name, 
                                                             sas_token=sas_token)
    
  • SDK v2

    from azure.ai.ml.entities import AzureBlobDatastore
    from azure.ai.ml import MLClient
    
    ml_client = MLClient.from_config()
    
    store = AzureBlobDatastore(
        name="blob-sas-example",
        description="Datastore pointing to a blob container using SAS token.",
        account_name="mytestblobstore",
        container_name="data-container",
        credentials=SasTokenCredentials(
            sas_token= "?xx=XXXX-XX-XX&xx=xxxx&xxx=xxx&xx=xxxxxxxxxxx&xx=XXXX-XX-XXXXX:XX:XXX&xx=XXXX-XX-XXXXX:XX:XXX&xxx=xxxxx&xxx=XXxXXXxxxxxXXXXXXXxXxxxXXXXXxxXXXXXxXXXXxXXXxXXxXX"
        ),
    )
    
    ml_client.create_or_update(store)
    

Membuat datastore dari kontainer Azure Blob melalui autentikasi berbasis identitas

  • SDK v1
blob_datastore = Datastore.register_azure_blob_container(workspace=ws,
                                                      datastore_name='credentialless_blob',
                                                      container_name='my_container_name',
                                                      account_name='my_account_name')

  • SDK v2

    from azure.ai.ml.entities import AzureBlobDatastore
    from azure.ai.ml import MLClient
    
    ml_client = MLClient.from_config()
    
    store = AzureBlobDatastore(
        name="",
        description="",
        account_name="",
        container_name=""
    )
    
    ml_client.create_or_update(store)
    

Mendapatkan datastore dari ruang kerja Anda

  • SDK v1

    # Get a named datastore from the current workspace
    datastore = Datastore.get(ws, datastore_name='your datastore name')
    
    # List all datastores registered in the current workspace
    datastores = ws.datastores
    for name, datastore in datastores.items():
        print(name, datastore.datastore_type)
    
  • SDK v2

    from azure.ai.ml import MLClient
    from azure.identity import DefaultAzureCredential
    
    #Enter details of your Azure Machine Learning workspace
    subscription_id = '<SUBSCRIPTION_ID>'
    resource_group = '<RESOURCE_GROUP>'
    workspace_name = '<AZUREML_WORKSPACE_NAME>'
    
    ml_client = MLClient(credential=DefaultAzureCredential(),
                         subscription_id=subscription_id, 
                         resource_group_name=resource_group,
                         workspace_name=workspace_name)
    
    datastore = ml_client.datastores.get(name='your datastore name')
    

    Nota

    DefaultAzureCredential mencoba beberapa metode autentikasi secara berurutan, termasuk variabel lingkungan, identitas terkelola, Azure CLI, dan banyak lagi. Untuk pengembangan lokal, masuk melalui Azure CLI (az login) atau atur variabel lingkungan AZURE_CLIENT_ID, AZURE_TENANT_ID, dan AZURE_CLIENT_SECRET untuk autentikasi service principal. Untuk informasi selengkapnya, lihat Menyiapkan autentikasi untuk sumber daya dan alur kerja Azure Machine Learning.

Pemetaan fungsionalitas utama di SDK v1 dan SDK v2

Jenis penyimpanan di SDK v1 Jenis penyimpanan di SDK v2
azureml_blob_datastore azureml_blob_datastore
azureml_data_lake_gen1_datastore azureml_data_lake_gen1_datastore
azureml_data_lake_gen2_datastore azureml_data_lake_gen2_datastore
azuremlml_sql_database_datastore Bekerja dengan data (mengimpor data dari sumber daya eksternal)
azuremlml_my_sql_datastore Bekerja dengan data (mengimpor data dari sumber daya eksternal)
azuremlml_postgre_sql_datastore Bekerja dengan data (mengimpor data dari sumber daya eksternal)

Langkah berikutnya

Untuk informasi selengkapnya, lihat: