了解如何在 HDInsight 上使用 Azure HDInsight .NET SDK 執行 Apache Sqoop 作業,以進行 HDInsight 叢集與 Azure SQL 資料庫或 SQL Server 資料庫之間的匯入和匯出作業。
必要條件
熟悉 Sqoop。 如需詳細資訊,請參閱 Sqoop 使用者指南。
使用 .NET SDK 在 HDInsight 叢集上使用 Sqoop
HDInsight .NET SDK 提供 .NET 用戶端程式庫,讓您輕鬆地從 .NET 使用 HDInsight 叢集。 在本節中,您會建立 C# 主控台應用程式,將 hivesampletable
匯出至您在先決條件中建立的 Azure SQL Database 資料表。
設定
啟動 Visual Studio,建立一個 C# 主控台應用程式。
瀏覽至 [工具]>[NuGet 套件管理員]>[套件管理員主控台],然後執行下列命令:
Install-Package Microsoft.Azure.Management.HDInsight.Job
Sqoop export
從 Hive 到 SQL Server。 此範例會將 Hive hivesampletable
資料表的資料匯出至 SQL Database 中的 mobiledata
資料表。
在 Program.cs 檔案中使用下列程式碼。 編輯程式碼以設定
ExistingClusterName
和ExistingClusterPassword
的值。using Microsoft.Azure.Management.HDInsight.Job; using Microsoft.Azure.Management.HDInsight.Job.Models; using Hyak.Common; namespace SubmitHDInsightJobDotNet { class Program { private static HDInsightJobManagementClient _hdiJobManagementClient; private const string ExistingClusterName = "<Your HDInsight Cluster Name>"; private const string ExistingClusterPassword = "<Cluster User Password>"; private const string ExistingClusterUri = ExistingClusterName + ".azurehdinsight.net"; private const string ExistingClusterUsername = "admin"; static void Main(string[] args) { System.Console.WriteLine("The application is running ..."); var clusterCredentials = new BasicAuthenticationCloudCredentials { Username = ExistingClusterUsername, Password = ExistingClusterPassword }; _hdiJobManagementClient = new HDInsightJobManagementClient(ExistingClusterUri, clusterCredentials); SubmitSqoopJob(); System.Console.WriteLine("Press ENTER to continue ..."); System.Console.ReadLine(); } private static void SubmitSqoopJob() { var sqlDatabaseServerName = ExistingClusterName + "dbserver"; var sqlDatabaseLogin = "sqluser"; var sqlDatabaseLoginPassword = ExistingClusterPassword; var sqlDatabaseDatabaseName = ExistingClusterName + "db"; // Connection string for using Azure SQL Database; Comment if using SQL Server var connectionString = "jdbc:sqlserver://" + sqlDatabaseServerName + ".database.windows.net;user=" + sqlDatabaseLogin + "@" + sqlDatabaseServerName + ";password=" + sqlDatabaseLoginPassword + ";database=" + sqlDatabaseDatabaseName; // Connection string for using SQL Server; Uncomment if using SQL Server // var connectionString = "jdbc:sqlserver://" + sqlDatabaseServerName + ";user=" + sqlDatabaseLogin + ";password=" + sqlDatabaseLoginPassword + ";database=" + sqlDatabaseDatabaseName; //sqoop start var tableName = "mobiledata"; var parameters = new SqoopJobSubmissionParameters { Command = "export --connect " + connectionString + " --table " + tableName + " --hcatalog-table hivesampletable" }; //sqoop end System.Console.WriteLine("Submitting the Sqoop job to the cluster..."); var response = _hdiJobManagementClient.JobManagement.SubmitSqoopJob(parameters); System.Console.WriteLine("Validating that the response is as expected..."); System.Console.WriteLine("Response status code is " + response.StatusCode); System.Console.WriteLine("Validating the response object..."); System.Console.WriteLine("JobId is " + response.JobSubmissionJsonResponse.Id); } } }
若要執行程式,請選取 F5 鍵。
Sqoop import
從 SQL Server 到 Azure 儲存體。 此範例相依於已執行上述匯出。 此範例會將資料從 SQL Database 中的 mobiledata
資料表匯入叢集預設儲存體帳戶中的 wasb:///tutorials/usesqoop/importeddata
目錄。
使用下列程式碼取代
//sqoop start //sqoop end
區塊中的上述程式碼:var tableName = "mobiledata"; var exportDir = "/tutorials/usesqoop/importeddata"; var parameters = new SqoopJobSubmissionParameters { Command = "import --connect " + connectionString + " --table " + tableName + " --target-dir " + exportDir + " --fields-terminated-by \\t --lines-terminated-by \\n -m 1" };
若要執行程式,請選取 F5 鍵。
限制
以 Linux 為基礎的 HDInsight 存在下列限制:
大量匯出:用來將資料匯出至 Microsoft SQL Server 或 Azure SQL Database 的 Sqoop 連接器目前不支援大量插入。
批次:藉由使用
-batch
參數,Sqoop 會執行多個插入,而不是對插入作業進行批次處理。
下一步
現在,您已了解如何使用 Sqoop。 若要深入了解,請參閱:
- 搭配使用 Apache Oozie 與 HDInsight:在 Oozie 工作流程中使用 Sqoop 動作。
- 將資料上傳至 HDInsight:尋找其他方法將資料上傳至 HDInsight 或 Azure Blob 儲存體。