Aggregates enable users to apply User-defined functions (UDF) to rows in a data set and combine their values into a final result, for example average or standard deviation. SchemaBuilder offers API methods for creating and dropping aggregates.
To start a CREATE AGGREGATE
query, use createAggregate
in SchemaBuilder:
import static com.datastax.oss.driver.api.querybuilder.SchemaBuilder.*; CreateAggregateStart create = createAggregate("average");
Like all other CREATE
queries, one may supply ifNotExists()
to require that the aggregate should only be created if it doesn't already exist, i.e.:
CreateAggregateStart create = createAggregate("cycling", "average").ifNotExists();
You may also specify that you would like to replace an existing aggregate by the same signature if it exists. In this case, use orReplace
:
CreateAggregateStart create = createAggregate("cycling", "average").orReplace();
One may also specify the parameters of an aggregate using withParameter
:
CreateAggregateStart create = createAggregate("cycling", "average") .withParameter(DataTypes.INT);
To complete an aggregate, one must then provide the following:
withSFunc
) to execute on each rowwithSType
)In addition, while optional, it is typical that the following is also provided:
withFinalFunc
)withInitCond
) which defines the initial value(s) to be passed in to the first parameter of the state function.For example, the following defines a complete CREATE AGGREGATE
statement:
createAggregate("cycling", "average") .withParameter(DataTypes.INT) .withSFunc("avgstate") .withSType(DataTypes.tupleOf(DataTypes.INT, DataTypes.BIGINT)) .withFinalFunc("avgfinal") .withInitCond(tuple(literal(0), literal(0))); // CREATE AGGREGATE cycling.average (int) SFUNC avgstate STYPE tuple<int, bigint> FINALFUNC avgfinal INITCOND (0,0)
To create a DROP AGGREGATE
query, use dropAggregate
:
dropAggregate("cycling", "average"); // DROP AGGREGATE cycling.average
You may also specify ifExists
:
dropAggregate("average").ifExists(); // DROP AGGREGATE IF EXISTS average