data access 2.0? please welcome: spring data!

56
Data Access 2.0? …please welcome… Spring Data! Oliver Gierke

Upload: oliver-gierke

Post on 09-May-2015

2.579 views

Category:

Technology


5 download

DESCRIPTION

Slides of my talk @ JAX 2012

TRANSCRIPT

Page 1: Data access 2.0? Please welcome: Spring Data!

Data Access 2.0?…please welcome…

Spring Data!

Oliver Gierke

Page 3: Data access 2.0? Please welcome: Spring Data!

What to expect?

Page 4: Data access 2.0? Please welcome: Spring Data!

Why?

How?

What?

Page 5: Data access 2.0? Please welcome: Spring Data!

A Developer‘s View

Page 6: Data access 2.0? Please welcome: Spring Data!

What to expect?NOT!

Page 7: Data access 2.0? Please welcome: Spring Data!

What to expect? NOT!

Page 8: Data access 2.0? Please welcome: Spring Data!

Retrospect

Page 9: Data access 2.0? Please welcome: Spring Data!

Relational databases

Page 10: Data access 2.0? Please welcome: Spring Data!

Scaling

Page 11: Data access 2.0? Please welcome: Spring Data!

Data structures

Page 12: Data access 2.0? Please welcome: Spring Data!

(No)SQLRedis

Riak

MongoDB

Cassandra

CouchDB

Neo4JHBase

SimpleDB

OrientDB

MembaseHibari Voldemort

Sones

Page 13: Data access 2.0? Please welcome: Spring Data!

Key Value

Page 14: Data access 2.0? Please welcome: Spring Data!

Column families

Page 15: Data access 2.0? Please welcome: Spring Data!

Graphs

Page 16: Data access 2.0? Please welcome: Spring Data!

Documents

Page 17: Data access 2.0? Please welcome: Spring Data!
Page 18: Data access 2.0? Please welcome: Spring Data!

Document databaseJSON documents

JSON queries

Page 19: Data access 2.0? Please welcome: Spring Data!

19

MongoDB Infrastructure API

Mongo mongo = new Mongo(…);DB db = mongo.getDB("myDatabase");Collection collection = db.getCollection("myCollection");

DBObject address = new BasicDBObject();address.put("city", "London");

DBObject person = new BasicDBObject();person.put("firstname", "Dave");person.put("lastname", "Matthews");person.put("address", address);

collection.save(person);

Page 20: Data access 2.0? Please welcome: Spring Data!

20

MongoDB Query API

Mongo mongo = new Mongo(…);DB db = mongo.getDB("myDatabase");Collection collection = db.getCollection("myCollection");

DBObject query = new BasicDBObject();query.put("address.city", "London");

DBCursor cursor = collection.find(query);

for (DBObject element : cursor) { // Map data onto object}

Page 21: Data access 2.0? Please welcome: Spring Data!

Graph databaseNodes / Relationships

Traversals / Cypher / Gremlin

Page 22: Data access 2.0? Please welcome: Spring Data!

22

Neo4J Infrastructure API

GraphDatabaseService database = new EmbeddedGraphDatabase(…);Transaction tx = database.beginTx();

try {Node mrAnderson = database.createNode();mrAnderson.setProperty("name", "Thomas Anderson");Node morpheus = database.createNode();morpheus.setProperty("name", "Morpheus");

Relationship friendship = mrAnderson.createRelationshipTo(morpheus, FriendTypes.KNOWS);

tx.success();} finally {

tx.finish();}

Page 23: Data access 2.0? Please welcome: Spring Data!

23

Neo4J Query API

GraphDatabaseService database = new EmbeddedGraphDatabase(…);

CypherParser parser = new CypherParser();Query query = parser.parse("start person = Person(id = *) match " +

"person-[:colleagues]->colleague where colleague.firstname = {name}");

Map<String, Object> parameters = new HashMap<String, Object>();parameters.put("name", "Dave");

ExecutionEngine engine = new ExecutionEngine(database);ExecutionResult result = engine.execute(query, parameters);

for (EntrySet<String, Object> element : result) {// Map data onto object

}

Page 24: Data access 2.0? Please welcome: Spring Data!

24

Neo4J entity

class Actor {

private final Node node;

public Actor(Node node) { … }

public String getName() {return (String) node.getProperty(„name“);

}

…}

Page 25: Data access 2.0? Please welcome: Spring Data!

Forest for the woods?

Page 26: Data access 2.0? Please welcome: Spring Data!

JPA?

Page 27: Data access 2.0? Please welcome: Spring Data!

" This document is the specification of the Java API for the management of persistence and object/relational mapping with Java EE and Java SE. The technical objective of this work is to provide an object/relational mapping facility for the Java application developer using a Java domain model to man- age a relational database.

Page 28: Data access 2.0? Please welcome: Spring Data!

" This document is the specification of the Java API for the management of persistence and object/relational mapping with Java EE and Java SE. The technical objective of this work is to provide an object/relational mapping facility for the Java application developer using a Java domain model to man- age a relational database.

Page 29: Data access 2.0? Please welcome: Spring Data!

JPA?

Page 30: Data access 2.0? Please welcome: Spring Data!

There‘s someSpring for that!

Page 31: Data access 2.0? Please welcome: Spring Data!

Spring Data

Page 32: Data access 2.0? Please welcome: Spring Data!

"… provide a familiar and consistent Spring-based programming model while retaining store-specific features and capabilities.

Page 33: Data access 2.0? Please welcome: Spring Data!

Spring Data

JPAJDBC

Page 34: Data access 2.0? Please welcome: Spring Data!

Spring Data

JPAJDBC

Page 35: Data access 2.0? Please welcome: Spring Data!

Spring Data

JPAJDBC

Page 36: Data access 2.0? Please welcome: Spring Data!

Spring Data

JPAJDBC

Page 37: Data access 2.0? Please welcome: Spring Data!

Building blocks

Page 38: Data access 2.0? Please welcome: Spring Data!

Spring

Page 39: Data access 2.0? Please welcome: Spring Data!

Mapping

Page 40: Data access 2.0? Please welcome: Spring Data!

40

JPA entity mapping

@Entityclass Person {

@Id@GeneratedValue(strategy=GenerationType.AUTO)private BigInteger id;private String firstname, lastname;

@Column(name="email") private String emailAddress;

@OneToMany private Set<Person> colleagues;

}

Page 41: Data access 2.0? Please welcome: Spring Data!

41

Entity mapping - MongoDB

@Documentclass Person {

@Id private BigInteger id;@Indexed private String firstname, lastname;@Field("email") private String emailAddress;@DBRef private Set<Person> colleagues;

public Person(String firstname) { … }

@PersistenceConstructorpublic Person(String firstname, String lastname) { … }

…}

Page 42: Data access 2.0? Please welcome: Spring Data!

42

Entity mapping - Neo4J

@NodeEntityclass Person {

@GraphId private long id;@Indexed private String firstname, lastname;@RelatedTo(direction = Direction.INCOMING)private Set<Person> colleagues;

public Person(String firstname) { … }

@PersistenceConstructorpublic Person(String firstname, String lastname) { … }

…}

Page 43: Data access 2.0? Please welcome: Spring Data!

Templates

Page 44: Data access 2.0? Please welcome: Spring Data!

44

MongoOperations / -Template

public interface MongoOperations {

// Generic callback-accepting methods <T> T execute(DbCallback<T> action);

<T> T execute(Class<?> entityClass, CollectionCallback<T> action);<T> T execute(String collectionName, CollectionCallback<T> action);

// Higher level access methods<T> List<T> find(Query query, Class<T> entityClass);void save(Object objectToSave, String collectionName);WriteResult updateFirst(Query query, Update update, Class<?>

entityClass);

// Geo API<T> GeoResults<T> geoNear(NearQuery near, Class<T> entityClass);

}

Page 45: Data access 2.0? Please welcome: Spring Data!

45

MongoTemplate usage

// Setup infrastructureMongo mongo = new Mongo();MongoDbFactory factory = new SimpleMongoDbFactory(mongo, „foo“);MongoTemplate template = new MongoTemplate(factory);

// Create and save entityPerson dave = new Person("Dave", "Matthews");dave.setEmailAddress("[email protected]");template.save(person);

// Query entityQuery query = new Query(new Criteria("emailAddress")

.is("[email protected]"));assertThat(template.find(query), is(dave));

Page 46: Data access 2.0? Please welcome: Spring Data!

Repositories

Page 47: Data access 2.0? Please welcome: Spring Data!

47

Repositories - JPA

<jpa:repositories base-package="com.acme.repositories" />

public interface PersonRepository extends Repository<Person, BigInteger>{

// Finder for a single entityPerson findByEmailAddress(String emailAddress);

// Finder for multiple entitiesList<Person> findByLastnameLike(String lastname);

// Finder with paginationPage<Person> findByFirstnameLike(String firstname, Pageable page);

}

Page 48: Data access 2.0? Please welcome: Spring Data!

48

Repositories - MongoDB

public interface PersonRepository extends Repository<Person, BigInteger>{

// Finder for a single entityPerson findByEmailAddress(String emailAddress);

// Finder for multiple entitiesList<Person> findByLastnameLike(String lastname);

// Finder with paginationPage<Person> findByFirstnameLike(String firstname, Pageable page);

// Geospatial queriesList<Person> findByLocationNear(Point location, Distance distance);GeoResults<Person> findByLocationNear(Point location);

}

Page 49: Data access 2.0? Please welcome: Spring Data!

49

Repositories - MongoDB

<mongo:repositories base-package="com.acme.repositories" />

@Componentpublic class MyClient {

@Autowiredprivate PersonRepository repository;

public List<Person> doSomething() {

Point point = new Point(43.7, 48.8);Distance distance = new Distance(200, Metrics.KILOMETERS);return repository.findByLocationNear(point, distance);

}}

Page 50: Data access 2.0? Please welcome: Spring Data!

50

Repositories - Neo4J

interface PersonRepository extends GraphRepository<Person, Long> // Finder for a single entityPerson findByEmailAddress(String emailAddress);

// Finder for multiple entitiesList<Person> findByLastnameLike(String lastname);

// Finder with paginationPage<Person> findByFirstnameLike(String firstname, Pageable page);

@Query("start person = Person(id = *) " + "match person-[:colleagues]->colleague where " + "colleague.firstname = {name}")List<Person> getPersonsWithColleaguesName( @Param("name") Movie m);

}

Page 51: Data access 2.0? Please welcome: Spring Data!

Repositories

51

Querydsl

Page 52: Data access 2.0? Please welcome: Spring Data!

52

Querydsl

QPerson $ = QPerson.person;BooleanExpression left = $.lastname.contains("eth");BooleanExpression right = $.firstname.is("Carter");

public interface QueryDslPredicateExecutor<T> {T findOne(Predicate predicate);List<T> findAll(Predicate predicate);

}

public interface PersonRepository extends Repository<Person, BigInteger>,QueryDslPredicateExecutor { … }

List<Person> result = repository.findAll(left.or(right));assertThat(result.size(), is(2));assertThat(result, hasItems(dave, carter));

Page 53: Data access 2.0? Please welcome: Spring Data!

Wrap up

Page 54: Data access 2.0? Please welcome: Spring Data!

Sophisticated mapping supportTemplates

RepositoriesQuerydsl

Spring namespaceGeospatial support (MongoDB)

Cross-store persistence

Wrap up

Page 55: Data access 2.0? Please welcome: Spring Data!

Questions?