gpt4 book ai didi

javascript - Azure Cosmos DB 存储过程响应大小太大

转载 作者:行者123 更新时间:2023-12-05 06:29:48 25 4
gpt4 key购买 nike

我有传感器 IoT 数据,该数据存储在 cosmos DB 中。我需要过去 30 天的分钟级数据聚合才能在 web 应用程序中显示。所以我编写了存储过程,用于对传感器数据进行分组和聚合。

它给出以下错误

Failed to execute stored procedure something for collection newData: {"code":400,"body":"{\"code\":\"BadRequest\",\"message\":\"Message: {\\"Errors\\":[\\"Encountered exception while executing function. Exception = Error: Resulting message would be too large because of \\\\"x-ms-documentdb-script-log-results\\\\". Return from script with current message and use continuation token to call the script again or modify your script.\\r\\nStack trace: Error: Resulting message would be too large because of \\\\"x-ms-documentdb-script-log-results\\\\". Return from script with current message and use continuation token to call the script again or modify your script.\\n at validateSize (sa\\"]}\r\nActivityId: 46713736-fe18-4fd1-8df1-49fa615c7289, Request URI: /apps/35edbe01-33d4-4189-9959-240fe985a75e/services/f3955cd0-044e-4a48-ad24-ae51a24c13b8/partitions/784e5371-950b-476d-a765-52ddb784f8dd/replicas/131850819031922581p/, RequestStats: \r\nRequestStartTime: 2018-11-06T21:08:06.7386246Z, Number of regions attempted: 1\r\n, SDK: Microsoft.Azure.Documents.Common/2.1.0.0\"}","activityId":"46713736-fe18-4fd1-8df1-49fa615c7289","substatus":413}

由于传感器具有 1 秒级别的数据,我需要找到 1 分钟的聚合,因此我无法使用流分析作业作为转发,我不知道要保留在 avg() 中的传感器名称是什么。

我唯一没有选择的就是运行存储过程并获取 1 分钟的聚合。

我使用Javascript作为存储过程,并且在我的api调用中我使用java Spring boot。请给我任何建议,我如何才能超越 Cosmos DB 的这一限制,或者如何在 Cosmos DB 中存储 1 分钟的聚合,以便我可以检索这些记录。

以下是我的程序,我传递 3 个字符串参数,例如“压力、温度、体积”“123444”“345552”

function something(variable1 , variable2 , variable3) {


var variables = variable1.split(",");
var parameters = variable1.split(",");
var variablestrings = '' , totals = {} ;
var keys = [] , values = [] , totals = [] ;
var dataPoints = {} , results = [], k , l , p ,i;
var resultPoints = [],value;

var collection = getContext().getCollection();

for ( var i = 0 ; i < variables.length ; i = i +1 ) {
results[i] = {};
results[i].variableName = variables[i] ;
results[i].data = [];
variables[i] = 'r.' + variables[i];
}

variablestrings = variables.toString() + ' , r._ts ' ;

var queryString = 'SELECT ' + variablestrings + 'FROM root r where r._ts between ' + variable2 + ' AND ' + variable3 ;

// Query documents and take 1st item.
var isAccepted = collection.queryDocuments(
collection.getSelfLink(),
queryString,
function (err, feed, options) {
// console.log(feed[0]._ts);
if (err) throw err;

// Check the feed and if empty, set the body to 'no docs found', 
// else take 1st element from feed
if (!feed || !feed.length) {
var response = getContext().getResponse();
response.setBody('no docs found');
}
else {
//console.log(feed.length);
feed.forEach( (item, index) => {
//var x = JSON.parse( item ) ;
var d1 = new Date( item._ts ) ;
item['timeInMinutes'] = d1.getFullYear() + '-' + d1.getUTCMonth() + '-' + d1.getUTCDate() + ' ' + d1.getUTCHours() + ':' + d1.getUTCMinutes() + ':00' ;
//delete item[_ts];
//console.log(JSON.stringify(item));
if( ! dataPoints[ item['timeInMinutes'] ] ) {
dataPoints[ item['timeInMinutes'] ] = [];
dataPoints[ item['timeInMinutes'] ].push(item);
}
else{
dataPoints[ item['timeInMinutes'] ].push(item);

}
//arrayObjects.push( item ) ;

} );

values = Object.values( dataPoints ) ;
for ( k = 0 ; k < values.length ; k = k+1){
value = values[k] ;

for ( l = 0 ; l < parameters.length ; l = l +1){
totals[ parameters[l] ]= 0;
}

for ( p = 0 ; p < parameters.length ; p = p +1){

for ( i = 0 ; i < value.length ; i = i +1 ){
totals[ parameters[p]] = value[i][parameters[p]] + totals[parameters[p]] ;
}

results[p].data.push({ 'x-axis' : value[0]['timeInMinutes'] , 'y-axis' : (totals[ parameters[p] ] / value.length ) });

}
}
//console.log(results[0]);
var response = getContext().getResponse();
response.setBody(results);


}
});

if (!isAccepted) throw new Error('The query was not accepted by the server.');

}

最佳答案

基于 @Chris Anderson-MSFT 在对 OP 问题的评论中的建议。

<小时/>

就我而言,我用 javascript 编写的存储过程几乎没有日志语句。简单地删除 console.logs 对我来说很有效。

关于javascript - Azure Cosmos DB 存储过程响应大小太大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53180181/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com