DATACASS-213 - Create secondary indexes for annotated properties.
We now create secondary indexes for annotated properties via @Indexed. Index creation is part of schema creation that is executed after Session initialization and table creation.
We support plain secondary indexes and key/value/entry indexes for map columns. Index creation is useful for rapid development but should not be used in large setups or at least with care to not impact performance in a negative way.
@Table
public class Person {
@Id
private String key;
@Indexed("name_index")
private String name;
private Map<@Indexed String, String> keys;
}
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
* CRUD repository interface renaming: `CassandraRepository` using `MapId` is now renamed to `MapIdCassandraRepository`. `TypedIdCassandraRepository` is renamed to `CassandraRepository`.
|
||||
* Lightweight transactions via `InsertOptions` and `UpdateOptions` using the Template API.
|
||||
* Merge of Spring CQL into Spring Data Cassandra.
|
||||
* Index creation on application startup via `@Indexed`.
|
||||
|
||||
[[new-features.1-5-0]]
|
||||
== What's new in Spring Data for Apache Cassandra 1.5
|
||||
|
||||
@@ -672,7 +672,7 @@ These entity classes can be used to create Cassandra table specifications and us
|
||||
Schema creation is tied to `Session` initialization with `SchemaAction`. Following actions are supported:
|
||||
|
||||
* `SchemaAction.NONE`: No tables/types will be created or dropped. This is the default setting.
|
||||
* `SchemaAction.CREATE`: Create tables and user-defined types from entities annotated with `@Table` and types annotated with `@UserDefinedType`. Existing tables/types will cause an error if the type is attempted to be created.
|
||||
* `SchemaAction.CREATE`: Create tables, indexesm and user-defined types from entities annotated with `@Table` and types annotated with `@UserDefinedType`. Existing tables/types will cause an error if the type is attempted to be created.
|
||||
* `SchemaAction.CREATE_IF_NOT_EXISTS`: Like `SchemaAction.CREATE` but with `IF NOT EXISTS` applied. Existing tables/types won't cause any errors but may remain stale.
|
||||
* `SchemaAction.RECREATE`: Drops and recreate existing tables and types that are known to be used. Tables and types that are not configured in the application are not dropped.
|
||||
* `SchemaAction.RECREATE_DROP_UNUSED`: Drop all tables and types and recreate only known tables and types.
|
||||
|
||||
@@ -245,6 +245,7 @@ be referenced with `@PrimaryKey`.
|
||||
where it is applied from being stored in the database.
|
||||
* `@Column` - applied at the field level. Describes the column name as it will be represented in the Cassandra table
|
||||
thus allowing the name to be different than the field name of the class.
|
||||
* `@Indexed` - applied at the field level. Describes the index to be created at session initialization.
|
||||
* `@CassandraType` - applied at the field level to specify a Cassandra data type. Types are derived from
|
||||
the declaration by default.
|
||||
* `@UserDefinedType` - applied at the type level to specify a Cassandra user-defined data type (UDT). Types are derived
|
||||
@@ -286,6 +287,7 @@ public class Person {
|
||||
private String firstName;
|
||||
|
||||
@Column(forceQuote = true)
|
||||
@Indexed
|
||||
private String lastName;
|
||||
|
||||
private Address address;
|
||||
@@ -299,7 +301,7 @@ public class Person {
|
||||
@CassandraType(type = Name.SET, typeArguments = Name.BIGINT)
|
||||
private Set<Long> timestamps;
|
||||
|
||||
private Map<String, InetAddress> sessions;
|
||||
private Map<@Indexed String, InetAddress> sessions;
|
||||
|
||||
public Person(Integer ssn) {
|
||||
this.ssn = ssn;
|
||||
@@ -346,6 +348,37 @@ public class Address {
|
||||
NOTE: Working with User-Defined Types requires a `UserTypeResolver` configured with the mapping context.
|
||||
See the <<cassandra.connectors,configuration chapter>> for how to configure a `UserTypeResolver`.
|
||||
|
||||
==== Index creation
|
||||
|
||||
You can annotate particular entity properties with `@Indexed` if you whish to create secondary indexes on application
|
||||
startup. Index creation will create simple secondary indexes for scalar types, user-defined, and collection types.
|
||||
|
||||
Map types distinguish between `ENTRY`, `KEYS` and `VALUES` indexes. Index creation derives the index type from the
|
||||
annotated element:
|
||||
|
||||
.Variants of map indexing
|
||||
====
|
||||
[source,java]
|
||||
----
|
||||
@Table
|
||||
public class Person {
|
||||
|
||||
@Id
|
||||
private String key;
|
||||
|
||||
@Indexed("indexed_map")
|
||||
private Map<String, String> entries;
|
||||
|
||||
private Map<@Indexed String, String> keys;
|
||||
|
||||
private Map<String, @Indexed String> values;
|
||||
|
||||
// …
|
||||
}
|
||||
----
|
||||
====
|
||||
|
||||
WARNING: Index creation on session initialization may have a severe performance impact on application startup.
|
||||
|
||||
[[cassandra.mapping.explicit-converters]]
|
||||
=== Overriding Mapping with explicit Converters
|
||||
|
||||
Reference in New Issue
Block a user