分享方式:


整合 OneLake 與 Azure Synapse Analytics

Azure Synapse 是一項無限的分析服務,可將企業資料倉儲與巨量資料分析彙集在一起。 本教學課程示範如何使用 Azure Synapse Analytics 連線到 OneLake

使用 Apache Spark 從 Synapse 寫入數據

請遵循下列步驟,使用 Apache Spark 從 Azure Synapse Analytics 將範例數據寫入 OneLake。

  1. 開啟 Synapse 工作區,並使用 您慣用的參數建立 Apache Spark 集 區。

    Screenshot showing where to select New in the Apache Spark pool screen.

  2. 建立新的 Apache Spark 筆記本。

  3. 開啟筆記本,將語言設定為 PySpark (Python),並將其連線到您新建立的 Spark 集區。

  4. 在不同的索引標籤中,流覽至您的 Microsoft Fabric lakehouse,並尋找最上層 的 Tables 資料夾。

  5. 以滑鼠右鍵按兩下 [ 資料表] 資料夾,然後選取 [ 屬性]。

    Screenshot showing where to open the Properties pane lakehouse explorer.

  6. 從屬性窗格複製 ABFS 路徑

    Screenshot showing where to copy the ABFS path.

  7. 回到 Azure Synapse 筆記本,在第一個新的程式代碼數據格中,提供 Lakehouse 路徑。 此 Lakehouse 是稍後寫入數據的位置。 執行資料格。

    # Replace the path below with the ABFS path to your lakehouse Tables folder. 
    oneLakePath = 'abfss://WorkspaceName@onelake.dfs.fabric.microsoft.com/LakehouseName.lakehouse/Tables'
    
  8. 在新的程式代碼數據格中,將數據從 Azure 開啟的數據集載入資料框架。 此數據集是您載入 Lakehouse 的數據集。 執行資料格。

    yellowTaxiDf = spark.read.parquet('wasbs://nyctlc@azureopendatastorage.blob.core.windows.net/yellow/puYear=2018/puMonth=2/*.parquet')
    display(yellowTaxiDf.limit(10))
    
  9. 在新的程式代碼數據格中,篩選、轉換或準備您的數據。 在此案例中,您可以修剪數據集以加快載入速度、與其他數據集聯結,或篩選為特定結果。 執行資料格。

    filteredTaxiDf = yellowTaxiDf.where(yellowTaxiDf.tripDistance>2).where(yellowTaxiDf.passengerCount==1)
    display(filteredTaxiDf.limit(10))
    
  10. 在新的程式代碼數據格中,使用您的 OneLake 路徑,將篩選的數據框架寫入 Fabric Lakehouse 中的新 Delta-Parquet 數據表。 執行資料格。

    filteredTaxiDf.write.format("delta").mode("overwrite").save(oneLakePath + '/Taxi/')
    
  11. 最後,在新的程式代碼數據格中,從 OneLake 讀取新載入的檔案,測試您的資料是否已成功寫入。 執行資料格。

    lakehouseRead = spark.read.format('delta').load(oneLakePath + '/Taxi/')
    display(lakehouseRead.limit(10))
    

恭喜! 您現在可以在 Azure Synapse Analytics 中使用 Apache Spark 在 OneLake 中讀取和寫入數據。

使用 SQL 從 Synapse 讀取數據

請遵循下列步驟,使用 SQL 無伺服器從 Azure Synapse Analytics 讀取 OneLake 的數據。

  1. 開啟 Fabric Lakehouse,並識別您想要從 Synapse 查詢的數據表。

  2. 以滑鼠右鍵按鍵表,然後選取 [ 屬性]。

  3. 複製數據表的ABFS路徑

    Screenshot showing where to copy the ABFS path.

  4. 在 Synapse Studio開啟您的 Synapse 工作區。

  5. 建立新的 SQL 腳本。

  6. 在 SQL 查詢編輯器中,輸入下列查詢,並將 取代 ABFS_PATH_HERE 為您稍早複製的路徑。

    SELECT TOP 10 *
    FROM OPENROWSET(
    BULK 'ABFS_PATH_HERE',
    FORMAT = 'delta') as rows;
    
  7. 執行查詢以檢視資料表的前 10 個數據列。

恭喜! 您現在可以在 Azure Synapse Analytics 中使用 SQL 無伺服器從 OneLake 讀取資料。