步骤 4:获取数据集以将行添加到 Power BI 表Step 4: Get a dataset to add rows into a Power BI table

02/05/2019

本文内容

本文是将数据推送到数据集的分步演练的一部分。This article is part of a step-by-step walkthrough to push data into a dataset.

在将数据推送到数据集的步骤 3(在 Power BI 中创建数据集)中,你调用了创建数据集操作以在 Power BI 中创建数据集。In step 3 of Push data into a dataset, Create a dataset in Power BI, you called the Create Dataset operation to create a dataset in Power BI. 在此步骤中,你将使用获取数据集操作和 Newtonsoft.Json 来获取数据集 ID。在步骤 4 中使用数据集 ID 向数据集添加行。In this step, you use the Get Datasets operation and Newtonsoft.Json to get a dataset id. You use the dataset id in step 4 to add rows to a dataset.

要将数据推送到 Power BI 数据集,需要引用数据集中的表。To push data into a Power BI dataset, you need to reference the table in the dataset. 要引用数据集中的表,首先需要获取数据集 ID。To reference a table in a dataset, you first need to get a Dataset ID. 使用获取数据集操作获取数据集 ID 。You get a Dataset ID using the Get Datasets operation. “获取数据集” 操作将返回一个 JSON 字符串,它包含 Power BI 中所有数据集的列表。The Get Datasets operation returns a JSON string containing a list of all datasets in Power BI. 对 JSON 字符串进行反序列化的推荐的方法是使用 Newtonsoft.Json。The recommended way to deserialize a JSON string is with Newtonsoft.Json.

下面介绍如何获取数据集。Here's how you get a dataset.

获取 Power BI 数据集Get a Power BI dataset

注意: 在开始之前,先确保已按将数据推送到数据集演练中之前的步骤进行了操作。NOTE: Before you get started, make sure you have followed the previous steps in the push data into a dataset walkthrough.

在步骤 2 中创建的控制台应用程序项目中:推送数据演练,获取身份验证访问令牌,安装 Newtonsoft.Json NuGet 包。In the Console Application project you created in Step 2: Walkthrough to push data, Get an authentication access token, install the Newtonsoft.Json NuGet package. 下面介绍了安装此程序包的方法:Here's how to install the package:

a.a. 在 Visual Studio 2015 中,选择工具 > NuGet 包管理器 > 程序包管理器控制台。In Visual Studio 2015, choose Tools > NuGet Package Manager > Package Manager Console.

b.b. 在包管理器控制台中,输入 Install-Package Newtonsoft.Json。In Package Manager Console, enter Install-Package Newtonsoft.Json.

安装包后,将 using Newtonsoft.Json; 添加到 Program.cs。After the package is installed, add using Newtonsoft.Json; to Program.cs.

在 Program.cs 中,添加以下代码以获取数据集 ID。In Program.cs, add the code below to get a Dataset ID.

运行控制台应用,并登录到你的 Power BI 帐户。Run the Console App, and login to your Power BI account. 应可在控制台窗口中看到数据集ID: 后跟 ID。You should see Dataset ID: followed by an id in the Console Window.

获取数据集示例Sample get a dataset

将此代码添加到 Program.cs。Add this code into Program.cs.

在 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();

//Get a dataset to add rows into a Power BI table

string datasetId = GetDataset();

}

添加 GetDatset() 方法 ︰Add a GetDatset() method:

#region Get a dataset to add rows into a Power BI table

private static string GetDataset()

{

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 = "GET";

request.ContentLength = 0;

request.ContentType = "application/json";

//Add token to the request header

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

string datasetId = string.Empty;

//Get HttpWebResponse from GET request

using (HttpWebResponse httpResponse = request.GetResponse() as System.Net.HttpWebResponse)

{

//Get StreamReader that holds the response stream

using (StreamReader reader = new System.IO.StreamReader(httpResponse.GetResponseStream()))

{

string responseContent = reader.ReadToEnd();

//TODO: Install NuGet Newtonsoft.Json package: Install-Package Newtonsoft.Json

//and add using Newtonsoft.Json

var results = JsonConvert.DeserializeObject(responseContent);

//Get the first id

datasetId = results["value"][0]["id"];

Console.WriteLine(String.Format("Dataset ID: {0}", datasetId));

Console.ReadLine();

return datasetId;

}

}

}

#endregion

完整代码清单Complete code listing

using System;

using Microsoft.IdentityModel.Clients.ActiveDirectory;

using System.Net;

using System.IO;

using Newtonsoft.Json;

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

//Get a dataset to add rows into a Power BI table

string datasetId = GetDataset();

}

#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

#region Get a dataset to add rows into a Power BI table

private static string GetDataset()

{

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 = "GET";

request.ContentLength = 0;

request.ContentType = "application/json";

//Add token to the request header

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

string datasetId = string.Empty;

//Get HttpWebResponse from GET request

using (HttpWebResponse httpResponse = request.GetResponse() as System.Net.HttpWebResponse)

{

//Get StreamReader that holds the response stream

using (StreamReader reader = new System.IO.StreamReader(httpResponse.GetResponseStream()))

{

string responseContent = reader.ReadToEnd();

//TODO: Install NuGet Newtonsoft.Json package: Install-Package Newtonsoft.Json

//and add using Newtonsoft.Json

var results = JsonConvert.DeserializeObject(responseContent);

//Get the first id

datasetId = results["value"][0]["id"];

Console.WriteLine(String.Format("Dataset ID: {0}", datasetId));

Console.ReadLine();

return datasetId;

}

}

}

#endregion

}

}

后续步骤Next steps

powerbi输入数据_获取数据集以添加行 - Power BI | Microsoft Docs相关推荐

  1. linux系统运行powerbi,使用 Power BI 服务 - Power BI | Microsoft Docs

    快速入门 - 使用 Power BI 服Quickstart - Getting around in Power BI service 10/12/2020 本文内容 备注 Power BI 正在转换 ...

  2. 注销共享服务器登录,登录和注销 - Power BI | Microsoft Docs

    登录 Power BI 服务 05/15/2021 本文内容 适用对象: 面向业务用户的 Power BI 服务 面向设计者和开发人员的 Power BI 服务 Power BI Desktop 需要 ...

  3. powerbi输入数据_如何用Power BI对数据建模?

    1.项目案例 这是一家咖啡店的数据,Excel里有两个表,分别是销售数据表.产品表. 销售数据表记录了咖啡订单,包括字段:订单编号.订日期.门店.产品ID.顾客.数量. 产品表中记录了咖啡的种类与价格 ...

  4. power bi 创建空表_如何使用R在Power BI中创建地理地图

    power bi 创建空表 介绍 (Introduction) This is the fifth article of a series dedicated to discovering geogr ...

  5. mysql2005备份_创建完整数据库备份 - SQL Server | Microsoft Docs

    完整数据库备份Create a Full Database Backup 09/12/2019 本文内容 适用于:Applies to: SQL ServerSQL Server(所有支持的版本)SQ ...

  6. restfull服务器端获取文件,使用 FileREST API (获取文件服务) - Azure 文件存储 | Microsoft Docs...

    您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn. 获取文件服务属性 06 ...

  7. sql server java类型_使用基本 JDBC 数据类型 - SQL Server | Microsoft Docs

    使用基本数据类型Using basic data types 01/29/2021 本文内容 Microsoft JDBC Driver for SQL ServerMicrosoft JDBC Dr ...

  8. access里的多步oledb错误_(ADO) 的 ConnectionString 属性 - SQL Server | Microsoft Docs

    ConnectionString 属性 (ADO)ConnectionString Property (ADO) 01/19/2017 本文内容 指示用于建立与数据源的连接的信息.Indicates ...

  9. access2007 定义了过多字段_定义了过多的字段错误 | Microsoft Docs

    在 Access 中保存表时出现 "定义过多的字段" 错误 2020/5/22 适用于: Access for Office 365, Access 2019, Access 20 ...

最新文章

  1. 我记录网站综合系统 -- 技术原理解析[10:PermissionChecker流程]
  2. Error:Java home supplied via 'org.gradle.java.home' is invalid
  3. 截取指定字符前_Excel应该怎么学--表格维护常用文本函数(截取、转换、连接)...
  4. Java 基本类型相互转换
  5. js页面加载前执行_做一名合格的前端开发工程师:Javascript加载执行问题探索
  6. PPC(Pocket PC)中显示二进制数组(byte[])类型的图片
  7. Android studio下载安装使用遇到的问题及解决办法
  8. 安卓3.0之后的网络访问问题
  9. Python 和curl 调用sendcloud发送邮件
  10. 图像椒盐噪声和高斯噪声
  11. 防止sql拼接的Java方法_JAVA程序防止SQL注入的方法
  12. qss设置平面按钮_QToolButton设置QSS
  13. python导入鸢尾花数据集_决策树分类鸢尾花数据集python实现
  14. openwrt路由器(红米AC2100)折腾全程——多拨、ipv6负载均衡、ipv6 nat6、ddns、端口转发
  15. OpenFOAM+petsc
  16. QT使用RabbitMQ
  17. vue js 清除 data数据
  18. 英语写计算机作文600字,介绍电脑 computer 英语作文
  19. 关于springboot+simbot+mriai实现QQ群智能回复机器人
  20. 双电子两个格点Hubbard模型严格对角化

热门文章

  1. 新闻微软谷歌曾私下表达反对博通收购高通 害怕苹果更强
  2. 聚“慧”金融,华为云如何念好AI这本经?
  3. 博客写作小技巧【1】:如何设置字体大小、颜色和字体类型!
  4. 双线性插值法(Bilinear Interpolation)
  5. 题目:类Test1、类Test2定义如下:
  6. 【摸鱼吃瓜工作录】ctrl+左键之让你快到飞起的--列编辑
  7. 海内外技术人们“看”音视频技术的未来
  8. 八年级计算机课学期末心得体会,八年级信息技术课工作总结范文
  9. 腾讯云轻量型服务器安装Nginx且SSL证书配置实现HTTP和HTTPS连接
  10. PHP中for循环语句的几种“变态”用法