使用 Kqlmagic 建立和執行筆記本

Kqlmagic 是可在 Azure Data Studio 筆記本 中擴充 Python 核心功能的命令。 您可結合 Python 和 Kusto 查詢語言 (KQL),使用整合了 render 命令的豐富 Plotly 程式庫來查詢資料,並視覺化資料。 Kqlmagic 可在同一個位置提供筆記本、資料分析和豐富 Python 功能的優點。 Kqlmagic 支援的資料來源包括 Azure 資料總管Application InsightsAzure 監視器記錄

本文說明如何使用 Azure 資料總管叢集的 Kqlmagic 延伸模組、Application Insights 記錄和 Azure 監視器記錄,以在 Azure Data Studio 中建立並執行筆記本。

必要條件

在筆記本中安裝及設定 Kqlmagic

本節中的步驟都會在 Azure Data Studio 筆記本中執行。

  1. 建立新的筆記本,並將核心變更為 Python 3

    Screenshot of a new notebook.

  2. 當您的 Python 套件需要更新時,系統會提示您升級套件。

    Screenshot of the result - yes.

  3. 安裝 Kqlmagic:

    import sys
    !{sys.executable} -m pip install Kqlmagic --no-cache-dir --upgrade
    

    驗證是否已安裝:

    import sys
    !{sys.executable} -m pip list
    

    Screenshot of the list.

  4. 載入 Kqlmagic:

    %reload_ext Kqlmagic
    

    注意

    如果此步驟失敗,請關閉檔案並重新開啟。

    Screenshot of the load the Kqlmagic extension.

  5. 您可瀏覽說明文件或檢查版本來測試是否已正確載入 Kqlmagic。

    %kql --help "help"
    

    注意

    如果 Samples@help 要求輸入密碼,則可將其保留空白,然後按 Enter

    Screenshot of help.

    若要查看安裝的 Kqlmagic 版本,請執行下列命令。

    %kql --version
    

搭配 Azure 資料總管叢集的 Kqlmagic

本節說明如何使用 Kqlmagic 搭配 Azure 資料總管叢集執行資料分析。

載入及驗證適用於 Azure 資料總管的 Kqlmagic

注意

當每次在 Azure Data Studio 中建立新筆記本時,都必須載入 Kqlmagic 延伸模組。

  1. 驗證核心已設為 Python3

    Screenshot of the kernel change.

  2. 載入 Kqlmagic:

    %reload_ext Kqlmagic
    

    Screenshot of the load the Kqlmagic extension.

  3. 連線至叢集並進行驗證:

    %kql azureDataExplorer://code;cluster='help';database='Samples'
    

    注意

    如果您使用自己的 ADX 叢集,則必須在連接字串中包含該區域,如下所示:

    %kql azuredataexplorer://code;cluster='mycluster.westus';database='mykustodb' 請使用裝置登入來進行驗證。 從輸出複製程式碼,然後選取 [驗證],瀏覽器將會開啟,請在其中貼上程式碼。 成功驗證後,您可回到 Azure Data Studio 以繼續進行指令碼的其餘部分。

    Screenshot of the Azure Data Explorer authentication.

針對 Azure 資料總管進行查詢與視覺化

使用 render 運算子查詢資料,並使用 Plotly 程式庫將資料視覺化。 此查詢與視覺效果會提供使用原生 KQL 的整合式體驗。

  1. 依照狀態和頻率來分析前 10 大 Storm 事件:

    %kql StormEvents | summarize count() by State | sort by count_ | limit 10
    

    若熟悉 Kusto 查詢語言 (KQL),則可在 %kql 後面鍵入查詢。

    Screenshot of the analyze storm events.

  2. 將時間軸圖表視覺化:

    %kql StormEvents \
    | summarize event_count=count() by bin(StartTime, 1d) \
    | render timechart title= 'Daily Storm Events'
    

    Screenshot of a time chart.

  3. 使用 %%kql 的多行查詢範例。

    %%kql
    StormEvents
    | summarize count() by State
    | sort by count_
    | limit 10
    | render columnchart title='Top 10 States by Storm Event count'
    

    Screenshot of a multiline Query sample.

Kqlmagic 搭配 Application Insights

載入及驗證適用於 Application Insights 的 Kqlmagic

  1. 驗證核心已設為 Python3

    Screenshot of a kernel.

  2. 載入 Kqlmagic:

    %reload_ext Kqlmagic
    

    Screenshot of loading the Kqlmagic extension.

    注意

    當每次在 Azure Data Studio 中建立新筆記本時,都必須載入 Kqlmagic 延伸模組。

  3. 連線並進行驗證。

    首先,您必須為 Application Insights 資源產生 API 金鑰。 然後,使用應用程式識別碼和 API 金鑰來連線至筆記本的 Application Insights:

    %kql appinsights://appid='DEMO_APP';appkey='DEMO_KEY'
    

針對 Application Insights 進行查詢與視覺化

使用 render 運算子查詢資料,並使用 Plotly 程式庫將資料視覺化。 此查詢與視覺效果會提供使用原生 KQL 的整合式體驗。

  1. 顯示頁面檢視:

    %%kql
    pageViews
    | limit 10
    

    Screenshot of page views.

    注意

    使用滑鼠拖曳圖表的某個區域,以放大特定日期。

  2. 在時間軸圖表中顯示頁面檢視:

    %%kql
    pageViews
    | summarize event_count=count() by name, bin(timestamp, 1d)
    | render timechart title= 'Daily Page Views'
    

    Screenshot of the timeline chart.

Kqlmagic 搭配 Azure 監視器記錄

載入及驗證適用於 Azure 監視器的 Kqlmagic

  1. 驗證核心已設為 Python3

    Screenshot of the change.

  2. 載入 Kqlmagic:

    %reload_ext Kqlmagic
    

    Screenshot showing to load the Kqlmagic extension.

    注意

    當每次在 Azure Data Studio 中建立新筆記本時,都必須載入 Kqlmagic 延伸模組。

  3. 連線並進行驗證:

    %kql loganalytics://workspace='DEMO_WORKSPACE';appkey='DEMO_KEY';alias='myworkspace'
    

    Screenshot of the log analytics authentication.

針對 Azure 監視器進行查詢與視覺化

使用 render 運算子查詢資料,並使用 Plotly 程式庫將資料視覺化。 此查詢與視覺效果會提供使用原生 KQL 的整合式體驗。

  1. 檢視時間軸圖表:

    %%kql
    KubeNodeInventory
    | summarize event_count=count() by Status, bin(TimeGenerated, 1d)
    | render timechart title= 'Daily Kubernetes Nodes'
    

    Screenshot showing the Log Analytics Daily Kubernetes Nodes timechart.

下一步

深入了解筆記本和 Kqlmagic: