通过 PowerShell 创建使用 Azure Data Lake Storage Gen1 作为默认存储的 HDInsight 群集

了解如何在 Azure PowerShell 中配置使用 Azure Data Lake Storage Gen1 作为默认存储的 Azure HDInsight 群集。 有关如何创建使用 Data Lake Storage Gen1 作为额外存储的 HDInsight 群集的说明,请参阅创建使用 Data Lake Storage Gen1 作为额外存储的 HDInsight 群集

下面是结合使用 HDInsight 和 Data Lake Storage Gen1 的一些重要注意事项:

  • HDInsight 版本 3.5 和 3.6 提供创建 HDInsight 群集(可访问作为默认存储的 Data Lake Storage Gen1)的选项。

  • 创建 HDInsight 群集(可访问作为默认存储的 Data Lake Storage Gen1)的选项不可用于 HDInsight Premium 群集

若要通过 PowerShell 来配置可以使用 Data Lake Storage Gen1 的 HDInsight,请遵循后续五个部分中的说明。

先决条件

注意

建议使用 Azure Az PowerShell 模块与 Azure 交互。 请参阅安装 Azure PowerShell 以开始使用。 若要了解如何迁移到 Az PowerShell 模块,请参阅 将 Azure PowerShell 从 AzureRM 迁移到 Az

在开始学习本教程之前,请确保满足以下要求:

  • 一个 Azure 订阅:转到获取 Azure 免费试用版

  • Azure PowerShell 1.0 或更高版本:参阅如何安装和配置 Azure PowerShell

  • Windows 软件开发工具包 (SDK):若要安装 Windows SDK,请转到适用于 Windows 10 的下载内容和工具。 该 SDK 用于创建安全证书。

  • Microsoft Entra服务主体:本教程介绍如何在 Microsoft Entra ID 中创建服务主体。 但是,若要创建服务主体,你必须是Microsoft Entra管理员。 管理员可以跳过此先决条件部分,继续阅读本教程。

    注意

    仅当是Microsoft Entra管理员时,才能创建服务主体。 Microsoft Entra管理员必须先创建服务主体,然后才能使用 Data Lake Storage Gen1 创建 HDInsight 群集。 必须根据使用证书创建服务主体中所述,使用证书创建服务主体。

创建 Azure Data Lake Storage Gen1 帐户

若要创建 Data Lake Storage Gen1 帐户,请执行以下操作:

  1. 在桌面上打开 PowerShell 窗口,并输入以下代码片段。 出现登录提示时,请以订阅管理员或所有者的身份登录。

    # Sign in to your Azure account
    Connect-AzAccount
    
    # List all the subscriptions associated to your account
    Get-AzSubscription
    
    # Select a subscription
    Set-AzContext -SubscriptionId <subscription ID>
    
    # Register for Data Lake Storage Gen1
    Register-AzResourceProvider -ProviderNamespace "Microsoft.DataLakeStore"
    

    注意

    如果在注册 Data Lake Storage Gen1 资源提供程序时收到类似于 Register-AzResourceProvider : InvalidResourceNamespace: The resource namespace 'Microsoft.DataLakeStore' is invalid 的错误,原因可能是订阅未获得 Data Lake Storage Gen1 批准。 要在 Data Lake Storage Gen1 中启用 Azure 订阅,请遵循通过 Azure 门户开始使用 Data Lake Storage Gen1 中的说明进行操作。

  2. Data Lake Store Gen1 帐户与 Azure 资源组关联。 首先请创建资源组。

    $resourceGroupName = "<your new resource group name>"
    New-AzResourceGroup -Name $resourceGroupName -Location "East US 2"
    

    应该会看到如下输出:

    ResourceGroupName : hdiadlgrp
    Location          : eastus2
    ProvisioningState : Succeeded
    Tags              :
    ResourceId        : /subscriptions/<subscription-id>/resourceGroups/hdiadlgrp
    
  3. 创建 Data Lake Storage Gen1 帐户。 指定的帐户名只能包含小写字母和数字。

    $dataLakeStorageGen1Name = "<your new Data Lake Storage Gen1 name>"
    New-AzDataLakeStoreAccount -ResourceGroupName $resourceGroupName -Name $dataLakeStorageGen1Name -Location "East US 2"
    

    应看到如下输出:

    ...
    ProvisioningState           : Succeeded
    State                       : Active
    CreationTime                : 5/5/2017 10:53:56 PM
    EncryptionState             : Enabled
    ...
    LastModifiedTime            : 5/5/2017 10:53:56 PM
    Endpoint                    : hdiadlstore.azuredatalakestore.net
    DefaultGroup                :
    Id                          : /subscriptions/<subscription-id>/resourceGroups/hdiadlgrp/providers/Microsoft.DataLakeStore/accounts/hdiadlstore
    Name                        : hdiadlstore
    Type                        : Microsoft.DataLakeStore/accounts
    Location                    : East US 2
    Tags                        : {}
    
  4. 要将 Data Lake Storage Gen1 用作默认存储,需要指定一个根路径,在创建群集过程中将复制此路径下的特定于群集的文件。 若要创建根路径(在代码片段中为“/clusters/hdiadlcluster”),请使用以下 cmdlet:

    $myrootdir = "/"
    New-AzDataLakeStoreItem -Folder -AccountName $dataLakeStorageGen1Name -Path $myrootdir/clusters/hdiadlcluster
    

对 Data Lake Storage Gen1 设置基于角色访问的身份验证

每个 Azure 订阅都与一个 Microsoft Entra 实体相关联。 使用 Azure 门户 或 Azure 资源管理器 API 访问订阅资源的用户和服务必须首先使用 Microsoft Entra ID 进行身份验证。 通过在 Azure 资源上为这些用户和服务分配相应角色,向其授予访问权限。 对于服务,服务主体标识Microsoft Entra ID中的服务。

本部分说明如何向应用程序服务(例如 HDInsight)授予对 Azure 资源(前面创建的 Data Lake Storage Gen1 帐户)的访问权限。 为此,可为应用程序创建一个服务主体,并通过 PowerShell 向其分配角色。

若要为 Data Lake Storage Gen1 设置 Active Directory 身份验证,请执行以下两个部分中的任务。

创建自签名证书

继续进行本部分中的步骤前,请确保已安装有 Windows SDK。 还必须事先创建一个目录例如 C:\mycertdir,会在该目录中创建证书

  1. 在 PowerShell 窗口中,转到安装 Windows SDK 的位置(通常为 C:\Program Files (x86)\Windows Kits\10\bin\x86),并使用 MakeCert 实用工具创建一个自签名证书和私钥 。 使用以下命令:

    $certificateFileDir = "<my certificate directory>"
    cd $certificateFileDir
    
    makecert -sv mykey.pvk -n "cn=HDI-ADL-SP" CertFile.cer -r -len 2048
    

    系统会提示输入私钥密码。 成功执行该命令后,指定的证书目录中应会出现 CertFile.cer 和 mykey.pvk

  2. 使用 Pvk2Pfx 实用工具将 MakeCert 创建的.pvk 和.cer 文件转换为.pfx 文件。 运行以下命令:

    pvk2pfx -pvk mykey.pvk -spc CertFile.cer -pfx CertFile.pfx -po <password>
    

    出现提示时,请输入前面指定的私钥密码。 为 -po 参数指定的值是与 .pfx 文件关联的密码。 成功完成该命令后,指定的证书目录中也应会出现 CertFile.pfx。

创建Microsoft Entra ID和服务主体

在本部分中,为Microsoft Entra应用程序创建服务主体,向服务主体分配角色,并通过提供证书作为服务主体进行身份验证。 若要在 Microsoft Entra ID 中创建应用程序,请运行以下命令:

  1. 将以下 cmdlet 粘贴到 PowerShell 控制台窗口中。 确保为 -DisplayName 属性指定的值是唯一的。 -HomePage 和 -IdentiferUris 的值是占位符值,且未经验证

    $certificateFilePath = "$certificateFileDir\CertFile.pfx"
    
    $password = Read-Host -Prompt "Enter the password" # This is the password you specified for the .pfx file
    
    $certificatePFX = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($certificateFilePath, $password)
    
    $rawCertificateData = $certificatePFX.GetRawCertData()
    
    $credential = [System.Convert]::ToBase64String($rawCertificateData)
    
    $application = New-AzADApplication `
        -DisplayName "HDIADL" `
        -HomePage "https://contoso.com" `
        -IdentifierUris "https://contoso.com" `
        -CertValue $credential  `
        -StartDate $certificatePFX.NotBefore  `
        -EndDate $certificatePFX.NotAfter
    
    $applicationId = $application.ApplicationId
    
  2. 使用应用程序 ID 创建服务主体。

    $servicePrincipal = New-AzADServicePrincipal -ApplicationId $applicationId -Role Contributor
    
    $objectId = $servicePrincipal.Id
    
  3. 授予服务主体访问之前指定的 Data Lake Storage Gen1 根路径和此路径中的所有文件夹的权限。 使用以下 cmdlet:

    Set-AzDataLakeStoreItemAclEntry -AccountName $dataLakeStorageGen1Name -Path / -AceType User -Id $objectId -Permissions All
    Set-AzDataLakeStoreItemAclEntry -AccountName $dataLakeStorageGen1Name -Path /clusters -AceType User -Id $objectId -Permissions All
    Set-AzDataLakeStoreItemAclEntry -AccountName $dataLakeStorageGen1Name -Path /clusters/hdiadlcluster -AceType User -Id $objectId -Permissions All
    

创建使用 Data Lake Storage Gen1 作为默认存储的 HDInsight Linux 群集

在本部分,我们将创建使用 Data Lake Storage Gen1 作为默认存储的 HDInsight Hadoop Linux 群集。 在此版本中,HDInsight 群集和 Data Lake Storage Gen1 必须位于同一位置。

  1. 检索订阅租户 ID,并存储该 ID 供稍后使用。

    $tenantID = (Get-AzContext).Tenant.TenantId
    
  2. 使用以下 cmdlet 创建 HDInsight 群集:

    # Set these variables
    
    $location = "East US 2"
    $storageAccountName = $dataLakeStorageGen1Name    # Data Lake Storage Gen1 account name
        $storageRootPath = "<Storage root path you specified earlier>"     # e.g. /clusters/hdiadlcluster
        $clusterName = "<unique cluster name>"
    $clusterNodes = <ClusterSizeInNodes>            # The number of nodes in the HDInsight cluster
    $httpCredentials = Get-Credential
    $sshCredentials = Get-Credential
    
    New-AzHDInsightCluster `
           -ClusterType Hadoop `
           -OSType Linux `
           -ClusterSizeInNodes $clusterNodes `
           -ResourceGroupName $resourceGroupName `
           -ClusterName $clusterName `
           -HttpCredential $httpCredentials `
           -Location $location `
           -DefaultStorageAccountType AzureDataLakeStore `
           -DefaultStorageAccountName "$storageAccountName.azuredatalakestore.net" `
           -DefaultStorageRootPath $storageRootPath `
           -Version "3.6" `
           -SshCredential $sshCredentials `
           -AadTenantId $tenantId `
           -ObjectId $objectId `
           -CertificateFilePath $certificateFilePath `
           -CertificatePassword $password
    

    成功完成该 cmdlet 后,应会出现列出群集详细信息的输出。

在 HDInsight 群集上运行测试作业以使用 Data Lake Storage Gen1

配置 HDInsight 群集后,可在该群集上运行测试作业,确保该群集可访问 Data Lake Storage Gen1。 为此,请运行示例 Hive 作业来创建表,该表使用 Data Lake Storage Gen1 的 <群集根目录>/example/data/sample.log 中已提供的示例数据。

在本部分,我们将与创建的 HDInsight Linux 群集建立安全外壳 (SSH) 连接,然后运行示例 Hive 查询。

  1. 建立连接后,请使用以下命令启动 Hive 命令行接口 (CLI):

    hive
    
  2. 使用该 CLI 输入以下语句,通过使用 Data Lake Storage Gen1 中的示例数据创建一个名为 vehicles 的新表

    DROP TABLE log4jLogs;
        CREATE EXTERNAL TABLE log4jLogs (t1 string, t2 string, t3 string, t4 string, t5 string, t6 string, t7 string)
        ROW FORMAT DELIMITED FIELDS TERMINATED BY ' '
        STORED AS TEXTFILE LOCATION 'adl:///example/data/';
        SELECT t4 AS sev, COUNT(*) AS count FROM log4jLogs WHERE t4 = '[ERROR]' AND INPUT__FILE__NAME LIKE '%.log' GROUP BY t4;
    

可以在 SSH 控制台上看到查询输出。

注意

上面 CREATE TABLE 命令中的示例数据的路径为 adl:///example/data/,其中 adl:/// 是群集根。 根据本教程中指定的群集根示例,该命令为 adl://hdiadlstore.azuredatalakestore.net/clusters/hdiadlcluster。 可以使用更短的替代内容,或者提供群集根的完整路径。

使用 HDFS 命令访问 Data Lake Storage Gen1

将 HDInsight 群集配置为使用 Data Lake Storage Gen1 后,可以使用 Hadoop 分布式文件系统 (HDFS) shell 命令来访问该存储。

在本部分,我们将与创建的 HDInsight Linux 群集建立 SSH 连接,然后运行 HDFS 命令。

建立连接后,使用以下 HDFS 文件系统命令列出 Data Lake Storage Gen1 中的文件。

hdfs dfs -ls adl:///

可以使用 hdfs dfs -put 命令将一些文件上传到 Data Lake Storage Gen1,然后使用 hdfs dfs -ls 验证是否已成功上传这些文件。

另请参阅