从 Azure 触发函数执行 Azure 存储过程
Executing Azure Stored Procedure from Azure Triggering Function
我编写了一个存储过程,用于根据文档的更新日期从 cosmosDB 中删除记录。
cosmosDB 集合有大量(比如 500 个)分区键,因此我必须执行存储过程 500 次。
我正在尝试从 Azure 时间触发函数执行一个 azure 存储过程。
下面是用azure触发函数写的代码:
添加了新文件project.json
{
"frameworks": {
"net46":{
"dependencies": {
"Microsoft.Azure.DocumentDB": "2.0.0"
}
}
}
}
run.csx 文件更新如下。
#r "Microsoft.Azure.Documents.Client"
using System;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
public static async Task Run(TimerInfo myTimer, IEnumerable<dynamic>
inputDocument, TraceWriter log)
{
log.Info($"C# Timer trigger function started at: {DateTime.Now}");
// Get the date 6 months before from Current Time in IST and convert to Epoch value.
TimeZoneInfo INDIAN_ZONE = TimeZoneInfo.FindSystemTimeZoneById("India
Standard Time");
DateTime indianTime =
TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow.AddDays(-180),
INDIAN_ZONE);
long epochTime = (long)(indianTime - new DateTime(1970, 1,
1)).TotalSeconds;
DocumentClient client;
string endpoint = "https://***cosmosdb.documents.azure.com:443/";
string key = "****";
client = new DocumentClient(new Uri(endpoint), key);
foreach (var doc in inputDocument)
{
string partKey = doc.NUM;
StoredProcedureResponse<bool> sprocResponse = await
client.ExecuteStoredProcedureAsync<bool>(
"/dbs/DB_NAME/colls/COLLECTION_NAME/sprocs/STORED PROC_NAME/",new
RequestOptions { PartitionKey = new PartitionKey(partKey) });
log.Info($"Cosmos DB is updated at: {DateTime.Now}");
}
}
编译上述代码时出现以下错误。
2019-08-22T15:30:01.759 [Error] Exception while executing function: Functions.TimerTriggerCSharp1. Microsoft.Azure.WebJobs.Script: One or more errors occurred. Microsoft.Azure.Documents.Client: Failed to deserialize stored procedure response or convert it to type 'System.Boolean': Unexpected character encountered while parsing value: {. Path '', line 1, position 1. Newtonsoft.Json: Unexpected character encountered while parsing value: {. Path '', line 1, position 1.
2019-08-22T15:30:01.822 [Error] Function completed (Failure, Id=02da4d71-8207-4227-9c79-1cc277439c20, Duration=1814ms)
我在执行存储过程时必须传递日期参数和分区键数组。
我完全陷入了这个阶段。可以做些什么来解决这个问题?
报错说明你的Azure Function的脚本代码不正确
删除函数内的所有 private
和 static
声明。
private static DocumentClient client;
static string endpoint;
static string key
==>
DocumentClient client;
string endpoint;
string key;
一般建议:不要在 Azure 门户(.csx)中编写函数。帮自己一个忙,使用合适的 IDE,例如 VS Code。
如果您使用的是 Portal,则需要像这样在函数顶部引入依赖项:
#r "Microsoft.Azure.Documents.Client"
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
请使用更新版本的 SDK,1.7.1 是 3 年多前的:https://www.nuget.org/packages/Microsoft.Azure.DocumentDB/
我编写了一个存储过程,用于根据文档的更新日期从 cosmosDB 中删除记录。 cosmosDB 集合有大量(比如 500 个)分区键,因此我必须执行存储过程 500 次。 我正在尝试从 Azure 时间触发函数执行一个 azure 存储过程。
下面是用azure触发函数写的代码:
添加了新文件project.json
{
"frameworks": {
"net46":{
"dependencies": {
"Microsoft.Azure.DocumentDB": "2.0.0"
}
}
}
}
run.csx 文件更新如下。
#r "Microsoft.Azure.Documents.Client"
using System;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
public static async Task Run(TimerInfo myTimer, IEnumerable<dynamic>
inputDocument, TraceWriter log)
{
log.Info($"C# Timer trigger function started at: {DateTime.Now}");
// Get the date 6 months before from Current Time in IST and convert to Epoch value.
TimeZoneInfo INDIAN_ZONE = TimeZoneInfo.FindSystemTimeZoneById("India
Standard Time");
DateTime indianTime =
TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow.AddDays(-180),
INDIAN_ZONE);
long epochTime = (long)(indianTime - new DateTime(1970, 1,
1)).TotalSeconds;
DocumentClient client;
string endpoint = "https://***cosmosdb.documents.azure.com:443/";
string key = "****";
client = new DocumentClient(new Uri(endpoint), key);
foreach (var doc in inputDocument)
{
string partKey = doc.NUM;
StoredProcedureResponse<bool> sprocResponse = await
client.ExecuteStoredProcedureAsync<bool>(
"/dbs/DB_NAME/colls/COLLECTION_NAME/sprocs/STORED PROC_NAME/",new
RequestOptions { PartitionKey = new PartitionKey(partKey) });
log.Info($"Cosmos DB is updated at: {DateTime.Now}");
}
}
编译上述代码时出现以下错误。
2019-08-22T15:30:01.759 [Error] Exception while executing function: Functions.TimerTriggerCSharp1. Microsoft.Azure.WebJobs.Script: One or more errors occurred. Microsoft.Azure.Documents.Client: Failed to deserialize stored procedure response or convert it to type 'System.Boolean': Unexpected character encountered while parsing value: {. Path '', line 1, position 1. Newtonsoft.Json: Unexpected character encountered while parsing value: {. Path '', line 1, position 1.
2019-08-22T15:30:01.822 [Error] Function completed (Failure, Id=02da4d71-8207-4227-9c79-1cc277439c20, Duration=1814ms)
我在执行存储过程时必须传递日期参数和分区键数组。 我完全陷入了这个阶段。可以做些什么来解决这个问题?
报错说明你的Azure Function的脚本代码不正确
删除函数内的所有 private
和 static
声明。
private static DocumentClient client;
static string endpoint;
static string key
==>
DocumentClient client;
string endpoint;
string key;
一般建议:不要在 Azure 门户(.csx)中编写函数。帮自己一个忙,使用合适的 IDE,例如 VS Code。
如果您使用的是 Portal,则需要像这样在函数顶部引入依赖项:
#r "Microsoft.Azure.Documents.Client"
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
请使用更新版本的 SDK,1.7.1 是 3 年多前的:https://www.nuget.org/packages/Microsoft.Azure.DocumentDB/