筆記本都是可讀取的文件,其中包含資料分析描述和結果,以及可執行進行資料分析的可執行檔。 在本文中,您將瞭解如何使用 Fabric 筆記本連線到 KQL 資料庫中的資料,並使用原生 KQL(Kusto 查詢語言)執行查詢。 如需有關筆記本的詳細資訊,請參閱 如何使用 Microsoft Fabric 筆記本。
有幾種方式可在 Fabric 筆記本中使用來自 KQL 資料庫的資料:
必要條件
從 OneLake 目錄載入 KQL 資料庫資料
OneLake 目錄中的 KQL 資料庫可作為筆記本環境的資料來源使用。 你可以把 KQL 資料庫當作資料來源加入筆記本,然後在程式碼格中引用它來執行 KQL 查詢。
在你的工作區中,切換到現有筆記本或建立新筆記本。
在筆記本的總管窗格中,選擇 新增資料項目 ,然後選擇 從 OneLake 目錄中選出。
在 OneLake 目錄中,前往您的 KQL 資料庫。 你可以在它所屬的 Eventhouse 下找到它,也可以依類型篩選列表並選擇 KQL 資料庫類型,或者你可以依名稱搜尋。
KQL 資料庫會在筆記本的總管窗格中新增至 OneLake 底下。
資料表中的資料會載入筆記本中的新程式碼格,並自動建立與 KQL 資料庫的連線。 接著你可以執行程式碼儲存格來查詢資料,並在筆記本中回傳結果。
欲了解更多關於筆記本中 KQL 資料體驗及資料分析選項的資訊,請參見 xxxxxxx。
在筆記本中使用 Kusto 程式碼片段
Fabric 筆記本提供程式碼片段,讓您輕鬆撰寫常用的程式碼範例。 您可以使用程式碼片段,在 KQL 資料庫中使用 KQL 來寫入或讀取資料。
瀏覽至現有筆記本或建立新的筆記本。
在程式碼儲存格中,開始輸入 kusto。
選取與您要執行的作業對應的片段:將資料寫入 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('kusto') 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('kusto') # 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()在資料格中每個欄位的引號內輸入必要資訊:
欄位 描述 相關連結 kustoQuery 待評估的 KQL 查詢。 KQL 概觀 KustoUri 用來查詢 KQL 資料庫的 URI。 複製 KQL 資料庫 URI 資料庫 您的 KQL 資料庫名稱。 存取現有 KQL 資料庫 資料 要寫入資料表的資料。 執行程式碼儲存格。
從 KQL 資料庫建立筆記本
當您在 KQL 資料庫中建立筆記本作為相關項目時,筆記本的名稱會與 KQL 資料庫相同,並且會預先填入連線資訊。
瀏覽至您的 KQL 資料庫。
選取 新增相關項目>筆記本。
系統會建立筆記本,並預先填入 KustoUri 和資料庫詳細資訊。
在 kustoQuery 欄位中輸入要評估的 KQL 查詢。
執行程式碼儲存格。