.NET SDK を使用して HDInsight で Apache Hadoop クラスターを管理する
HDInsight .NET SDK を使用して Azure HDInsight クラスターを管理する方法について説明します。
前提条件
この記事を読み始める前に、以下を用意する必要があります。
- アクティブなサブスクリプションが含まれる Azure アカウント。 Azure サブスクリプションをお持ちでない場合は、始める前に無料アカウントを作成してください。
Azure HDInsight への接続
次の NuGet パッケージが必要です。
Install-Package Microsoft.Rest.ClientRuntime.Azure.Authentication -Pre
Install-Package Microsoft.Azure.Management.ResourceManager -Pre
Install-Package Microsoft.Azure.Management.HDInsight
次のコード サンプルは、Azure サブスクリプションで HDInsight クラスターを管理する前に Azure に接続する方法を示しています。
using System;
using Microsoft.Azure;
using Microsoft.Azure.Management.HDInsight;
using Microsoft.Azure.Management.HDInsight.Models;
using Microsoft.Azure.Management.ResourceManager;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.Rest;
using Microsoft.Rest.Azure.Authentication;
namespace HDInsightManagement
{
class Program
{
private static HDInsightManagementClient _hdiManagementClient;
// Replace with your Azure Active Directory tenant ID if necessary.
private const string TenantId = UserTokenProvider.CommonTenantId;
private const string SubscriptionId = "<Your Azure Subscription ID>";
// This is the GUID for the PowerShell client. Used for interactive logins in this example.
private const string ClientId = "1950a258-227b-4e31-a9cf-717495945fc2";
static void Main(string[] args)
{
// Authenticate and get a token
var authToken = Authenticate(TenantId, ClientId, SubscriptionId);
// Flag subscription for HDInsight, if it isn't already.
EnableHDInsight(authToken);
// Get an HDInsight management client
_hdiManagementClient = new HDInsightManagementClient(authToken);
// insert code here
System.Console.WriteLine("Press ENTER to continue");
System.Console.ReadLine();
}
/// <summary>
/// Authenticate to an Azure subscription and retrieve an authentication token
/// </summary>
static TokenCloudCredentials Authenticate(string TenantId, string ClientId, string SubscriptionId)
{
var authContext = new AuthenticationContext("https://login.microsoftonline.com/" + TenantId);
var tokenAuthResult = authContext.AcquireToken("https://management.core.windows.net/",
ClientId,
new Uri("urn:ietf:wg:oauth:2.0:oob"),
PromptBehavior.Always,
UserIdentifier.AnyUser);
return new TokenCloudCredentials(SubscriptionId, tokenAuthResult.AccessToken);
}
/// <summary>
/// Marks your subscription as one that can use HDInsight, if it has not already been marked as such.
/// </summary>
/// <remarks>This is essentially a one-time action; if you have already done something with HDInsight
/// on your subscription, then this isn't needed at all and will do nothing.</remarks>
/// <param name="authToken">An authentication token for your Azure subscription</param>
static void EnableHDInsight(TokenCloudCredentials authToken)
{
// Create a client for the Resource manager and set the subscription ID
var resourceManagementClient = new ResourceManagementClient(new TokenCredentials(authToken.Token));
resourceManagementClient.SubscriptionId = SubscriptionId;
// Register the HDInsight provider
var rpResult = resourceManagementClient.Providers.Register("Microsoft.HDInsight");
}
}
}
このプログラムを実行すると、プロンプトが表示されます。 プロンプトを表示しない場合は、「非対話型認証 .NET HDInsight アプリケーションを作成する」をご覧ください。
クラスターを一覧表示する
次のコード スニペットは、クラスターと一部のプロパティを一覧表示します。
var results = _hdiManagementClient.Clusters.List();
foreach (var name in results.Clusters) {
Console.WriteLine("Cluster Name: " + name.Name);
Console.WriteLine("\t Cluster type: " + name.Properties.ClusterDefinition.ClusterType);
Console.WriteLine("\t Cluster location: " + name.Location);
Console.WriteLine("\t Cluster version: " + name.Properties.ClusterVersion);
}
クラスターの削除
クラスターを同期または非同期で削除するには、次のコード スニペットを使用します。
_hdiManagementClient.Clusters.Delete("<Resource Group Name>", "<Cluster Name>");
_hdiManagementClient.Clusters.DeleteAsync("<Resource Group Name>", "<Cluster Name>");
クラスターのスケール
クラスターのスケーリング機能を使用して、HDInsight 内で実行されているクラスターによって使用されるワーカー ノードの数を、クラスターを再作成することなく変更します。
Note
HDInsight バージョン 3.1.3 以降を使用しているクラスターのみがサポートされます。 クラスターのバージョンがわからない場合は、[プロパティ] ページを確認してください。 詳細については、「クラスターの一覧と表示」を参照してください。
HDInsight でサポートされているクラスターの種類ごとの、データ ノード数を変更した場合の影響:
Apache Hadoop: 保留中または実行中のジョブに影響を与えることなく、実行中の Hadoop クラスター内の worker ノードの数をシームレスに増加できます。 操作の進行中に新しいジョブを送信することもできます。 スケール設定処理の失敗は正常に処理され、クラスターは常に機能状態になります。
データ ノードの数を減らして Hadoop クラスターのスケールを小さくした場合、クラスター内の一部のサービスが再起動されます。 スケーリング操作の完了時に、実行中および保留中のすべてのジョブが失敗します。 操作が終了した後に、ジョブを再送信できます。
Apache HBase: 実行中の HBase クラスターに対して、ノードの追加または削除をシームレスに実行できます。 リージョンのサーバーは、スケーリング処理の完了後、数分以内に自動的にバランスが取られます。 リージョン サーバーのバランスを手動で調整することもできます。 クラスターのヘッド ノードにサインインし、コマンド プロンプト ウィンドウから次のコマンドを実行します。
>pushd %HBASE_HOME%\bin >hbase shell >balancer
HTTP ユーザーの資格情報の更新
更新手順は、HTTP アクセスの許可または取り消しに使用する手順と同じです。 クラスターに HTTP アクセスが許可されている場合は、まずそれを取り消す必要があります。 その後、新しい HTTP ユーザー資格情報を使用してアクセスを許可できます。
既定のストレージ アカウントの検索
次のコード スニペットは、クラスターの既定のストレージ アカウント名とキーを取得する方法を示しています。
var results = _hdiManagementClient.Clusters.GetClusterConfigurations(<Resource Group Name>, <Cluster Name>, "core-site");
foreach (var key in results.Configuration.Keys)
{
Console.WriteLine(String.Format("{0} => {1}", key, results.Configuration[key]));
}
ジョブの送信
次の製品のジョブを送信する方法について説明します。
- MapReduce: HDInsight で MapReduce サンプルを実行する
- Apache Hive: .NET SDK を使用して Apache Hive クエリを実行する
- Apache Sqoop: HDInsight で Apache Sqoop を使用する
- Apache Oozie: HDInsight での Apache Oozie と Hadoop を使用したワークフローの定義と実行
Azure Blob Storage にデータをアップロードする
データをアップロードするには、「HDInsight にデータをアップロードする」を参照してください。