你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
Scripts.ExecuteStoredProcedureStreamAsync 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
重载
ExecuteStoredProcedureStreamAsync(String, PartitionKey, Object[], StoredProcedureRequestOptions, CancellationToken) |
在 Azure Cosmos 服务中以异步操作的形式对容器执行存储过程,并获取流作为响应。 |
ExecuteStoredProcedureStreamAsync(String, Stream, PartitionKey, StoredProcedureRequestOptions, CancellationToken) |
在 Azure Cosmos 服务中以异步操作的形式对容器执行存储过程,并获取流作为响应。 |
ExecuteStoredProcedureStreamAsync(String, PartitionKey, Object[], StoredProcedureRequestOptions, CancellationToken)
- Source:
- Scripts.cs
在 Azure Cosmos 服务中以异步操作的形式对容器执行存储过程,并获取流作为响应。
public abstract System.Threading.Tasks.Task<Microsoft.Azure.Cosmos.ResponseMessage> ExecuteStoredProcedureStreamAsync(string storedProcedureId, Microsoft.Azure.Cosmos.PartitionKey partitionKey, object[] parameters, Microsoft.Azure.Cosmos.Scripts.StoredProcedureRequestOptions requestOptions = default, System.Threading.CancellationToken cancellationToken = default);
abstract member ExecuteStoredProcedureStreamAsync : string * Microsoft.Azure.Cosmos.PartitionKey * obj[] * Microsoft.Azure.Cosmos.Scripts.StoredProcedureRequestOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Microsoft.Azure.Cosmos.ResponseMessage>
Public MustOverride Function ExecuteStoredProcedureStreamAsync (storedProcedureId As String, partitionKey As PartitionKey, parameters As Object(), Optional requestOptions As StoredProcedureRequestOptions = Nothing, Optional cancellationToken As CancellationToken = Nothing) As Task(Of ResponseMessage)
参数
- storedProcedureId
- String
要执行的存储过程的标识符。
- partitionKey
- PartitionKey
项的分区键。
- parameters
- Object[]
表示存储过程的参数的动态对象的数组。 如果不需要任何参数,则可以为 null。
- requestOptions
- StoredProcedureRequestOptions
(可选) 存储过程请求的选项。
- cancellationToken
- CancellationToken
(表示请求取消的可选) CancellationToken 。
返回
表示异步操作的服务响应的任务对象,该操作将包含存储过程中的任何响应集。
例外
如果 storedProcedureId
为 或 partitionKey
未设置。
示例
这会创建并执行一个存储过程,该过程将字符串追加到从查询返回的第一项。
string sprocBody = @"function simple(prefix, postfix)
{
var collection = getContext().getCollection();
// Query documents and take 1st item.
var isAccepted = collection.queryDocuments(
collection.getSelfLink(),
'SELECT * FROM root r',
function(err, feed, options) {
if (err)throw err;
// Check the feed and if it's empty, set the body to 'no docs found',
// Otherwise just take 1st element from the feed.
if (!feed || !feed.length) getContext().getResponse().setBody(""no docs found"");
else getContext().getResponse().setBody(prefix + JSON.stringify(feed[0]) + postfix);
});
if (!isAccepted) throw new Error(""The query wasn't accepted by the server. Try again/use continuation token between API and script."");
}";
Scripts scripts = this.container.Scripts;
string sprocId = "appendString";
StoredProcedureResponse storedProcedureResponse = await scripts.CreateStoredProcedureAsync(
sprocId,
sprocBody);
// Execute the stored procedure
ResponseMessage sprocResponse = await scripts.ExecuteStoredProcedureStreamAsync(
sprocId,
new PartitionKey(testPartitionId),
new dynamic[] {"myPrefixString", "myPostfixString"});
using (StreamReader sr = new StreamReader(sprocResponse.Content))
{
string stringResponse = await sr.ReadToEndAsync();
Console.WriteLine(stringResponse);
}
///
适用于
ExecuteStoredProcedureStreamAsync(String, Stream, PartitionKey, StoredProcedureRequestOptions, CancellationToken)
- Source:
- Scripts.cs
在 Azure Cosmos 服务中以异步操作的形式对容器执行存储过程,并获取流作为响应。
public abstract System.Threading.Tasks.Task<Microsoft.Azure.Cosmos.ResponseMessage> ExecuteStoredProcedureStreamAsync(string storedProcedureId, System.IO.Stream streamPayload, Microsoft.Azure.Cosmos.PartitionKey partitionKey, Microsoft.Azure.Cosmos.Scripts.StoredProcedureRequestOptions requestOptions = default, System.Threading.CancellationToken cancellationToken = default);
abstract member ExecuteStoredProcedureStreamAsync : string * System.IO.Stream * Microsoft.Azure.Cosmos.PartitionKey * Microsoft.Azure.Cosmos.Scripts.StoredProcedureRequestOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Microsoft.Azure.Cosmos.ResponseMessage>
Public MustOverride Function ExecuteStoredProcedureStreamAsync (storedProcedureId As String, streamPayload As Stream, partitionKey As PartitionKey, Optional requestOptions As StoredProcedureRequestOptions = Nothing, Optional cancellationToken As CancellationToken = Nothing) As Task(Of ResponseMessage)
参数
- storedProcedureId
- String
要执行的存储过程的标识符。
- streamPayload
- Stream
包含 Stream 有效负载的 ,该有效负载应表示 JSON 数组或参数的 arraylike 对象。 这是使用 JSON.parse 进行分析的,Function.apply 使用结果调用存储过程。 如果不需要任何参数,则可以为 null。
- partitionKey
- PartitionKey
项的分区键。
- requestOptions
- StoredProcedureRequestOptions
(可选) 存储过程请求的选项。
- cancellationToken
- CancellationToken
(表示请求取消的可选) CancellationToken 。
返回
表示异步操作的服务响应的任务对象,该操作将包含存储过程中的任何响应集。 如果 streamPayload 表示 JSON 数组、对象或 null 以外的任何内容,则响应将包含状态代码 (400) BadRequest。
例外
如果 storedProcedureId
为 或 partitionKey
未设置。
示例
这会创建并执行一个存储过程,该过程将字符串追加到从查询返回的第一项。
string sprocBody = @"function simple(prefix, postfix)
{
var collection = getContext().getCollection();
// Query documents and take 1st item.
var isAccepted = collection.queryDocuments(
collection.getSelfLink(),
'SELECT * FROM root r',
function(err, feed, options) {
if (err)throw err;
// Check the feed and if it's empty, set the body to 'no docs found',
// Otherwise just take 1st element from the feed.
if (!feed || !feed.length) getContext().getResponse().setBody(""no docs found"");
else getContext().getResponse().setBody(prefix + JSON.stringify(feed[0]) + postfix);
});
if (!isAccepted) throw new Error(""The query wasn't accepted by the server. Try again/use continuation token between API and script."");
}";
Scripts scripts = this.container.Scripts;
string sprocId = "appendString";
StoredProcedureResponse storedProcedureResponse = await scripts.CreateStoredProcedureAsync(
sprocId,
sprocBody);
// Serialize the parameters into a stream
string[] parameters = new string[] { "myPrefixString", "myPostfixString" };
byte[] serializedBytes = JsonSerializer.SerializeToUtf8Bytes(parameters);
MemoryStream streamPayload = new MemoryStream(serializedBytes);
// Execute the stored procedure
ResponseMessage sprocResponse = await scripts.ExecuteStoredProcedureStreamAsync(
sprocId,
streamPayload,
new PartitionKey(testPartitionId));
using (StreamReader sr = new StreamReader(sprocResponse.Content))
{
string stringResponse = await sr.ReadToEndAsync();
Console.WriteLine(stringResponse);
}
///