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
Scylla Java Driver Manual Query builder Schema builder Materialized View

Materialized View¶

Materialized Views are an experimental feature introduced in Apache Cassandra 3.0 that provide a mechanism for server-side denormalization from a base table into a view that is updated when the base table is updated. SchemaBuilder offers API methods for creating, altering and dropping materialized views.

Creating a Materialized View (CREATE MATERIALIZED VIEW)¶

To start a CREATE MATERIALIZED VIEW query, use createMaterializedView in SchemaBuilder:

import static com.datastax.oss.driver.api.querybuilder.SchemaBuilder.*;

CreateMaterializedViewStart create = createMaterializedView("cycling", "cyclist_by_age");

Like all other CREATE queries, one may supply ifNotExists() to require that the view should only be created if it doesn’t already exist, i.e.:

CreateMaterializedViewStart create = createMaterializedView("cycling", "cyclist_by_age").ifNotExists();

There are a number of steps that must be executed to complete a materialized view:

  • Specify the base table using asSelectFrom

  • Specify the columns to include in the view via column or columns

  • Specify the where clause using relations

  • Specify the partition key columns using withPartitionKey and withClusteringColumn

For example, the following defines a complete CREATE MATERIALIZED VIEW statement:

createMaterializedView("cycling", "cyclist_by_age")
    .asSelectFrom("cycling", "cyclist")
    .columns("age", "name", "country")
    .whereColumn("age")
    .isNotNull()
    .whereColumn("cid")
    .isNotNull()
    .withPartitionKey("age")
    .withClusteringColumn("cid");
// CREATE MATERIALIZED VIEW cycling.cyclist_by_age AS
// SELECT age,name,country FROM cycling.cyclist WHERE age IS NOT NULL AND cid IS NOT NULL PRIMARY KEY(age,cid) 

Please note that not all WHERE clause relations may be compatible with materialized views.

Like a table, one may additionally provide configuration such as clustering order, compaction options and so on. Refer to RelationStructure for documentation on additional configuration that may be provided for a view.

Altering a Materialized View (ALTER MATERIALIZED VIEW)¶

To start a ALTER MATERIALIZED VIEW query, use alterMaterializedView:

alterMaterializedView("cycling", "cyclist_by_age");

Unlike a table, you may not alter, drop or rename columns on a materialized view. Instead, one may only alter the options defined in RelationStructure. For example:

alterMaterializedView("cycling", "cyclist_by_age")
    .withGcGraceSeconds(0)
    .withCaching(true, RowsPerPartition.NONE);
// ALTER MATERIALIZED VIEW cycling.cyclist_by_age WITH gc_grace_seconds=0 AND caching={'keys':'ALL','rows_per_partition':'NONE'}

Dropping a View (DROP MATERIALIZED VIEW)¶

To create a DROP MATERIALIZED VIEW query, use dropMaterializedView:

dropMaterializedView("cycling", "cyclist_by_age");
// DROP MATERIALIZED VIEW cycling.cyclist_by_age

You may also specify ifExists:

dropTable("cyclist_by_age").ifExists();
// DROP MATERIALIZED VIEW IF EXISTS cyclist_by_age
PREVIOUS
Keyspace
NEXT
Table
  • 4.13.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
      • Using the driver in GraalVM native images
      • 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

  • Materialized View
    • Creating a Materialized View (CREATE MATERIALIZED VIEW)
    • Altering a Materialized View (ALTER MATERIALIZED VIEW)
    • Dropping a View (DROP MATERIALIZED VIEW)
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