Uruchamianie zapytań apache Hive przy użyciu zestawu SDK platformy .NET usługi HDInsight

Dowiedz się, jak przesyłać zapytania apache Hive przy użyciu zestawu SDK platformy .NET usługi HDInsight. Napiszesz program w języku C#, aby przesłać zapytanie hive dotyczące wyświetlania listy tabel programu Hive i wyświetlania wyników.

Uwaga

Kroki opisane w tym artykule należy wykonać z klienta systemu Windows. Aby uzyskać informacje na temat korzystania z klienta systemu Linux, OS X lub Unix do pracy z programem Hive, użyj selektora kart pokazanego w górnej części artykułu.

Wymagania wstępne

Przed rozpoczęciem tego artykułu musisz mieć następujące elementy:

  • Klaster Apache Hadoop w usłudze HDInsight. Zobacz Wprowadzenie do korzystania z platformy Hadoop opartej na systemie Linux w usłudze HDInsight.

    Ważne

    Od 15 września 2017 r. zestaw .NET SDK usługi HDInsight obsługuje tylko zwracanie wyników zapytań hive z kont usługi Azure Storage. Jeśli używasz tego przykładu z klastrem usługi HDInsight, który używa usługi Azure Data Lake Storage jako magazynu podstawowego, nie możesz pobrać wyników wyszukiwania przy użyciu zestawu SDK platformy .NET.

  • Program Visual Studio 2013 lub nowszy. Należy zainstalować co najmniej programowanie aplikacji klasycznych .NET na platformie .NET.

Uruchamianie zapytania Hive

Zestaw .NET SDK usługi HDInsight udostępnia biblioteki klienckie platformy .NET, co ułatwia pracę z klastrami usługi HDInsight z platformy .NET.

  1. Utwórz aplikację konsolową języka C# w programie Visual Studio.

  2. W konsoli Menedżer pakietów Nuget uruchom następujące polecenie:

    Install-Package Microsoft.Azure.Management.HDInsight.Job
    
  3. Edytuj poniższy kod, aby zainicjować wartości zmiennych: ExistingClusterName, ExistingClusterUsername, ExistingClusterPassword,DefaultStorageAccountName,DefaultStorageAccountKey,DefaultStorageContainerName. Następnie użyj poprawionego kodu jako całej zawartości Program.cs w programie Visual Studio.

    using System.Collections.Generic;
    using System.IO;
    using System.Text;
    using System.Threading;
    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 ExistingClusterUsername = "<Cluster Username>";
            private const string ExistingClusterPassword = "<Cluster User Password>";
    
            // Only Azure Storage accounts are supported by the SDK
            private const string DefaultStorageAccountName = "<Default Storage Account Name>";
            private const string DefaultStorageAccountKey = "<Default Storage Account Key>";
            private const string DefaultStorageContainerName = "<Default Blob Container Name>";
    
            private const string ExistingClusterUri = ExistingClusterName + ".azurehdinsight.net";
    
            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);
    
                SubmitHiveJob();
    
                System.Console.WriteLine("Press ENTER to continue ...");
                System.Console.ReadLine();
            }
    
            private static void SubmitHiveJob()
            {
                Dictionary<string, string> defines = new Dictionary<string, string> { { "hive.execution.engine", "tez" }, { "hive.exec.reducers.max", "1" } };
                List<string> args = new List<string> { { "argA" }, { "argB" } };
                var parameters = new HiveJobSubmissionParameters
                {
                    Query = "SHOW TABLES",
                    Defines = defines,
                    Arguments = args
                };
    
                System.Console.WriteLine("Submitting the Hive job to the cluster...");
                var jobResponse = _hdiJobManagementClient.JobManagement.SubmitHiveJob(parameters);
                var jobId = jobResponse.JobSubmissionJsonResponse.Id;
                System.Console.WriteLine("Response status code is " + jobResponse.StatusCode);
                System.Console.WriteLine("JobId is " + jobId);
    
                System.Console.WriteLine("Waiting for the job completion ...");
    
                // Wait for job completion
                var jobDetail = _hdiJobManagementClient.JobManagement.GetJob(jobId).JobDetail;
                while (!jobDetail.Status.JobComplete)
                {
                    Thread.Sleep(1000);
                    jobDetail = _hdiJobManagementClient.JobManagement.GetJob(jobId).JobDetail;
                }
    
                // Get job output
                var storageAccess = new AzureStorageAccess(DefaultStorageAccountName, DefaultStorageAccountKey,
                    DefaultStorageContainerName);
                var output = (jobDetail.ExitValue == 0)
                    ? _hdiJobManagementClient.JobManagement.GetJobOutput(jobId, storageAccess) // fetch stdout output in case of success
                    : _hdiJobManagementClient.JobManagement.GetJobErrorLogs(jobId, storageAccess); // fetch stderr output in case of failure
    
                System.Console.WriteLine("Job output is: ");
    
                using (var reader = new StreamReader(output, Encoding.UTF8))
                {
                    string value = reader.ReadToEnd();
                    System.Console.WriteLine(value);
                }
            }
        }
    }
    
  4. Naciśnij klawisz F5, aby uruchomić aplikację.

Dane wyjściowe aplikacji powinny być podobne do następujących:

HDInsight Hadoop Hive job output.

Następne kroki

W tym artykule przedstawiono sposób przesyłania zapytań apache Hive przy użyciu zestawu SDK platformy .NET usługi HDInsight. Więcej informacji można znaleźć w następujących artykułach: