Poznámka:
Přístup k této stránce vyžaduje autorizaci. Můžete se zkusit přihlásit nebo změnit adresáře.
Přístup k této stránce vyžaduje autorizaci. Můžete zkusit změnit adresáře.
This article demonstrates how to develop Apache Spark applications on Azure HDInsight using the Azure Toolkit plug-in for the IntelliJ IDE. Azure HDInsight is a managed, open-source analytics service in the cloud. The service allows you to use open-source frameworks like Hadoop, Apache Spark, Apache Hive, and Apache Kafka.
You can use the Azure Toolkit plug-in in a few ways:
- Develop and submit a Scala Spark application to an HDInsight Spark cluster.
- Access your Azure HDInsight Spark cluster resources.
- Vyvíjejte a spouštějte aplikaci Scala Spark místně.
V tomto článku se naučíte:
- Použití pluginu Azure Toolkit for IntelliJ
- Vývoj aplikací Apache Spark
- Submit an application to Azure HDInsight cluster
Požadavky
Cluster Apache Spark ve službě HDInsight. Pokyny najdete v tématu Vytváření clusterů Apache Spark ve službě Azure HDInsight. Only HDInsight clusters in public cloud are supported while other secure cloud types (e.g. government clouds) are not.
Oracle Java Development Kit. This article uses Java version 8.0.202.
IntelliJ IDEA Tento článek používá IntelliJ IDEA Community 2018.3.4.
Azure Toolkit for IntelliJ. Viz Instalace sady Azure Toolkit for IntelliJ.
Instalace modulu plug-in Scala pro IntelliJ IDEA
Steps to install the Scala plugin:
Otevřete IntelliJ IDEA.
Na úvodní obrazovce přejděte na Konfigurovat>Pluginy a otevřete okno Pluginy.
Vyberte Nainstalovat pro plugin Scala, který je zobrazen v novém okně.
Po úspěšné instalaci modulu plug-in je nutné restartovat integrované vývojové prostředí (IDE).
Create a Spark Scala application for an HDInsight Spark cluster
Spusťte IntelliJ IDEA a výběrem možnosti Vytvořit nový projekt otevřete okno Nový projekt.
V levém podokně vyberte Azure Spark/HDInsight .
V hlavním okně vyberte Spark Project (Scala ).
From the Build tool drop-down list, select one of the following options:
Maven for Scala project-creation wizard support.
SBT pro správu závislostí a sestavování pro projekt Scala.
Vyberte Další.
V okně Nový projekt zadejte následující informace:
Vlastnictví Popis Název projektu Zadejte název. Tento článek používá myApp.Umístění projektu Zadejte umístění pro uložení projektu. Project SDK This field might be blank on your first use of IDEA. Select New... and navigate to your JDK. Spark Version Průvodce vytvořením integruje správnou verzi sady Spark SDK a Scala SDK. Pokud je verze clusteru Spark nižší než 2.0, vyberte Spark 1.x. V opačném případě vyberte Spark2.x. V tomto příkladu se používá Spark 2.3.0 (Scala 2.11.8).
Vyberte Dokončit. Než bude projekt dostupný, může to trvat několik minut.
Projekt Spark automaticky vytvoří artefakt za vás. To view the artifact, do the following steps:
a. V řádku nabídek přejděte na Soubor>Struktura projektu....
b) V okně Struktura projektu vyberte Artefakty.
c. Po zobrazení artefaktu vyberte Zrušit .
Add your application source code by doing the following steps:
a. From Project, navigate to myApp>src>main>scala.
b) Right-click scala, and then navigate to New>Scala Class.
c. In the Create New Scala Class dialog box, provide a name, select Object in the Kind drop-down list, and then select OK.
d. The myApp.scala file then opens in the main view. Replace the default code with the code found below:
import org.apache.spark.SparkConf import org.apache.spark.SparkContext object myApp{ def main (arg: Array[String]): Unit = { val conf = new SparkConf().setAppName("myApp") val sc = new SparkContext(conf) val rdd = sc.textFile("wasbs:///HdiSamples/HdiSamples/SensorSampleData/hvac/HVAC.csv") //find the rows that have only one digit in the seventh column in the CSV file val rdd1 = rdd.filter(s => s.split(",")(6).length() == 1) rdd1.saveAsTextFile("wasbs:///HVACOut") } }The code reads the data from HVAC.csv (available on all HDInsight Spark clusters), retrieves the rows that have only one digit in the seventh column in the CSV file, and writes the output to
/HVACOutunder the default storage container for the cluster.
Connect to your HDInsight cluster
User can either sign in to your Azure subscription, or link a HDInsight cluster. Use the Ambari username/password or domain joined credential to connect to your HDInsight cluster.
Přihlaste se ke svému předplatnému Azure.
From the menu bar, navigate to View>Tool Windows>Azure Explorer.
V Azure Exploreru klikněte pravým tlačítkem na uzel Azure a pak vyberte Přihlásit se.
V dialogovém okně Azure Sign In (Přihlášení k Azure) zvolte Device Login (Přihlášení zařízení) a pak vyberte Sign in (Přihlásit se).
In the Azure Device Login dialog box, click Copy&Open.
In the browser interface, paste the code, and then click Next.
Zadejte své přihlašovací údaje Azure a zavřete prohlížeč.
Po přihlášení se v dialogovém okně Vybrat předplatná zobrazí seznam všech předplatných Azure přidružených k přihlašovacím údajům. Select your subscription and then select the Select button.
From Azure Explorer, expand HDInsight to view the HDInsight Spark clusters that are in your subscriptions.
To view the resources (for example, storage accounts) that are associated with the cluster, you can further expand a cluster-name node.
Link a cluster
You can link an HDInsight cluster by using the Apache Ambari managed username. Podobně můžete propojit cluster HDInsight připojený k doméně pomocí domény a uživatelského jména, například user1@contoso.com. Also you can link Livy Service cluster.
From the menu bar, navigate to View>Tool Windows>Azure Explorer.
From Azure Explorer, right-click the HDInsight node, and then select Link A Cluster.
The available options in the Link A Cluster window will vary depending on which value you select from the Link Resource Type drop-down list. Enter your values and then select OK.
HDInsight Cluster
Vlastnictví Hodnota Link Resource Type Select HDInsight Cluster from the drop-down list. Cluster Name/URL Enter cluster name. Typ ověřování Leave as Basic Authentication Uživatelské jméno Enter cluster user name, default is admin. Heslo Enter password for user name.
Livy Service
Vlastnictví Hodnota Link Resource Type Select Livy Service from the drop-down list. Livy Endpoint Enter Livy Endpoint Název clusteru Enter cluster name. Yarn Endpoint Volitelný. Typ ověřování Leave as Basic Authentication Uživatelské jméno Enter cluster user name, default is admin. Heslo Enter password for user name.
You can see your linked cluster from the HDInsight node.
You also can unlink a cluster from Azure Explorer.
Run a Spark Scala application on an HDInsight Spark cluster
After creating a Scala application, you can submit it to the cluster.
V Projectu přejděte na myApp>src>main>scala>myApp. Right-click myApp, and select Submit Spark Application (It will likely be located at the bottom of the list).
In the Submit Spark Application dialog window, select 1. Spark on HDInsight.
In the Edit configuration window, provide the following values and then select OK:
Vlastnictví Hodnota Spark clusters (Linux only) Select the HDInsight Spark cluster on which you want to run your application. Výběr artefaktu k odeslání Ponechte výchozí nastavení. Název hlavní třídy Výchozí hodnota je hlavní třída z vybraného souboru. You can change the class by selecting the ellipsis(...) and choosing another class. Konfigurace úloh You can change the default keys and, or values. Další informace najdete v tématu Apache Livy REST API. Argumenty příkazového řádku V případě potřeby můžete zadat argumenty oddělené mezerou pro hlavní třídu. Referenced Jars and Referenced Files You can enter the paths for the referenced Jars and files if any. You can also browse files in the Azure virtual file system, which currently only supports ADLS Gen 2 cluster. For more information: Apache Spark Configuration. See also, How to upload resources to cluster. Úložiště pro nahrání úloh Rozbalením zobrazíte další možnosti. Typ úložiště Select Use Azure Blob to upload from the drop-down list. Účet úložiště Zadejte svůj účet úložiště. Klíč úložiště Zadejte svůj klíč úložiště. Úložný kontejner Po zadání účtu úložiště a klíče úložiště vyberte kontejner úložiště z rozevíracího seznamu.
Select SparkJobRun to submit your project to the selected cluster. The Remote Spark Job in Cluster tab displays the job execution progress at the bottom. You can stop the application by clicking the red button.
Debug Apache Spark applications locally or remotely on an HDInsight cluster
We also recommend another way of submitting the Spark application to the cluster. You can do so by setting the parameters in the Run/Debug configurations IDE. See Debug Apache Spark applications locally or remotely on an HDInsight cluster with Azure Toolkit for IntelliJ through SSH.
Access and manage HDInsight Spark clusters by using Azure Toolkit for IntelliJ
You can do various operations by using Azure Toolkit for IntelliJ. Most of the operations are started from Azure Explorer. From the menu bar, navigate to View>Tool Windows>Azure Explorer.
Přístup k zobrazení úlohy
From Azure Explorer, navigate to HDInsight><Your Cluster>>Jobs.
In the right pane, the Spark Job View tab displays all the applications that were run on the cluster. Vyberte název aplikace, pro kterou chcete zobrazit další podrobnosti.
To display basic running job information, hover over the job graph. To view the stages graph and information that every job generates, select a node on the job graph.
To view frequently used logs, such as Driver Stderr, Driver Stdout, and Directory Info, select the Log tab.
You can view the Spark history UI and the YARN UI (at the application level). Select a link at the top of the window.
Přístup k serveru historie Sparku
From Azure Explorer, expand HDInsight, right-click your Spark cluster name, and then select Open Spark History UI.
When you're prompted, enter the cluster's admin credentials, which you specified when you set up the cluster.
On the Spark history server dashboard, you can use the application name to look for the application that you just finished running. V předchozím kódu nastavíte název aplikace pomocí
val conf = new SparkConf().setAppName("myApp"). Your Spark application name is myApp.
Start the Ambari portal
From Azure Explorer, expand HDInsight, right-click your Spark cluster name, and then select Open Cluster Management Portal(Ambari).
Po zobrazení výzvy zadejte přihlašovací údaje správce clusteru. You specified these credentials during the cluster setup process.
Správa předplatných Azure
By default, Azure Toolkit for IntelliJ lists the Spark clusters from all your Azure subscriptions. If necessary, you can specify the subscriptions that you want to access.
From Azure Explorer, right-click the Azure root node, and then select Select Subscriptions.
From the Select Subscriptions window, clear the check boxes next to the subscriptions that you don't want to access, and then select Close.
Spark Console
Můžete spustit místní konzolu Sparku (Scala) nebo spustit konzolu interaktivní relace Spark Livy (Scala).
Spark Local Console(Scala)
Ujistěte se, že jste splnili požadavky na WINUTILS.EXE.
Na řádku nabídek přejděte na Spustit>Upravit konfigurace....
From the Run/Debug Configurations window, in the left pane, navigate to Apache Spark on HDInsight>[Spark on HDInsight] myApp.
From the main window, select the
Locally Runtab.Zadejte následující hodnoty a pak vyberte OK:
Vlastnictví Hodnota Job main class Výchozí hodnota je hlavní třída z vybraného souboru. You can change the class by selecting the ellipsis(...) and choosing another class. Proměnné prostředí Ujistěte se, že je správná hodnota pro HADOOP_HOME. Umístění WINUTILS.exe Ujistěte se, že je cesta správná.
V Projectu přejděte na myApp>src>main>scala>myApp.
From the menu bar, navigate to Tools>Spark Console>Run Spark Local Console(Scala).
Pak se můžou zobrazit dvě dialogová okna s dotazem, jestli chcete automaticky opravit závislosti. Pokud ano, vyberte Automatická oprava.
Konzola by měla vypadat podobně jako na následujícím obrázku. V okně konzoly zadejte
sc.appNamea stiskněte ctrl+Enter. Zobrazí se výsledek. You can end the local console by clicking red button.
Spark Livy Interactive Session Console(Scala)
Na řádku nabídek přejděte na Spustit>Upravit konfigurace....
From the Run/Debug Configurations window, in the left pane, navigate to Apache Spark on HDInsight>[Spark on HDInsight] myApp.
From the main window, select the
Remotely Run in Clustertab.Zadejte následující hodnoty a pak vyberte OK:
Vlastnictví Hodnota Spark clusters (Linux only) Select the HDInsight Spark cluster on which you want to run your application. Název hlavní třídy Výchozí hodnota je hlavní třída z vybraného souboru. You can change the class by selecting the ellipsis(...) and choosing another class.
V Projectu přejděte na myApp>src>main>scala>myApp.
From the menu bar, navigate to Tools>Spark Console>Run Spark Livy Interactive Session Console(Scala).
Konzola by měla vypadat podobně jako na následujícím obrázku. V okně konzoly zadejte
sc.appNamea stiskněte ctrl+Enter. Zobrazí se výsledek. You can end the local console by clicking red button.
Send Selection to Spark Console
It's convenient for you to foresee the script result by sending some code to the local console or Livy Interactive Session Console(Scala). You can highlight some code in the Scala file, then right-click Send Selection To Spark Console. The selected code will be sent to the console. Výsledek se zobrazí za kódem v konzole. The console will check the errors if existing.
Integrate with HDInsight Identity Broker (HIB)
Connect to your HDInsight ESP cluster with ID Broker (HIB)
You can follow the normal steps to sign in to Azure subscription to connect to your HDInsight ESP cluster with ID Broker (HIB). After sign-in, you'll see the cluster list in Azure Explorer. For more instructions, see Connect to your HDInsight cluster.
Run a Spark Scala application on an HDInsight ESP cluster with ID Broker (HIB)
You can follow the normal steps to submit job to HDInsight ESP cluster with ID Broker (HIB). Refer to Run a Spark Scala application on an HDInsight Spark cluster for more instructions.
We upload the necessary files to a folder named with your sign-in account, and you can see the upload path in the configuration file.
Spark console on an HDInsight ESP cluster with ID Broker (HIB)
You can run Spark Local Console(Scala) or run Spark Livy Interactive Session Console(Scala) on an HDInsight ESP cluster with ID Broker (HIB). Refer to Spark Console for more instructions.
Poznámka:
For the HDInsight ESP cluster with Id Broker (HIB), link a cluster and debug Apache Spark applications remotely is not supported currently.
Reader-only role
Když uživatelé odešlou úlohu do clusteru s oprávněním role jen pro čtení, vyžaduje se přihlašovací údaje Ambari.
Link cluster from context menu
Sign in with reader-only role account.
V Azure Explorerurozbalte HDInsight a zobrazte clustery HDInsight, které jsou ve vašem předplatném. Clustery označené Role:Reader mají pouze oprávnění role jen pro čtení.
Klikněte pravým tlačítkem na cluster s oprávněním jen pro čtení. Select Link this cluster from context menu to link cluster. Enter the Ambari username and Password.
Pokud je cluster úspěšně propojený, služba HDInsight se aktualizuje. The stage of the cluster will become linked.
Link cluster by expanding Jobs node
Click Jobs node, Cluster Job Access Denied window pops up.
Click Link this cluster to link cluster.
Link cluster from Run/Debug Configurations window
Create an HDInsight Configuration. Then select Remotely Run in Cluster.
Select a cluster, which has reader-only role permission for Spark clusters(Linux only). Warning message shows out. You can Click Link this cluster to link cluster.
Zobrazení účtů úložiště
For clusters with reader-only role permission, click Storage Accounts node, Storage Access Denied window pops up. You can click Open Azure Storage Explorer to open Storage Explorer.
For linked clusters, click Storage Accounts node, Storage Access Denied window pops up. You can click Open Azure Storage to open Storage Explorer.
Convert existing IntelliJ IDEA applications to use Azure Toolkit for IntelliJ
You can convert the existing Spark Scala applications that you created in IntelliJ IDEA to be compatible with Azure Toolkit for IntelliJ. You can then use the plug-in to submit the applications to an HDInsight Spark cluster.
For an existing Spark Scala application that was created through IntelliJ IDEA, open the associated
.imlfile.At the root level, is a module element like the following text:
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">Edit the element to add
UniqueKey="HDInsightTool"so that the module element looks like the following text:<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4" UniqueKey="HDInsightTool">Uložte změny. Your application should now be compatible with Azure Toolkit for IntelliJ. You can test it by right-clicking the project name in Project. The pop-up menu now has the option Submit Spark Application to HDInsight.
Upravte zdroje
Pokud nebudete tuto aplikaci dál používat, odstraňte cluster, který jste vytvořili, pomocí následujícího postupu:
Přihlaste se do Azure Portalu.
Do vyhledávacího pole v horní části zadejte HDInsight.
V části Služby vyberte clustery HDInsight.
In the list of HDInsight clusters that appears, select the ... next to the cluster that you created for this article.
Vyberte Odstranit. Vyberte Ano.
Errors and solution
Unmark the src folder as Sources if you get build failed errors as below:
Unmark the src folder as Sources to solution this issue:
Navigate to File and select the Project Structure.
Select the Modules under the Project Settings.
Select the src file and unmark as Sources.
Click on Apply button and then click on OK button to close the dialog.
Další kroky
In this article, you learned how to use the Azure Toolkit for IntelliJ plug-in to develop Apache Spark applications written in Scala. Then submitted them to an HDInsight Spark cluster directly from the IntelliJ integrated development environment (IDE). V dalším článku se dozvíte, jak se data zaregistrovaná v Apache Sparku dají načíst do analytického nástroje BI, jako je Power BI.