다음을 통해 공유

使用 .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

以下代码示例演示如何连接到由世纪互联运营的 Microsoft 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 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.chinacloudapi.cn/" + TenantId);
            var tokenAuthResult = authContext.AcquireToken("https://management.core.chinacloudapi.cn/", 
                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 中运行的群集使用的辅助角色节点数,而无需重新创建群集。

注释

只支持使用 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]));
}

提交作业

了解如何提交以下产品的作业:

将数据上传到 Azure Blob 存储

若要上传数据,请参阅将数据上传到 HDInsight