Python 用の HDInsight SDK

概要

Python 用の HDInsight SDK には、HDInsight クラスターの管理に使用できるクラスとメソッドが用意されています。 これには、スクリプト アクションを作成、削除、更新、一覧表示、サイズ変更、実行したり、HDInsight クラスターのプロパティを監視、取得したりする操作が含まれます。

前提条件

SDK のインストール

Python 用の HDInsight SDK は、Python Package Index にあり、次のコマンドを実行してインストールできます。

pip install azure-mgmt-hdinsight

認証

SDK は最初に Azure サブスクリプションで認証する必要があります。 以下の例に従って、サービス プリンシパルを作成し、これを使用して認証します。 その後、HDInsightManagementClient のインスタンスが生成されます。これには、管理操作の実行に使用できるメソッドが多数含まれています (以下のセクションで説明します)。

Note

認証方法は以下の例の他にもあり、そちらの方がご自身のニーズに適している可能性もあります。 すべての方法については、「Python 用 Azure 管理ライブラリを使用した認証」をご覧ください。

サービス プリンシパルを使用した認証の例

まず、Azure Cloud Shell にログインします。 現在、サービス プリンシパル作成対象のサブスクリプションを使用していることを確認します。

az account show

ご自身のサブスクリプション情報が JSON として表示されます。

{
  "environmentName": "AzureCloud",
  "id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
  "isDefault": true,
  "name": "XXXXXXX",
  "state": "Enabled",
  "tenantId": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
  "user": {
    "cloudShellID": true,
    "name": "XXX@XXX.XXX",
    "type": "user"
  }
}

正しいサブスクリプションにログインしていない場合は、以下を実行して正しいサブスクリプションを選択します。

az account set -s <name or ID of subscription>

重要

他の方法で、たとえば Azure portal で HDInsight クラスターを作成する、といった方法で HDInsight リソース プロバイダーをまだ登録していない場合は、認証の前にこれを一度実行する必要があります。 これを Azure Cloud Shell から実行するには、次のコマンドを実行します。

az provider register --namespace Microsoft.HDInsight

次に、ご自身のサービス プリンシパルの名前を選択し、次のコマンドを使用して作成します。

az ad sp create-for-rbac --name <Service Principal Name> --role Contributor --sdk-auth

サービス プリンシパル情報が JSON として表示されます。

{
  "clientId": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
  "clientSecret": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
  "subscriptionId": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
  "tenantId": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
  "activeDirectoryEndpointUrl": "https://login.microsoftonline.com",
  "resourceManagerEndpointUrl": "https://management.azure.com/",
  "activeDirectoryGraphResourceId": "https://graph.windows.net/",
  "sqlManagementEndpointUrl": "https://management.core.windows.net:8443/",
  "galleryEndpointUrl": "https://gallery.azure.com/",
  "managementEndpointUrl": "https://management.core.windows.net/"
}

次の Python スニペットをコピーし、コマンド実行後に返された JSON の文字列を TENANT_IDCLIENT_IDCLIENT_SECRETSUBSCRIPTION_ID に入力して、サービス プリンシパルを作成します。

from azure.mgmt.hdinsight import HDInsightManagementClient
from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.hdinsight.models import *

# Tenant ID for your Azure Subscription
TENANT_ID = ''
# Your Service Principal App Client ID
CLIENT_ID = ''
# Your Service Principal Client Secret
CLIENT_SECRET = ''
# Your Azure Subscription ID
SUBSCRIPTION_ID = ''

credentials = ServicePrincipalCredentials(
    client_id = CLIENT_ID,
    secret = CLIENT_SECRET,
    tenant = TENANT_ID
)

client = HDInsightManagementClient(credentials, SUBSCRIPTION_ID)

クラスターの管理

Note

このセクションでは、HDInsightManagementClient インスタンスの認証と構成が既に完了していること、および client と呼ばれる変数にそのインスタンスが格納されていることを前提としています。 HDInsightManagementClient を認証および取得する方法については、上記の「認証」セクションを参照してください。

クラスターの作成

新しいクラスターを作成するには、client.clusters.create() を呼び出します。

サンプル

いくつかの一般的な種類の HDInsight クラスターを作成するためのコード サンプルとして、 HDInsight Python サンプルがあります。

この例は、2 つのヘッド ノードと 1 つの worker ノードを含む Spark クラスターを作成する方法を示しています。

Note

次に示すように、最初にリソース グループとストレージ アカウントを作成する必要があります。 これらが既に作成済みの場合、この手順はスキップできます。

リソース グループの作成

Azure Cloud Shell を使用して次を実行することで、リソース グループを作成できます

az group create -l <Region Name (i.e. eastus)> --n <Resource Group Name>
ストレージ アカウントの作成

Azure Cloud Shell を使用して次を実行することで、ストレージ アカウントを作成できます。

az storage account create -n <Storage Account Name> -g <Existing Resource Group Name> -l <Region Name (i.e. eastus)> --sku <SKU i.e. Standard_LRS>

ここで、次のコマンドを実行して、ストレージ アカウントに対するキーを取得します (これはクラスターを作成するときに必要になります)。

az storage account keys list -n <Storage Account Name>

次の Python スニペットでは、2 つのヘッド ノードと 1 つの worker ノードを含む Spark クラスターを作成します。 コメントの説明に従って空白の変数を入力します。また、ご自身のニーズに合わせて他のパラメーターを変更します。

# The name for the cluster you are creating
cluster_name = ""
# The name of your existing Resource Group
resource_group_name = ""
# Choose a username
username = ""
# Choose a password
password = ""
# Replace <> with the name of your storage account
storage_account = "<>.blob.core.windows.net"
# Storage account key you obtained above
storage_account_key = ""
# Choose a region
location = ""
container = "default"

params = ClusterCreateProperties(
    cluster_version="3.6",
    os_type=OSType.linux,
    tier=Tier.standard,
    cluster_definition=ClusterDefinition(
        kind="spark",
        configurations={
            "gateway": {
                "restAuthCredential.enabled_credential": "True",
                "restAuthCredential.username": username,
                "restAuthCredential.password": password
            }
        }
    ),
    compute_profile=ComputeProfile(
        roles=[
            Role(
                name="headnode",
                target_instance_count=2,
                hardware_profile=HardwareProfile(vm_size="Large"),
                os_profile=OsProfile(
                    linux_operating_system_profile=LinuxOperatingSystemProfile(
                        username=username,
                        password=password
                    )
                )
            ),
            Role(
                name="workernode",
                target_instance_count=1,
                hardware_profile=HardwareProfile(vm_size="Large"),
                os_profile=OsProfile(
                    linux_operating_system_profile=LinuxOperatingSystemProfile(
                        username=username,
                        password=password
                    )
                )
            )
        ]
    ),
    storage_profile=StorageProfile(
        storageaccounts=[StorageAccount(
            name=storage_account,
            key=storage_account_key,
            container=container,
            is_default=True
        )]
    )
)

client.clusters.create(
    cluster_name=cluster_name,
    resource_group_name=resource_group_name,
    parameters=ClusterCreateParametersExtended(
        location=location,
        tags={},
        properties=params
    ))

クラスターの詳細の取得

特定のクラスターのプロパティを取得するには:

client.clusters.get("<Resource Group Name>", "<Cluster Name>")

get を使用すると、ご自身のクラスターを適切に作成できたことを確認できます。

my_cluster = client.clusters.get("<Resource Group Name>", "<Cluster Name>")
print(my_cluster)

出力は次のようになります。

{'additional_properties': {}, 'id': '/subscriptions/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/resourceGroups/<Resource Group Name>/providers/Microsoft.HDInsight/clusters/<Cluster Name>', 'name': '<Cluster Name>', 'type': 'Microsoft.HDInsight/clusters', 'location': '<Location>', 'tags': {}, 'etag': 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX', 'properties': <azure.mgmt.hdinsight.models.cluster_get_properties_py3.ClusterGetProperties object at 0x0000013766D68048>}

クラスターの一覧表示

サブスクリプションのクラスターの一覧表示

client.clusters.list()

リソース グループ別のクラスターの一覧表示

client.clusters.list_by_resource_group("<Resource Group Name>")

Note

list()list_by_resource_group() の両方が ClusterPaged オブジェクトを返します。 advance_page() を呼び出すと、そのページ上にクラスターの一覧が返され、ClusterPaged オブジェクトが次のページに進められます。 これは、StopIteration 例外が発生し、それ以上ページがないことが示されるまで繰り返されます。

次の例では、現在のサブスクリプションのすべてのクラスターのプロパティが出力されます。

clusters_paged = client.clusters.list()
while True:
  try:
    for cluster in clusters_paged.advance_page():
      print(cluster)
  except StopIteration: 
    break

クラスターの削除

クラスターを削除するには:

client.clusters.delete("<Resource Group Name>", "<Cluster Name>")

クラスター タグの更新

次のように、特定のクラスターのタグを更新できます。

client.clusters.update("<Resource Group Name>", "<Cluster Name>", tags={<Dictionary of Tags>})

client.clusters.update("<Resource Group Name>", "<Cluster Name>", tags={"tag1Name" : "tag1Value", "tag2Name" : "tag2Value"})

クラスターのサイズ変更

特定のクラスターの worker ノード数をサイズ変更するには、次のように新しいサイズを指定します。

client.clusters.resize("<Resource Group Name>", "<Cluster Name>", target_instance_count=<Num of Worker Nodes>)

クラスターの監視

HDInsight 管理 SDK を使用して、Operations Management Suite (OMS) でご自身のクラスターの監視を管理することもできます。

OMS 監視の有効化

Note

OMS の監視を有効にするには、既存の Log Analytics ワークスペースが必要です。 まだ作成していない場合、その方法については、「Azure ポータルで Log Analytics ワークスペースを作成する」を参照してください。

ご自身のクラスターで OMS 監視を有効にするには:

client.extension.enable_monitoring("<Resource Group Name>", "<Cluster Name>", workspace_id="<Workspace Id>")

OMS 監視の状態の表示

ご自身のクラスターに対する OMS の状態を取得するには:

client.extension.get_monitoring_status("<Resource Group Name", "Cluster Name")

OMS 監視の無効化

ご自身のクラスターに対する OMS を無効にするには:

client.extension.disable_monitoring("<Resource Group Name>", "<Cluster Name>")

[スクリプト操作]

HDInsight には、クラスターをカスタマイズするためにカスタム スクリプトを呼び出すスクリプト アクションという構成方法があります。

Note

スクリプト アクションを使用する方法の詳細については、「スクリプト アクションを使用して Linux ベースの HDInsight クラスターをカスタマイズする」を参照してください

スクリプト アクションの実行

特定のクラスターに対してスクリプト アクションを実行するには:

script_action1 = RuntimeScriptAction(name="<Script Name>", uri="<URL To Script>", roles=[<List of Roles>]) #valid roles are "headnode", "workernode", "zookeepernode", and "edgenode"

client.clusters.execute_script_actions("<Resource Group Name>", "<Cluster Name>", <persist_on_success (bool)>, script_actions=[script_action1]) #add more RuntimeScriptActions to the list to execute multiple scripts

スクリプト アクションの削除

特定のクラスターに対して指定した保存済みスクリプト アクションを削除するには:

client.script_actions.delete("<Resource Group Name>", "<Cluster Name", "<Script Name>")

保存済みスクリプト アクションの一覧表示

Note

list()list_persisted_scripts()RuntimeScriptActionDetailPaged オブジェクトを返します。 advance_page() を呼び出すと、そのページ上に RuntimeScriptActionDetailPaged の一覧が返され、RuntimeScriptActionDetail オブジェクトが次のページに進められます。 これは、StopIteration 例外が発生し、それ以上ページがないことが示されるまで繰り返されます。 次の例を見てください。

指定したクラスターに対する保存済みスクリプト アクションを一覧表示するには:

client.script_actions.list_persisted_scripts("<Resource Group Name>", "<Cluster Name>")

scripts_paged = client.script_actions.list_persisted_scripts(resource_group_name, cluster_name)
while True:
  try:
    for script in scripts_paged.advance_page():
      print(script)
  except StopIteration:
    break

スクリプトの全実行履歴の一覧表示

指定したクラスターに対するスクリプトの実行履歴をすべて一覧表示するには:

client.script_execution_history.list("<Resource Group Name>", "<Cluster Name>")

この例では、過去のすべてのスクリプト実行の詳細が出力されます。

script_executions_paged = client.script_execution_history.list("<Resource Group Name>", "<Cluster Name>")
while True:
  try:
    for script in script_executions_paged.advance_page():            
      print(script)
    except StopIteration:       
      break