【问题标题】:Accessing Azure Synapse Analytics from Node.js Function从 Node.js 函数访问 Azure Synapse Analytics
【发布时间】:2021-06-28 05:01:44
【问题描述】:

我正在尝试通过 Node.js 函数访问 Azure Synapse SQL 池。我以article 为基础,将身份验证方法切换为“azure-active-directory-msi-app-service”。

如果我运行该函数,我可以在 Function cmd 中看到 queryText context.log,但不幸的是没有其他任何反应。它运行时没有任何输出。

我看不到任何其他 context.log 或从函数中获得任何响应。

const Request = require('tedious').Request;
const TYPES = require('tedious').TYPES;
const test = require('lodash')

module.exports = function (context, req) {
   //get the Query Parameter

   // Create connection to database
   const config = {
     authentication: {
         type: "azure-active-directory-msi-app-service"
     },
     server: "database.sql.azuresynapse.net", 
     options: {
         database: "database", 
         encrypt: true,
         port: 1433
     }
   };

   const connection = new Connection(config);

   // Create array to store the query results
   let result = [];
   let rowData = {};

   // req.query.color will be passed as a query variable in the URL
   //const payload = [req.query.color];

   // Create query to execute against the database
   const queryText = "SELECT TOP (10) [id],[column1],[column2] FROM [dbo].[Table]";
   context.log(queryText);
   
   // Create Request object
   request = new Request(queryText, function(err) {
       context.log("Request")
       if (err) {
           // Error in executing query
           context.log.error(err);
           context.res.status = 500;
           context.res.body = "Error executing the query";
       } else {
           context.res = {
               status: 200,
               isRaw: true,
               body: result,
               headers: {
                   'Content-Type': 'application/json'
               }
           }
       }
       context.done();
   });

   // Manipulate the results and create JSON
   request.on('row', function(columns) {
       rowData = {};
       columns.forEach(function(column) {
           // IMPORTANT: Change the conversion logic here to adjust JSON format
           rowData[column.metadata.colName] = column.value;
       });
       result.push(rowData);
   });

   connection.on('connect', function(err) {
       if (err) {
           // Error in connecting
           context.log.error(err);
           context.res.status = 500;
           context.res.body = "Error connecting to Azure Synapase";
           context.done();
       } else {
           // Connection succeeded
           connection.execSql(request);
       }
   });
}``` 

【问题讨论】:

    标签: azure azure-functions azure-sql-database azure-synapse tedious


    【解决方案1】:

    这应该可以...

    const Connection = require('tedious').Connection;
    const Request = require('tedious').Request;
    const TYPES = require('tedious').TYPES;
    
    const config = {
        server: "SQL pool endpoint goes here",
        authentication: {
            type: "azure-active-directory-msi-app-service"
        },
        options: {
            encrypt: true,
            database: "pool name goes here",
            port: 1433
        }
    }
    var connection = new Connection(config);  
    

    这假定您在 SQL 池中将托管身份适当地分配为外部用户。我也有此更改的 PR,您可以跟踪 here

    【讨论】:

      猜你喜欢
      • 2021-02-17
      • 2021-06-10
      • 2021-10-18
      • 2020-09-05
      • 2021-01-14
      • 2022-06-23
      • 2020-08-16
      • 2021-08-25
      • 2021-05-05
      相关资源
      最近更新 更多