共用方式為


使用網狀架構筆記本搭配 KQL 資料庫的數據

筆記本都是可讀取的檔,其中包含數據分析描述和結果,以及可執行的檔,可執行執行數據分析。 在本文中,您將瞭解如何使用網狀架構筆記本連線到 KQL 資料庫中的數據,並使用原生 KQL 執行查詢(Kusto 查詢語言)。 如需筆記本的詳細資訊,請參閱 如何使用 Microsoft Fabric 筆記本

有兩種方式可以搭配 KQL 資料庫的數據使用 Fabric 筆記本:

必要條件

在筆記本中使用 Kusto 代碼段

網狀架構筆記本提供 代碼段 ,可協助您輕鬆撰寫常用的程序代碼模式。 您可以使用代碼段,使用 KQL 在 KQL 資料庫中寫入或讀取資料。

  1. 流覽至現有的筆記本,或建立新的筆記本。

  2. 在程式代碼數據格中,開始輸入 kusto

    使用 kusto 代碼段在網狀架構筆記本中使用 KQL 的螢幕快照。

  3. 選取對應至您要執行的作業的代碼段:將數據寫入 KQL 資料庫或從 KQL 資料庫讀取數據。

    下列代碼段顯示範例資料讀取作業:

    # Example of query for reading data from Kusto. Replace T with your <tablename>.
    kustoQuery = "['T'] | take 10"
    # The query URI for reading the data e.g. https://<>.kusto.data.microsoft.com.
    kustoUri = "https://<yourKQLdatabaseURI>.z0.kusto.data.microsoft.com"
    # The database with data to be read.
    database = "DocsDatabase"
    # The access credentials.
    accessToken = mssparkutils.credentials.getToken(kustoUri)
    kustoDf  = spark.read\
        .format("com.microsoft.kusto.spark.synapse.datasource")\
        .option("accessToken", accessToken)\
        .option("kustoCluster", kustoUri)\
        .option("kustoDatabase", database)\
        .option("kustoQuery", kustoQuery).load()
    
    # Example that uses the result data frame.
    kustoDf.show()
    

    下列代碼段顯示範例寫入資料作業:

    # The Kusto cluster uri to write the data. The query Uri is of the form https://<>.kusto.data.microsoft.com 
    kustoUri = ""
    # The database to write the data
    database = ""
    # The table to write the data 
    table    = ""
    # The access credentials for the write
    accessToken = mssparkutils.credentials.getToken(kustoUri)
    
    # Generate a range of 5 rows with Id's 5 to 9
    data = spark.range(5,10) 
    
    # Write data to a Kusto table
    data.write.\
    format("com.microsoft.kusto.spark.synapse.datasource").\
    option("kustoCluster",kustoUri).\
    option("kustoDatabase",database).\
    option("kustoTable", table).\
    option("accessToken", accessToken ).\
    option("tableCreateOptions", "CreateIfNotExist").mode("Append").save()
    
  4. 在資料格中每個欄位的引號內輸入必要資訊:

    欄位 描述 相關連結
    kustoQuery 要評估的 KQL 查詢。 KQL 概觀
    KustoUri KQL 資料庫的查詢 URI。 複製 KQL 資料庫 URI
    database KQL 資料庫的名稱。 存取現有的 KQL 資料庫
    資料 要寫入數據表的數據。
  5. 執行程式代碼數據格。

從 KQL 資料庫建立筆記本

當您在 KQL 資料庫中建立筆記本做為相關專案時,筆記本的名稱會與 KQL 資料庫相同,並且會預先填入連線資訊。

  1. 流覽至您的 KQL 資料庫。

  2. 選取 [新增相關項目>筆記本]。

    在 KQL 資料庫中建立筆記本作為相關項目的螢幕快照。

    系統會使用 KustoUri 和預先填入的資料庫詳細數據來建立筆記本。

  3. 在 kustoQuery 字段中輸入要評估的 KQL 查詢

    從 KQL 資料庫建立的筆記本螢幕快照。

  4. 執行程式代碼數據格。