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

640?wx_fmt=png

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


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




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

640?wx_fmt=png


步驟如下:

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

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

    640?wx_fmt=png


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

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


    640?wx_fmt=png

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


    640?wx_fmt=png


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


640?wx_fmt=png

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

可視化效果選擇折線圖

“軸”選擇時間

溫度溼度添加爲“值”

640?wx_fmt=png

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

640?wx_fmt=png


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

640?wx_fmt=png

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

640?wx_fmt=png



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;        }    } }

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

640?wx_fmt=png




640?wx_fmt=gif



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章