Scylla Documentation Logo Documentation
  • Server
    • Scylla Open Source
    • Scylla Enterprise
    • Scylla Alternator
  • Cloud
    • Scylla Cloud
    • Scylla Cloud Docs
  • Tools
    • Scylla Manager
    • Scylla Monitoring Stack
    • Scylla Operator
  • Drivers
    • CQL Drivers
    • DynamoDB Drivers
Download
Menu

Caution

You're viewing documentation for a previous version of Scylla Java Driver. Switch to the latest stable version.

Scylla Java Driver Manual Query builder Schema builder Aggregate

Aggregate¶

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.

Creating an aggregate (CREATE AGGREGATE)¶

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)

Dropping an aggregate (DROP AGGREGATE)¶

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
PREVIOUS
Schema builder
NEXT
Function
  • 4.10.0.x
    • 4.13.0.x
    • 4.12.0.x
    • 4.11.1.x
    • 4.10.0.x
    • 4.7.2.x
    • 3.11.2.x
    • 3.11.0.x
    • 3.10.2.x
    • 3.7.2.x
  • Java Driver for Scylla and Apache Cassandra®
  • API Documentation
  • Manual
    • API conventions
    • Case sensitivity
    • Core driver
      • Address resolution
      • Asynchronous programming
      • Authentication
      • Bill of Materials (BOM)
      • Compression
      • Configuration
        • Reference configuration
      • Control connection
      • Custom codecs
      • Detachable types
      • Query idempotence
      • Integration
      • Load balancing
      • Logging
      • Metadata
        • Node metadata
        • Schema metadata
        • Token metadata
      • Metrics
      • Native protocol
      • Non-blocking programming
      • Paging
      • Performance
      • Connection pooling
      • Query timestamps
      • Reactive Style Programming
      • Reconnection
      • Request tracker
      • Retries
      • Using the shaded JAR
      • Speculative query execution
      • SSL
      • Statements
        • Batch statements
        • Per-query keyspace
        • Prepared statements
        • Simple statements
      • Temporal types
      • Request throttling
      • Query tracing
      • Tuples
      • User-defined types
    • Developer docs
      • Administrative tasks
      • Common infrastructure
        • Concurrency
        • Driver context
        • Event bus
      • Native protocol layer
      • Netty pipeline
      • Request execution
    • Mapper
      • Integration
        • Kotlin
        • Lombok
        • Java 14 Records
        • Scala
      • DAOs
        • Custom result types
        • Delete methods
        • GetEntity methods
        • Increment methods
        • Insert methods
        • Null saving strategy
        • Query methods
        • Query provider methods
        • Select methods
        • SetEntity methods
        • Statement attributes
        • Update methods
      • Entities
      • Mapper interface
    • OSGi
    • Query builder
      • Conditions
      • DELETE
      • Idempotence in the query builder
      • INSERT
      • Relations
      • Schema builder
        • Aggregate
        • Function
        • Index
        • Keyspace
        • Materialized View
        • Table
        • Type
      • SELECT
      • Terms
      • TRUNCATE
      • UPDATE
  • Upgrade guide
  • Frequently asked questions
  • Changelog
  • Create an issue
  • Edit this page

On this page

  • Aggregate
    • Creating an aggregate (CREATE AGGREGATE)
    • Dropping an aggregate (DROP AGGREGATE)
Logo
Docs Contact Us About Us
Mail List Icon Slack Icon
© 2022, ScyllaDB. All rights reserved.
Last updated on 25 May 2022.
Powered by Sphinx 4.3.2 & ScyllaDB Theme 1.2.2