使用Power BI API 向流數據集推送實時數據並在儀表板可視化


使用Power BI 實現實時數據的可視化是大家比較關心的一個話題,在儀表盤上實現推送數據的展示,可以在諸如指揮大屏等場景下使用。

 

本視頻實戰內容如下: https://v.qq.com/x/page/y3030euh6do.html

先看下效果,下圖中的曲線會自動刷新:

 

步驟如下:

  1. 創建流數據集,選擇API 方式

    其中Azure 流分析,截至到2019年12月,中國區Azure流分析暫時不支持將輸出直接寫入到Power BI 中。

     

  2. 填寫數據集名稱和值及值類型並打開歷史數據分析:

    其中歷史數據分析是用來暫存數據的,暫存的數據可以呈現一條曲線。

     

  3. 創建一個儀表盤並向儀表盤添加一個實時數據磁貼

     

 

4. 選擇已經創建好的流數據集

 

5. 在儀表板頁面添加一個自定義的流數據磁貼

可視化效果選擇折線圖

“軸”選擇時間

溫度濕度添加為“值”

6. 通過如下圖示的信息調用Post請求即可將數據推送到數據集

 

Postman發送的結果為200表示執行成功。

7. 在數據集上創建報表,可以查閱使用POST請求推送到流數據集的結果

 

 

8.調用示例代碼如下:

using Newtonsoft.Json;
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace pushdatatopowerbidataset
{
    class Program
    {

        private static int s_telemetryInterval = 1; // Seconds
        private static string PowerBIPushDataUrl = "https://api.powerbi.cn/beta/729c6bf9-debe-4b7f-b56a-5fb0c70c9a80/datasets/fc445a3c-9a25-4298-8188-89112874e5c3/rows?key=seAORXugMKybekrdRAxfSWM5o1MS%2F9d4pcPF9zAgblivdNXz9pRivqyVwAS%2FXMoo8wA01vuAu%2B2hBHI8gdAWMg%3D%3D";

       private static void Main(string[] args)
        {
            Console.WriteLine("Send realtime data to power bi dataset by api. Ctrl-C to exit.\n");



            SendMessageToPbiDataSetAsync();
            Console.ReadLine();


        }

        private static async void SendMessageToPbiDataSetAsync()
        {
           

            while (true)
            {
                // Initial telemetry values
                double minTemperature = 20;
                double minHumidity = 60;
                Random rand = new Random();

                double currentTemperature = minTemperature + rand.NextDouble() * 15;
                double currentHumidity = minHumidity + rand.NextDouble() * 20;

                // Create JSON message
                var telemetryDataPoint = new
                {
                    temperature = currentTemperature,
                    humidity = currentHumidity,
                    time=DateTime.Now
                };
                var messageString = JsonConvert.SerializeObject(telemetryDataPoint);


                PostUrlAsync(PowerBIPushDataUrl, messageString);

                await Task.Delay(s_telemetryInterval * 1000);

            }
        }



        public static string PostUrlAsync(string url, string postData)
        {
            string result = "";

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

            req.Method = "POST";

            req.Timeout = 8000;//設置請求超時時間,單位為毫秒

            req.ContentType = "application/json";

            byte[] data = Encoding.UTF8.GetBytes("["+ postData+"]");

            req.ContentLength = data.Length;

            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);

                reqStream.Close();
            }

            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

            Stream stream = resp.GetResponseStream();

            //獲取響應內容
            
            if(resp.StatusCode==HttpStatusCode.OK)
            {
                Console.WriteLine("OK"+"    "+postData);
            }


            return result;
        }

    }

   
}

 



至此,可以在儀表板上看到實時刷新的可視化效果:

 

 

關注公眾號,請識別如下二維碼: 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM