Krok č. 3: Vytvorenie množiny údajov v službe Power BIStep 3: Create a dataset in Power BI

Tento článok je súčasťou podrobného postupu na presunutie údajov do množiny údajov.This article is part of a step-by-step walkthrough to push data into a dataset.

V kroku 2 postupu presunutia údajov do množiny údajov, Získanie overovacieho prístupového tokenu, ste získali token určený na overovanie v službe Azure AD.In step 2 of Push data into a dataset, Get an authentication access token, you got a token to authenticate to Azure AD. V tomto kroku token využijete na to, aby ste zavolali operáciu PostDataset.In this step, you use the token to call the PostDataset operation.

Ak chcete zavolať prostriedok REST, použijete URL adresu, ktorá ho vyhľadá, a zašlete reťazec vo formáte JavaScript Object Notation (JSON), ktorým sa popisuje množina údajov prostriedku služby Power BI.To make a call to a REST resource, you use a url that locates the resource, and you send a JavaScript Object Notation (JSON) string, which describes the dataset, to the Power BI service resource. Prostriedok REST identifikuje časť služby Power BI, s ktorou chcete pracovať.A REST resource identifies the part of the Power BI service you want to work with. Ak chcete presunúť údaje do množiny údajov, cieľovým prostriedkom je množina údajov.To push data into the dataset, the target resource is a Dataset. URL adresa, ktorá identifikuje množinu údajov, je https://api.PowerBI.com/v1.0/myorg/datasets.The URL that identifies a dataset is https://api.PowerBI.com/v1.0/myorg/datasets. Ak údaje zapisujete v rámci skupiny, URL adresa je https://api.PowerBI.com/v1.0/myorg/groups/{group_id}/datasets.If you are pushing data within a group, the url is https://api.PowerBI.com/v1.0/myorg/groups/{group_id}/datasets.

Na overenie operácie s prostriedkom REST v službe Power BI môžete do hlavičky požiadavky pridať token, ktorý ste získali v kroku Získanie overovacieho prístupového tokenu:To authenticate a Power BI REST operation, you add the token you got in Get an authentication access token to a request header:

Zavolaním operácie PostDataset sa vytvorí nová množina údajov.When you call the PostDataset operation, a new dataset is created.

vytvorenie množiny údajov

Tu je postup na vytvorenie množiny údajov v službe Power BI.Here's how to create a dataset in Power BI.

Vytvorenie množiny údajov v službe Power BICreate a dataset in Power BI

Poznámka

Skôr než začnete, skontrolujte, či ste v predchádzajúcich krokoch návodu Presunutie údajov do množiny údajov dodržali postup.Before you get started, make sure you have followed the previous steps in the push data into a dataset walkthrough.

  1. V projekte Konzolová aplikácia, ktorý ste vytvorili v kroku 2 – Získanie overovacieho prístupového tokenu, pridajte do súboru Program.cs riadky using System.Net; a using System.IO; .In the Console Application project you created in Step 2 - Get an authentication access token, add using System.Net;, and using System.IO; to Program.cs.
  2. Do súboru Program.cs pridajte nižšie uvedený kód.In Program.cs, add the code below.
  3. Spustite konzolovú aplikáciu a prihláste sa do konta služby Power BI.Run the Console App, and login to your Power BI account. V okne konzoly by ste mali vidieť informáciu Dataset Created (Množina údajov vytvorená).You should see Dataset Created in the Console Window. Okrem toho môžete novú množinu údajov zobraziť po prihlásení do služby Power BI.Also, you can login to Power BI to see the new dataset.

Presunutie ukážky údajov do množiny údajovSample push data into a dataset

Do súboru Program.cs pridajte tento kód.Add this code into Program.cs.

  • V časti static void Main(string[] args):In static void Main(string[] args):

    static void Main(string[] args)
    {
        //Get an authentication access token
        token = GetToken();
    
        //Create a dataset in Power BI
        CreateDataset();
    }
    
  • Pridajte metódu CreateDataset():Add a CreateDataset() method:

    #region Create a dataset in Power BI
    private static void CreateDataset()
    {
        //TODO: Add using System.Net and using System.IO
    
        string powerBIDatasetsApiUrl = "https://api.powerbi.com/v1.0/myorg/datasets";
        //POST web request to create a dataset.
        //To create a Dataset in a group, use the Groups uri: https://api.PowerBI.com/v1.0/myorg/groups/{group_id}/datasets
        HttpWebRequest request = System.Net.WebRequest.Create(powerBIDatasetsApiUrl) as System.Net.HttpWebRequest;
        request.KeepAlive = true;
        request.Method = "POST";
        request.ContentLength = 0;
        request.ContentType = "application/json";
    
        //Add token to the request header
        request.Headers.Add("Authorization", String.Format("Bearer {0}", token));
    
        //Create dataset JSON for POST request
        string datasetJson = "{\"name\": \"SalesMarketing\", \"tables\": " +
            "[{\"name\": \"Product\", \"columns\": " +
            "[{ \"name\": \"ProductID\", \"dataType\": \"Int64\"}, " +
            "{ \"name\": \"Name\", \"dataType\": \"string\"}, " +
            "{ \"name\": \"Category\", \"dataType\": \"string\"}," +
            "{ \"name\": \"IsCompete\", \"dataType\": \"bool\"}," +
            "{ \"name\": \"ManufacturedOn\", \"dataType\": \"DateTime\"}" +
            "]}]}";
    
        //POST web request
        byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(datasetJson);
        request.ContentLength = byteArray.Length;
    
        //Write JSON byte[] into a Stream
        using (Stream writer = request.GetRequestStream())
        {
            writer.Write(byteArray, 0, byteArray.Length);
    
            var response = (HttpWebResponse)request.GetResponse();
    
            Console.WriteLine(string.Format("Dataset {0}", response.StatusCode.ToString()));
    
            Console.ReadLine();
        }
    }
    #endregion
    

V ďalšom kroku sa dozviete, ako získať množinu údajov na pridanie riadkov do tabuľky Power BI.The next step shows you how to get a dataset to add rows into a Power BI table.

Nižšie sa uvádza úplný zoznam kódov.Below is the complete code listing.

Úplný zoznam kódovComplete code listing

using System;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System.Net;
using System.IO;

namespace walkthrough_push_data
{
    class Program
    {
        private static string token = string.Empty;

        static void Main(string[] args)
        {

            //Get an authentication access token
            token = GetToken();

            //Create a dataset in Power BI
            CreateDataset();

        }

        #region Get an authentication access token
        private static string GetToken()
        {
            // TODO: Install-Package Microsoft.IdentityModel.Clients.ActiveDirectory -Version 2.21.301221612
            // and add using Microsoft.IdentityModel.Clients.ActiveDirectory

            //The client id that Azure AD created when you registered your client app.
            string clientID = "{Client_ID}";

            //RedirectUri you used when you register your app.
            //For a client app, a redirect uri gives Azure AD more details on the application that it will authenticate.
            // You can use this redirect uri for your client app
            string redirectUri = "https://login.live.com/oauth20_desktop.srf";

            //Resource Uri for Power BI API
            string resourceUri = "https://analysis.windows.net/powerbi/api";

            //OAuth2 authority Uri
            string authorityUri = "https://login.microsoftonline.com/common/";

            //Get access token:
            // To call a Power BI REST operation, create an instance of AuthenticationContext and call AcquireToken
            // AuthenticationContext is part of the Active Directory Authentication Library NuGet package
            // To install the Active Directory Authentication Library NuGet package in Visual Studio,
            //  run "Install-Package Microsoft.IdentityModel.Clients.ActiveDirectory" from the nuget Package Manager Console.

            // AcquireToken will acquire an Azure access token
            // Call AcquireToken to get an Azure token from Azure Active Directory token issuance endpoint
            AuthenticationContext authContext = new AuthenticationContext(authorityUri);
            string token = authContext.AcquireToken(resourceUri, clientID, new Uri(redirectUri)).AccessToken;

            Console.WriteLine(token);
            Console.ReadLine();

            return token;
        }

        #endregion


        #region Create a dataset in Power BI
        private static void CreateDataset()
        {
            //TODO: Add using System.Net and using System.IO

            string powerBIDatasetsApiUrl = "https://api.powerbi.com/v1.0/myorg/datasets";
            //POST web request to create a dataset.
            //To create a Dataset in a group, use the Groups uri: https://api.PowerBI.com/v1.0/myorg/groups/{group_id}/datasets
            HttpWebRequest request = System.Net.WebRequest.Create(powerBIDatasetsApiUrl) as System.Net.HttpWebRequest;
            request.KeepAlive = true;
            request.Method = "POST";
            request.ContentLength = 0;
            request.ContentType = "application/json";

            //Add token to the request header
            request.Headers.Add("Authorization", String.Format("Bearer {0}", token));

            //Create dataset JSON for POST request
            string datasetJson = "{\"name\": \"SalesMarketing\", \"tables\": " +
                "[{\"name\": \"Product\", \"columns\": " +
                "[{ \"name\": \"ProductID\", \"dataType\": \"Int64\"}, " +
                "{ \"name\": \"Name\", \"dataType\": \"string\"}, " +
                "{ \"name\": \"Category\", \"dataType\": \"string\"}," +
                "{ \"name\": \"IsCompete\", \"dataType\": \"bool\"}," +
                "{ \"name\": \"ManufacturedOn\", \"dataType\": \"DateTime\"}" +
                "]}]}";

            //POST web request
            byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(datasetJson);
            request.ContentLength = byteArray.Length;

            //Write JSON byte[] into a Stream
            using (Stream writer = request.GetRequestStream())
            {
                writer.Write(byteArray, 0, byteArray.Length);

                var response = (HttpWebResponse)request.GetResponse();

                Console.WriteLine(string.Format("Dataset {0}", response.StatusCode.ToString()));

                Console.ReadLine();
            }
        }
        #endregion
    }
}

Ďalší krok >Next Step >

Ďalšie krokyNext steps

Máte ďalšie otázky?More questions? Skúste komunitu služby Power BITry the Power BI Community