简体   繁体   English

CosmosDB 在读取时从外部 API 返回数据

[英]CosmosDB return data from external API on read

I am attempting to write an Azure CosmosDB integration (Core SQL Api) that integrates with an external service to provide some of the query data.我正在尝试编写一个 Azure CosmosDB 集成(核心 SQL Api),它与外部服务集成以提供一些查询数据。 As an example, I need a query made on Cosmos DB to convert some of the data returned (eg ID's) by the query into real data by calling an external service via a REST API. This should only happen when querying certain columns.例如,我需要在 Cosmos DB 上进行查询,通过 REST API 调用外部服务,将查询返回的一些数据(例如 ID)转换为真实数据。这应该只在查询某些列时发生。

I initially investigated using a JS stored procedure and/or a UDF to make this external call, but the JS environment seems to be extremely limited and doesn't provide any way to make external calls.我最初调查使用 JS 存储过程和/或 UDF 来进行此外部调用,但 JS 环境似乎非常有限,并且不提供任何方式进行外部调用。 I then tried using this https://github.com/Oblarg/cosmosdb-storedprocs-ts repository, which uses webpack to bundle all of node.js into the stored procedure, allowing node modules to be used in stored procedures.然后我尝试使用这个https://github.com/Oblarg/cosmosdb-storedprocs-ts存储库,它使用 webpack 将所有 node.js 捆绑到存储过程中,允许在存储过程中使用节点模块。 Whilst this does allow some node modules to be used, whenever I try and use "https", "fetch", or "axios" modules to make an HTTP GET request I get errors (the same code works fine in a normal node environment, but I'm not a JS expert and can't seem to work past these errors).虽然这确实允许使用一些节点模块,但每当我尝试使用“https”、“fetch”或“axios”模块来发出 HTTP GET 请求时,我都会收到错误消息(相同的代码在正常的节点环境中工作正常,但我不是 JS 专家,似乎无法解决这些错误)。 After a day of attempts it seems like the stored procedure approach is not possible.经过一天的尝试,存储过程方法似乎是不可能的。

Is this the case or is there some way of making HTTP GET requests from a JS stored procedure?是这种情况还是有某种方法可以从 JS 存储过程发出 HTTP GET 请求? If not possible with stored procedures, are there any other techniques to achieve the requirement of reading data from a remote API when querying cosmos DB?如果存储过程不行,请问有没有其他技术可以实现查询cosmos DB时从远程API读取数据的需求?

Thanks谢谢

There is no way to achieve this from CosmosDB directly, for queries you also cannot use the change feed as the document dont change, so really your only option is to use a function or some preprocessor app to handle it, as you say its not ideal but there is no other solution here.没有办法直接从 CosmosDB 实现这一点,对于查询,您也不能使用更改提要,因为文档没有更改,所以实际上您唯一的选择是使用 function 或某些预处理器应用程序来处理它,正如您所说,这并不理想但这里没有其他解决方案。 If it was an insert or an update then change feed would allow you to do this but for plain queries its not possible.如果它是插入或更新,则更改提要将允许您执行此操作,但对于普通查询而言,这是不可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM