Condividi tramite


Usare i notebook di Fabric con i dati di un database KQL

I notebook sono entrambi documenti leggibili contenenti descrizioni di analisi dei dati e risultati e documenti eseguibili che possono essere eseguiti per eseguire l'analisi dei dati. Questo articolo illustra come usare un notebook di Fabric per connettersi ai dati in un database KQL ed eseguire query usando KQL (Linguaggio di query Kusto) nativo. Per altre informazioni sui notebook, vedere Come usare i notebook di Microsoft Fabric.

Esistono due modi per usare i notebook di Fabric con i dati del database KQL:

Prerequisiti

Usare frammenti di codice Kusto in un notebook

I notebook di Infrastruttura forniscono frammenti di codice che consentono di scrivere facilmente modelli di codice usati comunemente. È possibile usare frammenti di codice per scrivere o leggere dati in un database KQL usando KQL.

  1. Passare a un notebook esistente o crearne uno nuovo.

  2. In una cella di codice iniziare a digitare kusto.

    Acquisizione dello schermo dell'uso di un frammento kusto per l'uso di KQL in un notebook di Fabric.

  3. Selezionare il frammento che corrisponde all'operazione da eseguire: scrivere dati in un database KQL o Leggere i dati da un database KQL.

    Il frammento di codice seguente mostra l'operazione di lettura dei dati di esempio:

    # 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()
    

    Il frammento di codice seguente mostra l'operazione di scrittura dei dati di esempio:

    # 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. Immettere le informazioni necessarie tra virgolette di ogni campo nella cella di dati:

    Campo Descrizione Collegamenti correlati
    kustoQuery Query KQL da valutare. Panoramica di KQL
    KustoUri URI di query del database KQL. Copiare un URI del database KQL
    database Nome del database KQL. Accedere a un database KQL esistente
    data Dati da scrivere nella tabella.
  5. Eseguire la cella di codice.

Creare un notebook da un database KQL

Quando si crea un notebook come elemento correlato in un database KQL, al notebook viene assegnato lo stesso nome del database KQL e viene prepopolato con le informazioni di connessione.

  1. Passare al database KQL.

  2. Selezionare Nuovo elemento>correlato Notebook.

    Screenshot della creazione di un notebook come elemento correlato in un database KQL.

    Viene creato un notebook con i dettagli di KustoUri e del database prepopolati.

  3. Immettere la query KQL da valutare nel campo kustoQuery .

    Screenshot del notebook creato da un database KQL.

  4. Eseguire la cella di codice.