搭配 Azure HDInsight 叢集使用 Data Lake Storage Gen1

注意

針對已改善的效能和新功能,將新 HDInsight 叢集搭配 Azure Data Lake Storage Gen2 使用。

若要分析 HDInsight 叢集中的資料,您可以將資料儲存在 Azure Blob storageAzure Data Lake Storage Gen 1Azure Data Lake Storage Gen 2。 所有儲存體選項都可讓您安全地刪除用於計算的 HDInsight 叢集,而不會遺失使用者資料。

在本文中,您將了解 Data Lake Storage Gen1 與 HDInsight 叢集搭配運作的方式。 若要深入了解 Azure Blob 儲存體與 HDInsight 叢集搭配運作的方式,請參閱使用 Azure Blob 儲存體搭配 Azure HDInsight 叢集。 如需建立 HDInsight 叢集的詳細資訊,請參閱在 HDInsight 中建立 Apache Hadoop 叢集

注意

Data Lake Storage Gen1 一律透過安全通道存取,因此不會有 adls 檔案系統配置名稱。 您會一律使用 adl

注意

建議您使用 Azure Az PowerShell 模組來與 Azure 互動。 請參閱安裝 Azure PowerShell 以開始使用。 若要了解如何移轉至 Az PowerShell 模組,請參閱將 Azure PowerShell 從 AzureRM 移轉至 Az

HDInsight 叢集的可用性

Apache Hadoop 支援預設檔案系統的概念。 預設檔案系統意指預設配置和授權。 也可用來解析相對路徑。 在 HDInsight 叢集建立程序期間,請指定 Azure 儲存體中的 Blob 容器作為預設檔案系統。 或使用 HDInsight 3.5 及更新版本,選取 Azure Blob 儲存體或 Azure Data Lake Storage Gen1 作為預設檔案系統 (有少數除外)。 叢集與儲存體帳戶必須在相同區域內託管。

HDInsight 叢集可透過 2 種方式來使用 Data Lake Storage Gen1:

  • 作為預設儲存體
  • 作為其他儲存體,搭配 Azure Blob 儲存體作為預設儲存體。

目前,只有一些 HDInsight 叢集類型/版本支援使用 Data Lake Storage Gen1 來作為預設儲存體和其他儲存體帳戶:

HDInsight 叢集類型 使用 Data Lake Storage Gen1 作為預設儲存體 使用 Data Lake Storage Gen1 作為其他儲存體 備註
HDInsight 4.0 版 No No HDInsight 4.0 不支援 ADLS Gen1
HDInsight 3.6 版 Yes Yes 不包含 HBase
HDInsight 3.5 版 Yes Yes 不包含 HBase
HDInsight 3.4 版 No Yes
HDInsight 3.3 版 No No
HDInsight 3.2 版 No Yes

警告

Azure Data Lake Storage Gen1 不支援 HDInsight HBase

使用 Data Lake Storage Gen1 作為其他儲存體帳戶,並不會影響效能。 或者,從叢集讀取或寫入 Azure Blob 儲存體的能力。

使用 Data Lake Storage Gen1 作為預設儲存體

當使用 Data Lake Storage Gen1 作為預設儲存體部署 HDInsight 時,與叢集相關的檔案會儲存在 adl://mydatalakestore/<cluster_root_path>/ 中,其中 <cluster_root_path> 是您在 Data Lake Storage 中建立的資料夾名稱。 藉由指定每個叢集的根路徑,您可針對多個叢集使用相同的 Data Lake Storage 帳戶。 因此在您的設定中︰

  • Cluster 1 可以使用路徑 adl://mydatalakestore/cluster1storage
  • Cluster 2 可以使用路徑 adl://mydatalakestore/cluster2storage

請注意,這兩個叢集都使用相同的 Data Lake Storage Gen1 帳戶 mydatalakestore。 每個叢集都會在 Data Lake Storage 中存取自己的根檔案系統。 Azure 入口網站的部署經驗會提示您使用像是 /clusters/<clustername> 的資料夾名稱做為根路徑。

若要使用 Data Lake Storage Gen1 作為預設儲存體,您必須授與服務主體存取下列路徑:

  • 刪除 Data Lake Storage Gen1 帳戶根目錄。 例如:adl://mydatalakestore/。
  • 所有叢集資料夾的資料夾。 例如:adl://mydatalakestore/clusters。
  • 叢集的資料夾。 例如:adl://mydatalakestore/clusters/cluster1storage。

如需建立服務主體和授與存取權的詳細資訊,請參閱設定 Data Lake Storage 存取。

從 Azure Key Vault 擷取憑證以用於建立叢集

如果服務主體的憑證儲存在 Azure Key Vault,您必須將憑證轉換成正確的格式。 下列程式碼片段示範如何執行轉換。

首先,從 Key Vault 下載憑證,並擷取 SecretValueText

$certPassword = Read-Host "Enter Certificate Password"
$cert = (Get-AzureKeyVaultSecret -VaultName 'MY-KEY-VAULT' -Name 'MY-SECRET-NAME')
$certValue = [System.Convert]::FromBase64String($cert.SecretValueText)

接下來,將 SecretValueText 轉換為憑證。

$certObject = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2 -ArgumentList $certValue,$null,"Exportable, PersistKeySet"
$certBytes = $certObject.Export([System.Security.Cryptography.X509Certificates.X509ContentType]::Pkcs12, $certPassword.SecretValueText);
$identityCertificate = [System.Convert]::ToBase64String($certBytes)

然後,您可以使用 $identityCertificate 來部署新的叢集,如下列程式碼片段所示:

New-AzResourceGroupDeployment `
    -ResourceGroupName $resourceGroupName `
    -TemplateFile $pathToArmTemplate `
    -identityCertificate $identityCertificate `
    -identityCertificatePassword $certPassword.SecretValueText `
    -clusterName  $clusterName `
    -clusterLoginPassword $SSHpassword `
    -sshPassword $SSHpassword `
    -servicePrincipalApplicationId $application.ApplicationId

使用 Data Lake Storage Gen1 作為其他儲存體

您也可以使用 Data Lake Storage Gen1 作為叢集的其他儲存體。 在這種情況下,叢集預設儲存體可以是 Azure Blob 儲存體或 Azure Data Lake Storage Gen1 帳戶。 若 HDInsight 工作執行的目標資料儲存在作為其他儲存體的 Azure Data Lake Storage Gen1 中,則請使用完整路徑。 例如:

adl://mydatalakestore.azuredatalakestore.net/<file_path>

現在 URL 中沒有任何 cluster_root_path。 這是因為在此案例中,Data Lake Storage 不是預設儲存體。 因此,您只需要提供檔案的路徑即可。

若要使用 Data Lake Storage Gen1 作為其他儲存體,請將檔案儲存位置路徑的存取權授與服務主體。 例如:

adl://mydatalakestore.azuredatalakestore.net/<file_path>

如需建立服務主體和授與存取權的詳細資訊,請參閱設定 Data Lake Storage 存取。

使用多個 Data Lake Storage Gen1 帳戶

您可以新增 Data Lake Storage 帳戶作為額外帳戶,並新增多個 Data Lake Storage 帳戶。 將一或多個 Data Lake Storage 帳戶中資料的權限授與 HDInsight 叢集。 請參閱「設定 Data Lake Storage Gen1 存取」。

設定 Data Lake Storage Gen1 存取

若要從 HDInsight 叢集設定 Azure Data Lake Storage Gen1 存取,您必須擁有 Microsoft Entra 服務主體。 只有 Microsoft Entra 系統管理員才可以建立服務主體。 必須使用憑證來建立服務主體。 如需詳細資訊,請參閱快速入門:在 HDInsight 中設定叢集以及使用自我簽署憑證來建立服務主體

注意

如果您即將使用 Azure Data Lake Storage Gen1 作為 HDInsight 叢集的額外儲存體,強烈建議您如本文所述建立叢集時執行此作業。 將 Azure Data Lake Storage Gen1 新增為現有 HDInsight 叢集的額外儲存體不是支援的案例。

如需存取控制模型的詳細資訊,請參閱 Data Lake Storage Gen1 中的存取控制

從叢集存取檔案

有數種方式可讓您從 HDInsight 叢集存取 Data Lake Storage 中的檔案。

  • 使用完整格式名稱。 使用這種方法,您可以針對想要存取的檔案提供完整路徑。

    adl://<data_lake_account>.azuredatalakestore.net/<cluster_root_path>/<file_path>
    
  • 使用簡短路徑格式。 使用這種方法,您可以利用以下方式取代到叢集根目錄的路徑:

    adl:///<file path>
    
  • 使用相對路徑。 使用這種方法,您可以針對想要存取的檔案,只提供相對路徑。

    /<file.path>/
    

資料存取範例

範例是以叢集前端節點的 ssh 連線為基礎。 這些範例會使用這三個 URI 配置。 請將 DATALAKEACCOUNTCLUSTERNAME 取代為相關值。

一些 hdfs 命令

  1. 在本機儲存體上建立檔案。

    touch testFile.txt
    
  2. 在叢集儲存體上建立目錄。

    hdfs dfs -mkdir adl://DATALAKEACCOUNT.azuredatalakestore.net/clusters/CLUSTERNAME/sampledata1/
    hdfs dfs -mkdir adl:///sampledata2/
    hdfs dfs -mkdir /sampledata3/
    
  3. 將資料從本機儲存體複製到叢集儲存體。

    hdfs dfs -copyFromLocal testFile.txt adl://DATALAKEACCOUNT.azuredatalakestore.net/clusters/CLUSTERNAME/sampledata1/
    hdfs dfs -copyFromLocal testFile.txt adl:///sampledata2/
    hdfs dfs -copyFromLocal testFile.txt /sampledata3/
    
  4. 列出叢集儲存體上的目錄內容。

    hdfs dfs -ls adl://DATALAKEACCOUNT.azuredatalakestore.net/clusters/CLUSTERNAME/sampledata1/
    hdfs dfs -ls adl:///sampledata2/
    hdfs dfs -ls /sampledata3/
    

建立 Hive 資料表

顯示三個檔案位置以供說明之用。 若為實際執行,請僅使用其中一個 LOCATION 項目。

DROP TABLE myTable;
CREATE EXTERNAL TABLE myTable (
    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://DATALAKEACCOUNT.azuredatalakestore.net/clusters/CLUSTERNAME/example/data/';
LOCATION 'adl:///example/data/';
LOCATION '/example/data/';

識別來自 Ambari 的儲存體路徑

若要識別已設定的預設儲存體完整路徑,請瀏覽至 HDFS>設定,然後在篩選輸入方塊中輸入 fs.defaultFS

建立可存取 Data Lake Storage Gen1 的 HDInsight 叢集

如需建立可存取 Data Lake Storage Gen1 的 HDInsight 叢集詳細指示,請使用下列連結。

重新整理存取 Data Lake Storage Gen1 時會用到的 HDInsight 憑證

下列範例 PowerShell 程式碼會從本機檔案或 Azure Key Vault 讀取憑證,然後使用新憑證更新 HDInsight 叢集,以存取 Azure Data Lake Storage Gen1。 提供您自己的 HDInsight 叢集名稱、資源群組名稱、訂用帳戶識別碼、app ID、憑證的本機路徑。 在系統提示時輸入密碼。

$clusterName = '<clustername>'
$resourceGroupName = '<resourcegroupname>'
$subscriptionId = '01234567-8a6c-43bc-83d3-6b318c6c7305'
$appId = '01234567-e100-4118-8ba6-c25834f4e938'
$addNewCertKeyCredential = $true
$certFilePath = 'C:\localfolder\adls.pfx'
$KeyVaultName = "my-key-vault-name"
$KeyVaultSecretName = "my-key-vault-secret-name"
$certPassword = Read-Host "Enter Certificate Password"
# certSource
# 0 - create self signed cert
# 1 - read cert from file path
# 2 - read cert from key vault
$certSource = 0

Login-AzAccount
Select-AzSubscription -SubscriptionId $subscriptionId

if($certSource -eq 0)
{
    Write-Host "Generating new SelfSigned certificate"

    $cert = New-SelfSignedCertificate -CertStoreLocation "cert:\CurrentUser\My" -Subject "CN=hdinsightAdlsCert" -KeySpec KeyExchange
    $certBytes = $cert.Export([System.Security.Cryptography.X509Certificates.X509ContentType]::Pkcs12, $certPassword);
    $certString = [System.Convert]::ToBase64String($certBytes)
}
elseif($certSource -eq 1)
{

    Write-Host "Reading the cert file from path $certFilePath"

    $cert = new-object System.Security.Cryptography.X509Certificates.X509Certificate2($certFilePath, $certPassword)
    $certString = [System.Convert]::ToBase64String([System.IO.File]::ReadAllBytes($certFilePath))
}
elseif($certSource -eq 2)
{

    Write-Host "Reading the cert file from Azure Key Vault $KeyVaultName"

    $cert = (Get-AzureKeyVaultSecret -VaultName $KeyVaultName -Name $KeyVaultSecretName)
    $certValue = [System.Convert]::FromBase64String($cert.SecretValueText)
    $certObject = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2 -ArgumentList $certValue, $null,"Exportable, PersistKeySet"

    $certBytes = $certObject.Export([System.Security.Cryptography.X509Certificates.X509ContentType]::Pkcs12, $certPassword.SecretValueText);

    $certString =[System.Convert]::ToBase64String($certBytes)
}

if($addNewCertKeyCredential)
{
    Write-Host "Creating new KeyCredential for the app"
    $keyValue = [System.Convert]::ToBase64String($cert.GetRawCertData())
    New-AzADAppCredential -ApplicationId $appId -CertValue $keyValue -EndDate $cert.NotAfter -StartDate $cert.NotBefore
    Write-Host "Waiting for 7 minutes for the permissions to get propagated"
    Start-Sleep -s 420 #7 minutes
}

Write-Host "Updating the certificate on HDInsight cluster..."

Invoke-AzResourceAction `
    -ResourceGroupName $resourceGroupName `
    -ResourceType 'Microsoft.HDInsight/clusters' `
    -ResourceName $clusterName `
    -ApiVersion '2015-03-01-preview' `
    -Action 'updateclusteridentitycertificate' `
    -Parameters @{ ApplicationId = $appId; Certificate = $certString; CertificatePassword = $certPassword.ToString() } `
    -Force

下一步

在本文中,您已了解如何搭配 HDInsight 使用 HDFS 相容的 Azure Data Lake Storage Gen1。 這個儲存體可讓您建置具有調適性的長期封存資料擷取解決方案。 然後使用 HDInsight 來解除鎖定已儲存的結構化和非結構化資料內資訊。

如需詳細資訊,請參閱