Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Spring Data Cassandra

2,932 views

Published on

A brief overview of some of the features of Spring Data Cassandra. Loosely based on material from .... http://www.infoq.com/presentations/spring-data-cassandra-couchbase

Find an example project referenced in the slides here.

https://github.com/digbigdata/sdcassandra/

And more information about us on digbigdata.com

Published in: Technology

Spring Data Cassandra

  1. 1. CASSANDRA INTEGRATION Spring Data
  2. 2. CASSANDRA INTRO
  3. 3. SPRING DATA CASSANDRA
  4. 4. Rationale • Reduces ramp up time and code required to work with Cassandra • Clear, annotation based mappings for java objects to Cassandra tables • Good fit if already using spring data / spring boot • Minimal property based configuration
  5. 5. Features • Uses common spring data repository pattern • Synchronous and Asynchronous requests via the DataStax Java Driver • XML based CQL schema creation • JavaConfig / XML support via properties • QueryBuilder interface for complex query creation.
  6. 6. Annotated POJOs • Allows easy mapping of annotated POJOs to Cassandra tables. @Table("users") public class User { @PrimaryKey("user_id") private String userId; @Column("first_name") private String firstName; @Column("last_name") private String lastName; private String password;
  7. 7. Spring Repository Support • Basic Support of Spring Data Repository pattern. • Provides CRUD operations without code, findOne, findAll, save (upsert), delete. • Easily extensible with custom queries. public interface UserRepository extends CassandraRepository<User> { @Query("select * from users where user_id = ?0") User findByUserId(String userId); }
  8. 8. Primary Keys • Easily define simple, compound or composite primary keys. Allows for narrow or wide rows. • Simple Key example: @PrimaryKey("user_id") private String userId; • Compound Key with Cluster Column Example: @PrimaryKeyColumn(name = "eid", ordinal = 0, type = PrimaryKeyType.PARTITIONED) private Integer eid; @PrimaryKeyColumn(name = "day", ordinal = 1, type = PrimaryKeyType.PARTITIONED) private Integer day; @PrimaryKeyColumn(name = "event_id", ordinal = 2, type = PrimaryKeyType.CLUSTERED) private UUID eventId;
  9. 9. Query Support • No support for spring data “findBy” style queries at present, e.g. findByNameLikeOrderBy • Use @Query annotation for custom queries @Query("select * from events_by_tag where eid = ?0 and tag = ?1") List<Event> findByEidAndTag(Integer eid, String tag);
  10. 10. Query Support • Use static BasicMapId.id() to construct primary key based queries. MapId id = id() .with("eid", 1) .with("day", 12345) .with("eventId”, “XXXX-XXXX-XXXX”); return eventsByDayRepository.findOne(id);
  11. 11. Or use Java Driver QueryBuilder • Use QueryBuilder to create queries including batch and transaction based queries. Select select = QueryBuilder .select() .all() .from(“events_by_day”) .where(eq("eid", 1)) .and(eq("day", 12345)) .and(eq("eventId", “XXXX-XXXX-XXXX”)); return cassandraOperations .select (select, Event.class);
  12. 12. DEMO

×