0

Hello dear PBI REST API experts, I would appreciate any support on the following issue. Trying to send data from MongoDB Atlas to Power BI Push Dataset through Rest API. However I met the following issue, when I'm making more than 5 post http requests to PBI Rest API it says 'Error: connect: exceeded the limit of 5 sockets'. I found the following link on this website, however seems like solution was not found there: Close socket connection in request.js

function postDataToPowerBi(result) {
...    
              collection.findOne({"_id": item._id}).then(itemDoc => {
                postOne(itemDoc, access_token);
              });
...
  }

  function postOne(itemDoc, access_token){

      var postData = JSON.stringify({
          "rows": [
            {
              "Name": itemDoc.Name,
              "name.1": itemDoc.name
            }
          ]
      });
      
      // request option
      var options = {
        host: 'api.powerbi.com' ,
        port: 443,
        method: 'POST',
        path: '/v1.0/myorg/groups/' + group_id + '/datasets/' + dataset_id + '/tables/' + table_name + '/rows',
        headers: {
          'Content-Type': 'application/json',
          'Authorization': 'Bearer ' + access_token,
          // 'Content-Length': postData.length,
          'Connection': 'Close'
        }
      };
      // request object
      var req = https.request(options, function (res) {
        var result = '';
        res.on('data', function (chunk) {
          result += chunk;
        });
        res.on('end', function () {
          console.log(res.statusCode);

        });
        res.on('error', function (err) {
          console.log(err);
        });
      });
      
      // req error
      req.on('error', function (err) {
        console.log(err);
      });
      
      req.on('close', function () {
        console.log("request closed");
      });
      
      //send request witht the postData form
      req.write(postData);
      req.end();
  }
katilinas
  • 11
  • 2

1 Answers1

0

Found explanation for the question posted. Power BI has 5 max pending POST rows requests per dataset for push datasets. More information here: https://learn.microsoft.com/en-us/power-bi/developer/embedded/push-datasets-limitations Hence the only and much better approach is to post rows in bulk instead of line by line. Example below:

function postBulk(docs, access_token, firstRun){
      
      if(firstRun){
        deleteRows(access_token);
      }
      
      var postData = '{"rows": ' + JSON.stringify(docs) + '}';
      
      // console.log(postData);
      
      // request option
      var options = {
        host: 'api.powerbi.com' ,
        port: 443,
        method: 'POST',
        path: '/v1.0/myorg/groups/' + group_id + '/datasets/' + dataset_id + '/tables/' + table_name + '/rows',
        headers: {
          'Content-Type': 'application/json',
          'Authorization': 'Bearer ' + access_token,
          // 'Content-Length': postData.length,
          // 'Connection': 'Close'
        }
      };
      // request object
      var req = https.request(options, function (res) {
        var result = '';
        res.on('data', function (chunk) {
          result += chunk;
        });
        res.on('end', function () {
          console.log("postBulk: " + res.statusMessage);

        });
        res.on('error', function (err) {
          console.log("postBulk: " + err);
        });
      });
      
      // req error
      req.on('error', function (err) {
        console.log("postBulk: " + err);
      });
      
      // req.on('close', function () {
      //   console.log("request closed");
      // });
      
      //send request witht the postData form
      req.write(postData);
      req.end();
  }
katilinas
  • 11
  • 2