我在使用.proc客户端使用聚合storedproc lumenize https://github.com/lmaccherone/documentdb-lumenize时遇到问题。尝试将参数和查询传递给storedproc时出错。以下是我的代码
public async static void QuerySP() {
using (client = new DocumentClient(new Uri(endpointUrl), authorizationKey))
{
//Get the Database
var database = client.CreateDatabaseQuery().Where(db => db.Id == databaseId).ToArray().FirstOrDefault();
//Get the Document Collection
var collection = client.CreateDocumentCollectionQuery(database.SelfLink).Where(c => c.Id == collectionId).ToArray().FirstOrDefault();
StoredProcedure storedProc = client.CreateStoredProcedureQuery(collection.StoredProceduresLink).Where(sp => sp.Id == "cube").ToArray().FirstOrDefault();
dynamic result = await client.ExecuteStoredProcedureAsync<dynamic>(storedProc.SelfLink, "{cubeConfig: {groupBy: 'publication', field: 'pid', f: 'count'}, filterQuery: 'SELECT pid, publication FROM c'}");
Console.WriteLine("Result from script: {0}\r\n", result.Response);
}
}
执行代码时出现以下错误
消息:{“错误”:[“执行Javascript时遇到异常。异常=错误:需要cubeConfig或savedCube \ r \ n堆栈跟踪:错误:在fn处需要cubeConfig或savedCube \ n(cube.js:1803:7 )\ n at __docDbMain(cube.js:1844:5)\ n at Unknown script code(cube.js:1:2)“]}
不确定我做错了什么。我真的很感激帮助。感谢。
答案 0 :(得分:1)
你几乎拥有它。问题是您将cubeConfig作为字符串发送。它需要是一个对象。这是代码:
string cubeConfigString = @"{
cubeConfig: {
groupBy: 'publication',
field: 'pid',
f: 'count'
},
filterQuery: 'SELECT * FROM c'
}";
Object cubeConfig = JsonConvert.DeserializeObject<Object>(cubeConfigString);
Console.WriteLine(cubeConfig);
dynamic result = await client.ExecuteStoredProcedureAsync<dynamic>("dbs/dev-test-database/colls/dev-test-collection/sprocs/cube", cubeConfig);
Console.WriteLine(result.Response);
答案 1 :(得分:1)
我的工作代码
public async static Task QuerySP2()
{
using (client = new DocumentClient(new Uri(endpointUrl), authorizationKey))
{
//Get the Database
var database = client.CreateDatabaseQuery().Where(db => db.Id == databaseId).ToArray().FirstOrDefault();
//Get the Document Collection
var collection = client.CreateDocumentCollectionQuery(database.SelfLink).Where(c => c.Id == collectionId).ToArray().FirstOrDefault();
StoredProcedure storedProc = client.CreateStoredProcedureQuery(collection.StoredProceduresLink).Where(sp => sp.Id == "cube").ToArray().FirstOrDefault();
string filterQuery = string.Format(@"SELECT * from c");
string cubeConfigString = @"{
cubeConfig: {
groupBy: 'publication',
field: 'id',
f: 'count'
},
filterQuery: '" + filterQuery + "'}";
dynamic cubeConfig = JsonConvert.DeserializeObject<dynamic>(cubeConfigString);
Console.WriteLine(cubeConfig);
string continuationToken = null;
dynamic result=null;
do
{
var queryDone = false;
while (!queryDone)
{
try
{
result = await client.ExecuteStoredProcedureAsync<dynamic>(storedProc.SelfLink, cubeConfig);
cubeConfig = result.Response;
continuationToken = cubeConfig.continuation;
queryDone = true;
}
catch (DocumentClientException documentClientException)
{
var statusCode = (int)documentClientException.StatusCode;
if (statusCode == 429 || statusCode == 503)
System.Threading.Thread.Sleep(documentClientException.RetryAfter);
else
throw;
}
catch (AggregateException aggregateException)
{
if (aggregateException.InnerException.GetType() == typeof(DocumentClientException))
{
var docExcep = aggregateException.InnerException as DocumentClientException;
var statusCode = (int)docExcep.StatusCode;
if (statusCode == 429 || statusCode == 503)
System.Threading.Thread.Sleep(docExcep.RetryAfter);
else
throw;
}
}
}
} while (continuationToken != null);
Console.WriteLine("Result from script: {0}\r\n", result.Response);
}
}