本文介绍如何使用 HDInsight .NET SDK 以编程方式管理 Azure HDInsight 群集。 SDK 提供了一组客户端库,可让你自动执行群集作,例如直接从 .NET 应用程序创建、列出、删除和缩放。 通过使用 SDK,可以将 HDInsight 群集管理集成到现有的工作流和应用程序中。
先决条件
在开始阅读本文前,必须具备:
- 拥有有效订阅的 Azure 帐户。 如果没有 Azure 订阅,请在开始之前先创建一个免费帐户。
连接到Azure HDInsight
需要以下 NuGet 包:
Install-Package Microsoft.Azure.Management.ResourceManager -Pre
Install-Package Microsoft.Azure.Management.HDInsight
Install-Package Microsoft.Azure.HDInsight.Job -Version 3.0.0-preview.3
以下代码示例演示如何连接到 Azure,然后才能在 Azure 订阅下管理 HDInsight 群集。
using Azure.Identity;
using Microsoft.Azure.HDInsight.Job;
using Microsoft.Azure.Management.HDInsight;
using Microsoft.Azure.Management.ResourceManager;
using Microsoft.Rest;
using System;
using System.Threading.Tasks;
namespace SubmitHDInsightJobDotNet
{
class Program
{
private static HDInsightManagementClient _hdiManagementClient;
// HDInsight Cluster Configuration
private const string ExistingClusterName = "adms-";
// Service Principal Configuration
private const string TenantID = "
private const string ClientID = "";
private const string ClientSecret = "";
private const string SubscriptionID = "";
private const string ResourceGroup = "";
static async Task Main(string[] args)
{
Console.WriteLine("Authenticating with Microsoft Entra...");
await InitializeHDInsightClientAsync();
// Register the HDInsight RP (one-time)
await EnableHDInsightAsync();
// Example: Get cluster details
await GetClusterDetailsAsync();
// Example: Resize cluster (scale to 5 worker nodes)
await ResizeClusterAsync(5);
Console.WriteLine("Press ENTER to continue");
Console.ReadLine();
}
private static async Task InitializeHDInsightClientAsync()
{
var credential = new ClientSecretCredential(TenantID, ClientID, ClientSecret);
var tokenRequestContext = new Azure.Core.TokenRequestContext(new[] { "https://" + "management.core.chinacloudapi.cn/.default" });
var tokenResponse = await credential.GetTokenAsync(tokenRequestContext);
var tokenCredentials = new TokenCredentials(tokenResponse.Token);
_hdiManagementClient = new HDInsightManagementClient(tokenCredentials);
System.Console.WriteLine("HDInsight client initialized successfully with Service Principal authentication.");
}
private static async Task EnableHDInsightAsync()
{
var resourceClient = new Microsoft.Azure.Management.ResourceManager.ResourceManagementClient(
_hdiManagementClient.Credentials
)
{
SubscriptionID = SubscriptionID
};
var result = await resourceClient.Providers.RegisterAsync("Microsoft.HDInsight");
Console.WriteLine($"Provider registration state: {result.RegistrationState}");
}
private static async Task GetClusterDetailsAsync()
{
var resourceClient = new Microsoft.Azure.Management.HDInsight.HDInsightManagementClient(
_hdiManagementClient.Credentials
)
{
SubscriptionID = SubscriptionID
};
var cluster = await resourceClient.Clusters.GetAsync(ResourceGroup, ExistingClusterName);
Console.WriteLine($"Cluster Name: {cluster.Name}");
Console.WriteLine($"Cluster Type: {cluster.Properties.ClusterDefinition.Kind}");
Console.WriteLine($"Cluster State: {cluster.Properties.ClusterState}");
Console.WriteLine($"Worker Nodes: {cluster.Properties.ComputeProfile.Roles[1].TargetInstanceCount}");
}
private static async Task ResizeClusterAsync(int targetWorkerNodes)
{
Console.WriteLine($"Resizing cluster {ExistingClusterName} to {targetWorkerNodes} worker nodes...");
await _hdiManagementClient.Clusters.ResizeAsync(ResourceGroup, ExistingClusterName, targetWorkerNodes);
Console.WriteLine("Resize request submitted. Use GetClusterDetailsAsync to check progress.");
}
}
}
运行此程序时会看到提示。 若不想看到提示,请参阅创建非交互式身份验证 .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 版本 5.1 或更高版本的群集。 如果不确定群集的版本,请检查 “属性” 页。 有关详细信息,请参阅 列表和显示群集。
更改 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]));
}