In this post we will see how to create app to use the Power BI REST APIs for Streaming Data.
Full documentation: https://powerbi.microsoft.com/documentation/powerbi-service-real-time-streaming/
To run this app follow the steps discussed in my previous post: [Real-Time Dashboard Tile & Streaming Dataset– in Power BI]
Summary as below:
- Go to app.powerbi.com
- Go to streaming data management page by via new dashboard > Add tile > Custom Streaming Data > manage data
- Click “Add streaming dataset”
- Select API, then Next, and give your streaming dataset a name
- Add a field with name “Customer ID”, type Number
- Add a field with name “Customer Name”, type Text
- Add a field with name “Sales Value”, type Number
- Click “Create”
- Copy the “push URL” and paste it as the value of “realTimePushURL” in below app
We will start with new project in Visual Studio.
Create a new Visual C# Console Application.
Open the Program.cs File and write a code as shown below.
This app Uses the WebRequest sample code as documented here: https://msdn.microsoft.com/en-us/library/debx8sh9(v=vs.110).aspx
For your easy here is the code of Program.cs below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
namespace RealTimeStreaming
{
class Program
{
// Paste your own push URL below as obtained from while creating Streaming Dataset and saved in step 9 above
private static string realTimePushURL = “https://api.powerbi.com/beta/4e7ca966-123e-4ce7-9833-3e858854b98f/datasets/7d154cd7-11e0-4d5e-9569-12fa3f82f224/rows?key=mD1nkJOf426PjPPaEQsW9xEg%2FN1EENQ2hRZvXIpHr%2BTXNk3XQpKsR2Jbe5CATiMoLmxjlzSp%2FIMlbe9HL8G4xQ%3D%3D”;
static void Main(string[] args)
{
while (true) { //Set Infinite Loop
try
{
// Declare values that we will be sending
Random r = new Random();
int currentValue = r.Next(0, 100);
String Name = “Dummy Name”;
// Send POST request to the push URL
WebRequest request = WebRequest.Create(realTimePushURL);
request.Method = “POST”;
//Here you will retrieve the data from the source and format as per the request.
//In this example we are sending Random Value generated by above code for testing purpose.
string postData = String.Format(“[{{ \”Customer ID\”: {0}, \”Sales Value\”:{1} }}]”, currentValue,currentValue);
Console.WriteLine(String.Format(“Making POST request with data: {0}”, postData));
// Prepare request for sending
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
// Get the response.
WebResponse response = request.GetResponse();
// Display the status.
Console.WriteLine(String.Format(“Service response: {0}”, ((HttpWebResponse)response).StatusCode));
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
Console.WriteLine(responseFromServer);
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
// Wait 5 second before sending
System.Threading.Thread.Sleep(5000);
} //Infinite Loop ends here.
}
}
}
Compile and Run the Program.
Leave the Program Running and switch to Power BI dashboard. You will see your newly created Tile in previous post will be displaying the Random Value generated by this program updating every 5 seconds.
That’s all with little tweaking to this program you can fetch your data and send the updated data to your Real Time Streaming Dataset.
That’s end to this post.
I will come up with more details in my upcoming posts.
Till then keep Exploring and learning.
You must be logged in to post a comment.