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.
An index provides a means of expanding the query capabilities of a table. SchemaBuilder offers API methods for creating and dropping indices. Unlike other schema members, there is no mechanism to alter an index.
To start a CREATE INDEX
query, use createIndex
in SchemaBuilder:
import static com.datastax.oss.driver.api.querybuilder.SchemaBuilder.*;
// an index name is not required
CreateIndexStart create = createIndex();
create = createIndex("my_idx");
Unlike other keyspace elements, there is not option to provide the keyspace name, instead it is implied from the indexed table’s keyspace.
Like all other CREATE
queries, one may supply ifNotExists()
to require that the index should
only be created if it doesn’t already exist, i.e.:
CreateIndexStart create = createIndex("my_idx").ifNotExists();
Note one small difference with IF NOT EXISTS
with indices is that the criteria also applies to
whether or not the table and column specification has an index already, not just the name of the
index.
At this stage, the query cannot be completed yet. You need to provide at least:
The table the index applies to using onTable
The column the index applies to using an andColumn*
implementation.
For example:
createIndex().onTable("tbl").andColumnKeys("addresses");
// CREATE INDEX ON tbl (KEYS(addresses))
Cassandra supports indices with a custom implementation, specified by an input class name. The
class implementation may be specified using custom(className)
, for example:
createIndex()
.custom("org.apache.cassandra.index.MyCustomIndex")
.onTable("tbl")
.andColumn("x");
// CREATE CUSTOM INDEX ON tbl (x) USING 'org.apache.cassandra.index.MyCustomIndex'
One popular custom index implementation is SASI (SSTable Attached Secondary Index). To use SASI,
use usingSASI
and optionally withSASIOptions
:
createIndex()
.usingSASI()
.onTable("tbl")
.andColumn("x")
.withSASIOptions(ImmutableMap.of("mode", "CONTAINS", "tokenization_locale", "en"));
// CREATE CUSTOM INDEX ON tbl (x) USING 'org.apache.cassandra.index.sasi.SASIIndex' WITH OPTIONS={'mode':'CONTAINS','tokenization_locale':'en'}
When indexing columns, one may simply use andColumn
. However, when indexing collection columns
there are several additional options available:
andColumnKeys
: Creates an index on a map column’s keys.
andColumnValues
: Creates an index on a map column’s values.
andColumnEntries
: Creates an index on a map column’s entries.
andColumnFull
: Creates an index of a frozen collection’s full value.
After specifying the columns for the index, you may use withOption
to provide free-form options on
the index. These are really only applicable to custom index implementations.
To create a DROP INDEX
query, use dropIndex
:
dropIndex("ks", "my_idx");
// DROP INDEX ks.my_idx
You may also specify ifExists
:
dropIndex("my_idx").ifExists();
// DROP INDEX IF EXISTS my_idx
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.