Hey Team, I need some support...There is a need wh...
# general
u
Hey Team, I need some support...There is a need which can be resolved only by doing the union of two responses or merging the two queries and then getting the response... I want to set different aggregations for different parameters on device data.... But only thing is druid supports min, max, avg, sum but does not support the difference aggregation (difference of last and first values)... For which I'll be writing 2 queries one for difference and one for other combined.... Now response should get union of both or I should get response from combining both the queries...Is there a possibility of doing this One of them which combines all other aggregations is coded below
_for_ (DownloadTelemetryDataRequestDto.ParameterAggregationDTO parameterAggregationDTO : parameterAggregationDTOList) {
AggregationRequestDto.Aggregation aggregation = _new_ AggregationRequestDto.Aggregation();
String parameter = parameterAggregationDTO.getParameter(), aggregationType = parameterAggregationDTO.getAggregationType();
aggregation.setFieldName(swappedSchema.get(parameter));
_if_(swappedSchema.containsKey(parameter)){
aggregation.setName(swappedSchema.get(parameter).substring(swappedSchema.get(parameter).indexOf(".") + 1));
}
_if_ (aggregationType.equalsIgnoreCase("min")) aggregation.setType(DRUID_MIN);
_else if_ (aggregationType.equalsIgnoreCase("max")) aggregation.setType(DRUID_MAX);
_else if_ (aggregationType.equalsIgnoreCase("sum")) aggregation.setType(DRUID_SUM);
_else_ aggregation.setType(DRUID_AVG);
aggregationList.add(aggregation);
}
_assert_ dataSource != _null_;
Object query = druidQueryBuilder.buildQueryForTelemetryData(dataSource, deviceIds, aggregationPeriod, dates.get(0), dates.get(1), aggregationList);
JsonNode response = druidHttpClient.druidClientResponse(query, DRUID_NATIVE);
Second one is the difference for which I need to find the first and last values and then sort them according to timestamp and then get the difference like
_if_ (aggregationType.equalsIgnoreCase(DIFF)) {
String interval = startDate + "/" + endDate;
Object requestFirst = druidQueryBuilder.buildDruidQueryTimeZoneAdjusted(attribute, interval, aggregationType, aggregationInvterval, DRUID_STRINGFIRST, timezone, dataSource);
JsonNode responseFirst = druidHttpClient.druidClientResponse(requestFirst, DRUID_NATIVE);
List<DruidAggregationResponseDto> firstResponse = _new_ ArrayList<>();
_if_ (responseFirst != _null_) {
firstResponse = objectMapper.readValue(responseFirst.toPrettyString(), _new_ TypeReference<List<DruidAggregationResponseDto>>() {
});
}
HashMap<String, ValuesDateRangeDto> aggregationHash = _new_ LinkedHashMap<>();
Object requestLast = druidQueryBuilder.buildDruidQueryTimeZoneAdjusted(attribute, interval, aggregationType, aggregationInvterval, DRUID_STRINGLAST, timezone, dataSource);
JsonNode responseLast = druidHttpClient.druidClientResponse(requestLast, DRUID_NATIVE);
List<DruidAggregationResponseDto> lastResponse = _new_ ArrayList<>();
_if_ (responseLast != _null_) {
lastResponse = objectMapper.readValue(responseLast.toPrettyString(), _new_ TypeReference<List<DruidAggregationResponseDto>>() {
});
}
firstResponse = firstResponse.stream()
.sorted(Comparator.comparing(DruidAggregationResponseDto::getTimestamp))
.collect(Collectors.toList());
lastResponse = lastResponse.stream()
.sorted(Comparator.comparing(DruidAggregationResponseDto::getTimestamp))
.collect(Collectors.toList());
_for_ (_int_ i = 0; i < firstResponse.size() && i < lastResponse.size(); i++) {
DruidAggregationResponseDto firstDto = firstResponse.get(i);
DruidAggregationResponseDto lastDto = lastResponse.get(i);
Double diffValue = lastDto.getResult().getValue() - firstDto.getResult().getValue();
}
But now the thing is how to combine the response or call the database with a common query. Can someone help me out on this...