Was this page helpful?
ScyllaDB Java Driver is available under the Apache v2 License. ScyllaDB Java Driver is a fork of DataStax Java Driver. See Copyright here.
Caution
You're viewing documentation for a previous version of Scylla Java Driver. Switch to the latest stable version.
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:
The state function (withSFunc
) to execute on each row
The type of the value returned by the state function (withSType
)
In addition, while optional, it is typical that the following is also provided:
The final function to be executed after the state function is evaluated against all rows
(withFinalFunc
)
The initial condition (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
Was this page helpful?
ScyllaDB Java Driver is available under the Apache v2 License. ScyllaDB Java Driver is a fork of DataStax Java Driver. See Copyright here.