Eseguire processi Apache Sqoop con .NET SDK per Apache Hadoop in HDInsight

Informazioni su come usare Azure HDInsight .NET SDK per eseguire processi Apache Sqoop in HDInsight per importare ed esportare tra un cluster HDInsight e un database Azure SQL o SQL Server.

Prerequisiti

Usare Sqoop nei cluster HDInsight con .NET SDK

HDInsight .NET SDK fornisce librerie client .NET che semplificano l'uso dei cluster HDInsight da .NET. In questa sezione viene creata un'applicazione console C# per esportare nella hivesampletable tabella di database Azure SQL creata dai prerequisiti.

Configurazione

  1. Avviare Visual Studio e creare un'applicazione console C#.

  2. Passare a Strumenti> console diGestione pacchetti>NuGetEd eseguire il comando seguente:

    Install-Package Microsoft.Azure.Management.HDInsight.Job
    

Esportazione con Sqoop

Da Hive a SQL Server. Questo esempio mostra come esportare i dati dalla tabella Hive hivesampletable alla tabella del database SQL mobiledata.

  1. Usare il codice seguente nel file Program.cs. Modificare il codice per impostare i valori per ExistingClusterNamee 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);
            }
        }
    }
    
  2. Per eseguire il programma, premere F5.

Importazione con Sqoop

Da SQL Server ad Archiviazione di Azure. Questo esempio dipende dall'esportazione precedente eseguita. Questo esempio importa i dati dalla mobiledata tabella in database SQL alla wasb:///tutorials/usesqoop/importeddata directory nell'account di archiviazione predefinito del cluster.

  1. Sostituire il codice precedente nel //sqoop start //sqoop end blocco con il codice seguente:

    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"
    };
    
  2. Per eseguire il programma, premere F5.

Limitazioni

HDInsight basato su Linux prevede le limitazioni seguenti:

  • Esportazione in blocco: il connettore Sqoop usato per esportare dati in Microsoft SQL Server o nel database SQL di Azure non supporta al momento gli inserimenti in blocco.

  • Invio in batch: usando l'opzione -batch , Sqoop esegue più inserimenti anziché eseguire l'invio in batch delle operazioni di inserimento.

Passaggi successivi

In questa esercitazione si è appreso come usare Sqoop. Per altre informazioni, vedere: