使用 .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,然後才透過 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.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 中執行的叢集所使用的背景工作節點數目,而不需要重新建立叢集。
注意
只支援使用 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:搭配 Hadoop 使用 Apache Oozie 在 HDInsight 中定義和執行工作流程
將資料上傳至 Azure Blob 儲存體
若要上傳數據,請參閱 將數據上傳至 HDInsight。