liveBook · Manning

Partitioning key

As explained in Spring in Action, Sixth Edition

A partition key is the component of a Cassandra primary key that determines the partition in which a row is stored.

A partitioning key is the component of a Cassandra primary key that determines where a row is stored. Cassandra hashes each row’s partition key to determine the partition or partitions managing it. A clustering key, when present, instead controls row ordering within that partition. section 4.1.2

When mapping a domain type with Spring Data Cassandra:

  • Annotate the partitioning property with @PrimaryKeyColumn(type = PrimaryKeyType.PARTITIONED). section 4.1.3
  • Use another @PrimaryKeyColumn with PrimaryKeyType.CLUSTERED when rows need ordering within the partition. section 4.1.3
  • Model Cassandra tables around read operations, often duplicating related data instead of relying on relational-style joins. section 4.1.2

Taco persistence

A Taco mapping uses a UUID id annotated with @PrimaryKeyColumn(type = PrimaryKeyType.PARTITIONED). That id determines the Cassandra partition for each taco row. The createdAt property is mapped as a clustered primary-key column and ordered descending, so newer tacos appear first within a partition. section 4.1.3

Listing 4.1 Annotating the Taco class for Cassandra persistence
package tacos;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
 
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
 
import org.springframework.data.cassandra.core.cql.Ordering;
import org.springframework.data.cassandra.core.cql.PrimaryKeyType;
import org.springframework.data.cassandra.core.mapping.Column;
import org.springframework.data.cassandra.core.mapping.PrimaryKeyColumn;
import org.springframework.data.cassandra.core.mapping.Table;
 
import com.datastax.oss.driver.api.core.uuid.Uuids;
 
import lombok.Data;
 
@Data
@Table("tacos")                                                    #1
public class Taco {
 
  @PrimaryKeyColumn(type=PrimaryKeyType.PARTITIONED)               #2
  private UUID id = Uuids.timeBased();
 
  @NotNull
  @Size(min = 5, message = "Name must be at least 5 characters long")
  private String name;
 
  @PrimaryKeyColumn(type=PrimaryKeyType.CLUSTERED,                 #3
                    ordering=Ordering.DESCENDING)
  private Date createdAt = new Date();
 
  @Size(min=1, message="You must choose at least 1 ingredient")
  @Column("ingredients")                                           #4
  private List<IngredientUDT> ingredients = new ArrayList<>();
 
  public void addIngredient(Ingredient ingredient) {
    this.ingredients.add(TacoUDRUtils.toIngredientUDT(ingredient));
  }
}

Inspect the complete Taco entity mapping to see the partitioning annotation alongside the table and clustering-key configuration. section 4.1.3

How Spring in Action, Sixth Edition covers it

How deep it goes
Explains hash-based placement and demonstrates Spring Data annotations, while deferring detailed partition and cluster mechanics to Cassandra documentation.
Examples
Uses a Taco entity whose UUID id is partitioned and whose createdAt value provides descending clustering order.
What it assumes
Assumes basic primary-key knowledge while establishing Cassandra partitions, clustering keys, and nonrelational data modeling.
What you can do after
Apply it via Spring Data Cassandra annotations when mapping a domain type to a partitioned table.
How it connects
Connects primary keys with clustering keys, Cassandra data modeling, denormalization, and UUID-based identifiers.

Also explained in

Compare how 10 books explain partitioning key →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.