Hi all,
DAS 3.0.0 and DAS 3.0.1 has 5 built-in Aggregates[1][2] namely MIN, MAX,
SUM, AVG and COUNT which can be used in interactive analytics. Sometimes
some scenarios may require custom aggregate functions for aggregate
calculations. For this we have introduced a new interface from
carbon-analytics/analytics-core, which has three methods which need to be
implemented.
Interface :
org.wso2.carbon.analytics.dataservice.core.indexing.aggregates.AggregateFunction
Following are the three methods that need to be implemented.
*public void process(RecordValuesContext ctx, String[] aggregateFields)
throws AnalyticsException*;
This method will get called for all the records which need to be
aggregated. RecordValuesContext will contain the record values of the
current record being processed. aggregateFields will contain an array of
fields which will be used for the aggregation. The order of the
aggregateFields will matter when we implement the logic. For example, lets
say we are going to implement SUM aggregate. Then we know that only one
field will be required to calculate SUM and always aggregateFields will
contain one field name in it, which is the name of the field being SUMed.
public void process(RecordValuesContext ctx, String[] aggregateFields)
throws AnalyticsException {
if (aggregateFields == null || aggregateFields.length == 0) {
throw new AnalyticsException("Field to be aggregated, is
missing");
}
Object value = ctx.getValue(aggregateFields[0]);
if (value == null) {
throw new AnalyticsException("Error while calculating SUM:
value of the field, " +
aggregateFields[0] + " is null");
}
if (value instanceof Number) {
sum += ((Number)value).doubleValue();
} else {
throw new AnalyticsException("Error while calculating Average:
Value '" + value.toString() +
"', being aggregated is not
numeric.");
}
}
*public Object finish() throws AnalyticsException*
Once the aggregation is complete for a group or for whole record set, this
method will be called. This will return the final result of the aggregate
function. for example, the SUM aggregate,
public Number finish() throws AnalyticsException {
return sum;
}
*public String getAggregateName()*
This method is used to identify the Aggregate function uniquely among other
Aggregate functions. You can return a preferred string inside this method.
*Calling custom aggregate functions through Javascript API*
var queryInfo = {
tableName:"Students", //table name on which the aggregation is performed
searchParams : {
groupByField:"location", //grouping field if any
query : "Grade:10" //additional filtering query
aggregateFields:[
{
fields:["Height", "Weight"], //fields necessary for aggregate
function
aggregate:"CUSTOM_AGGREGATE", //unique name of the aggregate
function, this is what we return using "getAggregateName" method above.
alias:"aggregated_result" //Alias for the result of the
aggregate function
}]
}
}
client.searchWithAggregates(queryInfo, function(data) {
console.log (data["message"]);
}, function(error) {
console.log("error occured: " + error["message"]);
});
*Note that the order elements in attribute "fields" will be the same order
of aggregateFields parameter's element order in above process method. That
is Height will be aggregateFields[0] and Weight will be aggregateFields[1]
in process method. Based on that order, "CUSTOM_AGGREGATE" should be
implemented.*
*Aggregates REST APIs*This is as same as the Javascript API.
POST https://localhost:9443/analytics/aggregates
{
"tableName":"Students",
"groupByField":"location",
"aggregateFields":[
{
"fields":["Height", "Weight"],
"aggregate":"CUSTOM_AGGREGATE",
"alias":"aggregated_result"
}]
}
[1]
https://docs.wso2.com/display/DAS301/Retrieving+Aggregated+Values+of+Given+Records+via+REST+API
[2]
https://docs.wso2.com/display/DAS301/Retrieving+Aggregated+Values+of+Given+Records+via+JS+API
--
Gimantha Bandara
Software Engineer
WSO2. Inc : http://wso2.com
Mobile : +94714961919
_______________________________________________
Architecture mailing list
[email protected]
https://mail.wso2.org/cgi-bin/mailman/listinfo/architecture