快速入门:使用 Azure 门户在 Azure HDInsight 中创建 Apache Spark 群集Quickstart: Create Apache Spark cluster in Azure HDInsight using Azure portal

在本快速入门中,你将使用 Azure 门户在 Azure HDInsight 中创建 Apache Spark 群集。In this quickstart, you use the Azure portal to create an Apache Spark cluster in Azure HDInsight. 然后你将创建一个 Jupyter 笔记本,并使用它来针对 Apache Hive 表运行 Spark SQL 查询。You then create a Jupyter notebook, and use it to run Spark SQL queries against Apache Hive tables. Azure HDInsight 是适用于企业的分析服务,具有托管、全面且开源的特点。Azure HDInsight is a managed, full-spectrum, open-source analytics service for enterprises. 用于 HDInsight 的 Apache Spark 框架使用内存中处理功能实现快速数据分析和群集计算。The Apache Spark framework for HDInsight enables fast data analytics and cluster computing using in-memory processing. 使用 Jupyter 笔记本,可以与数据进行交互、将代码和 Markdown 文本结合使用,以及进行简单的可视化。Jupyter notebook lets you interact with your data, combine code with markdown text, and do simple visualizations.

有关可用配置的详细说明,请参阅在 HDInsight 中设置群集For in-depth explanations of available configurations, see Set up clusters in HDInsight. 有关使用门户创建群集的详细信息,请参阅在门户中创建群集For more information regarding the use of the portal to create clusters, see Create clusters in the portal.


HDInsight 群集是基于分钟按比例收费,而不管用户是否正在使用它们。Billing for HDInsight clusters is prorated per minute, whether you are using them or not. 请务必在使用完之后删除群集。Be sure to delete your cluster after you have finished using it. 有关详细信息,请参阅本文的清理资源部分。For more information, see the Clean up resources section of this article.


在 HDInsight 中创建 Apache Spark 群集Create an Apache Spark cluster in HDInsight

你将使用 Azure 门户创建一个 HDInsight 群集,它使用 Azure 存储 Blob 作为群集存储。You use the Azure portal to create an HDInsight cluster that uses Azure Storage Blobs as the cluster storage. 有关使用 Data Lake Storage Gen2 的详细信息,请参阅快速入门:在 HDInsight 中设置群集For more information on using Data Lake Storage Gen2, see Quickstart: Set up clusters in HDInsight.

  1. 登录到 Azure 门户Sign in to the Azure portal.

  2. 在顶部菜单中,选择“+ 创建资源” 。From the top menu, select + Create a resource.

    Azure 门户“创建资源”Azure portal create a resource

  3. 选择“分析” > “Azure HDInsight”,转到“创建 HDInsight 群集” 页。Select Analytics > Azure HDInsight to go to the Create HDInsight cluster page.

  4. 在“基本信息”选项卡中提供以下信息: From the Basics tab, provide the following information:

    属性Property 说明Description
    订阅Subscription 从下拉列表中选择用于此群集的 Azure 订阅。From the drop-down list, select the Azure subscription that's used for the cluster.
    资源组Resource group 从下拉列表中选择现有资源组,或选择“新建” 。From the drop-down list, select your existing resource group, or select Create new.
    群集名称Cluster name 输入任何全局唯一的名称。Enter a globally unique name.
    区域Region 从下拉列表中,选择在其中创建群集的区域。From the drop-down list, select a region where the cluster is created.
    群集类型Cluster type 选择“选择群集类型”,打开一个列表。Select Select cluster type to open a list. 从列表中选择“Spark” 。From the list, select Spark.
    群集版本Cluster version 选择群集类型后,此字段中将自动填充默认版本。This field will auto-populate with the default version once the cluster type has been selected.
    群集登录用户名Cluster login username 输入群集登录用户名。Enter the cluster login username. 默认名称为 admin。在此快速入门中稍后使用该帐户登录到 Jupyter notebook。The default name is admin. You use this account to login in to the Jupyter notebook later in the quickstart.
    群集登录密码Cluster login password 输入群集登录密码。Enter the cluster login password.
    安全外壳 (SSH) 用户名Secure Shell (SSH) username 输入 SSH 用户名。Enter the SSH username. 用于此快速入门的 SSH 用户名为“sshuser” 。The SSH username used for this quickstart is sshuser. 默认情况下,此帐户的密码与群集登录用户名帐户的密码相同 。By default, this account shares the same password as the Cluster Login username account.

    创建 HDInsight 群集基本配置Create HDInsight cluster basic configurations

    在完成时选择“下一步: 存储 >>”转到“存储”页 。Select Next: Storage >> to continue to the Storage page.

  5. 在“存储”下,提供以下值 :Under Storage, provide the following values:

    属性Property 说明Description
    主存储类型Primary storage type 使用默认值“Azure 存储”。 Use the default value Azure Storage.
    选择方法Selection method 使用默认值“从列表中选择”。 Use the default value Select from list.
    主存储帐户Primary storage account 使用自动填充的值。Use the auto-populated value.
    容器Container 使用自动填充的值。Use the auto-populated value.

    创建 HDInsight 群集基本配置Create HDInsight cluster basic configurations

    选择“查看 + 创建”以继续。 Select Review + create to continue.

  6. 在“查看 + 创建”下,选择“创建”。 Under Review + create, select Create. 创建群集大约需要 20 分钟时间。It takes about 20 minutes to create the cluster. 必须先创建群集,才能继续下一会话。The cluster must be created before you can proceed to the next session.

如果在创建 HDInsight 群集时遇到问题,可能是因为你没有这样做的适当权限。If you run into an issue with creating HDInsight clusters, it could be that you don't have the right permissions to do so. 有关详细信息,请参阅访问控制要求For more information, see Access control requirements.

创建 Jupyter 笔记本Create a Jupyter notebook

Jupyter Notebook 是支持各种编程语言的交互式笔记本环境。Jupyter Notebook is an interactive notebook environment that supports various programming languages. 通过此笔记本可以与数据进行交互、结合代码和 markdown 文本以及执行简单的可视化效果。The notebook allows you to interact with your data, combine code with markdown text and perform simple visualizations.

  1. 在 Web 浏览器中导航到 https://CLUSTERNAME.azurehdinsight.cn/jupyter,其中的 CLUSTERNAME 是群集的名称。From a web browser, navigate to https://CLUSTERNAME.azurehdinsight.cn/jupyter, where CLUSTERNAME is the name of your cluster. 出现提示时,请输入群集的群集登录凭据。If prompted, enter the cluster login credentials for the cluster.

  2. 选择“新建” > “PySpark”,创建笔记本 。Select New > PySpark to create a notebook.

    创建 Jupyter Notebook 以运行交互式 Spark SQL 查询Create a Jupyter Notebook to run interactive Spark SQL query

    新 Notebook 随即会创建,并以 Untitled(Untitled.pynb) 名称打开。A new notebook is created and opened with the name Untitled(Untitled.pynb).

运行 Apache Spark SQL 语句Run Apache Spark SQL statements

SQL(结构化查询语言)是用于查询和定义数据的最常见、最广泛使用的语言。SQL (Structured Query Language) is the most common and widely used language for querying and defining data. Spark SQL 作为 Apache Spark 的扩展使用,可使用熟悉的 SQL 语法处理结构化数据。Spark SQL functions as an extension to Apache Spark for processing structured data, using the familiar SQL syntax.

  1. 验证 kernel 已就绪。Verify the kernel is ready. 如果在 Notebook 中的内核名称旁边看到空心圆,则内核已准备就绪。The kernel is ready when you see a hollow circle next to the kernel name in the notebook. 实心圆表示内核正忙。Solid circle denotes that the kernel is busy.

    HDInsight Spark1 中的 Hive 查询Hive query in HDInsight Spark1

    首次启动 Notebook 时,内核在后台执行一些任务。When you start the notebook for the first time, the kernel performs some tasks in the background. 等待内核准备就绪。Wait for the kernel to be ready.

  2. 将以下代码粘贴到一个空单元格中,然后按 SHIFT + ENTER 来运行这些代码。Paste the following code in an empty cell, and then press SHIFT + ENTER to run the code. 此命令列出群集上的 Hive 表:The command lists the Hive tables on the cluster:


    将 Jupyter Notebook 与 HDInsight Spark 群集配合使用时,会获得一个预设的 sqlContext,可以使用它通过 Spark SQL 来运行 Hive 查询。When you use a Jupyter Notebook with your HDInsight Spark cluster, you get a preset sqlContext that you can use to run Hive queries using Spark SQL. %%sql 指示 Jupyter Notebook 使用预设 sqlContext 运行 Hive 查询。%%sql tells Jupyter Notebook to use the preset sqlContext to run the Hive query. 该查询从默认情况下所有 HDInsight 群集都带有的 Hive 表 (hivesampletable ) 检索前 10 行。The query retrieves the top 10 rows from a Hive table (hivesampletable) that comes with all HDInsight clusters by default. 需要大约 30 秒才能获得结果。It takes about 30 seconds to get the results. 输出如下所示:The output looks like:

    HDInsight 中的 Apache Hive 查询Apache Hive query in HDInsight

    每次在 Jupyter 中运行查询时,Web 浏览器窗口标题中都会显示“(繁忙)” 状态和 Notebook 标题。Every time you run a query in Jupyter, your web browser window title shows a (Busy) status along with the notebook title. 右上角“PySpark” 文本的旁边还会出现一个实心圆。You also see a solid circle next to the PySpark text in the top-right corner.

  3. 运行另一个查询,请查看 hivesampletable 中的数据。Run another query to see the data in hivesampletable.

    SELECT * FROM hivesampletable LIMIT 10

    屏幕在刷新后会显示查询输出。The screen shall refresh to show the query output.

    HDInsight 中的 Hive 查询输出Hive query output in HDInsight

  4. 请在 Notebook 的“文件”菜单中选择“关闭并停止” 。From the File menu on the notebook, select Close and Halt. 关闭 Notebook 会释放群集资源。Shutting down the notebook releases the cluster resources.

清理资源Clean up resources

HDInsight 将数据保存在 Azure 存储或 Azure Data Lake Storage 中,因此可以在未使用群集时安全地删除群集。HDInsight saves your data in Azure Storage or Azure Data Lake Storage, so you can safely delete a cluster when it is not in use. 此外,还需要支付 HDInsight 群集费用,即使未使用。You are also charged for an HDInsight cluster, even when it is not in use. 由于群集费用高于存储空间费用数倍,因此在不使用群集时将其删除可以节省费用。Since the charges for the cluster are many times more than the charges for storage, it makes economic sense to delete clusters when they are not in use. 如果要立即开始后续步骤中所列的教程,可能需要保留群集。If you plan to work on the tutorial listed in Next steps immediately, you might want to keep the cluster.

切换回 Azure 门户,并选择“删除” 。Switch back to the Azure portal, and select Delete.

在 Azure 门户中删除 HDInsight 群集Azure portal delete an HDInsight cluster

还可以选择资源组名称来打开“资源组”页,然后选择“删除资源组” 。You can also select the resource group name to open the resource group page, and then select Delete resource group. 通过删除资源组,可以删除 HDInsight 群集和默认存储帐户。By deleting the resource group, you delete both the HDInsight cluster, and the default storage account.

后续步骤Next steps

在本快速入门中,你已了解如何在 HDInsight 中创建 Apache Spark 群集并运行基本的 Spark SQL 查询。In this quickstart, you learned how to create an Apache Spark cluster in HDInsight and run a basic Spark SQL query. 转到下一教程,了解如何使用 HDInsight 群集针对示例数据运行交互式查询。Advance to the next tutorial to learn how to use an HDInsight cluster to run interactive queries on sample data.