使用 .NET SDK 管理 HDInsight 中的 Apache Hadoop 叢集

了解如何使用 HDInsight.NET SDK管理 HDInsight 叢集。

先決條件

開始閱讀本文之前,您必須符合下列必要條件:

連接至 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,然後才透過 Azure 訂用帳戶管理 HDInsight 叢集。

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 AAD 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>");

擴充叢集

叢集調整功能可讓您變更在 Azure HDInsight 中執行的叢集所用的背景工作節點數目,而不需要重新建立叢集。

注意

只支援使用 HDInsight 3.1.3 版或更高版本的叢集。 如果不確定您的叢集版本,您可以檢查 [屬性] 頁面。 請參閱列出和顯示叢集

變更 HDInsight 支援的每一種叢集所用的資料節點數目會有何影響:

  • Apache Hadoop \(英文\)

    您可以順暢地增加正在執行的 Hadoop 叢集中背景工作節點數目,而不會影響任何擱置或執行中的工作。 您也可以在作業進行當中提交新工作。 系統會順暢處理失敗的調整作業,讓叢集永保正常運作狀態。

    減少資料節點數目以縮減 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 上搭配 Hadoop 使用 Apache Oozie 來定義並執行工作流程

將資料上傳至 Azure Blob 儲存體

請參閱將資料上傳至 HDInsight

另請參閱