how to use nosql - nosqlroadshow.comnosqlroadshow.com/dl/nosql-road-show/slides/nosql... · •...

28
How to Use NoSQL in Enterprise Java Applications Patrick Baumgartner NoSQL Roadshow | Basel | 30.08.2012

Upload: others

Post on 17-Mar-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: How to Use NoSQL - nosqlroadshow.comnosqlroadshow.com/dl/NoSQL-Road-Show/slides/NoSQL... · • POJOs mapped as nodes or relationships – type safe • Works directly Database, typically

How to Use NoSQL in Enterprise Java Applications Patrick Baumgartner

NoSQL Roadshow | Basel | 30.08.2012

Page 2: How to Use NoSQL - nosqlroadshow.comnosqlroadshow.com/dl/NoSQL-Road-Show/slides/NoSQL... · • POJOs mapped as nodes or relationships – type safe • Works directly Database, typically

2

Agenda

•  Speaker Profile •  New Demands on Data Access •  New Types of Data Stores •  Integrating NoSQL Data Stores •  Spring Data Overview •  Example with MongoDB •  Example with Neo4j •  Q & A

Page 3: How to Use NoSQL - nosqlroadshow.comnosqlroadshow.com/dl/NoSQL-Road-Show/slides/NoSQL... · • POJOs mapped as nodes or relationships – type safe • Works directly Database, typically

3

Speaker Profile

Patrick Baumgartner •  Senior Software Consultant | Partner •  VMware/SpringSource Certified Instructor (Spring Trainer) •  Spring Framework, OSGi & agile engineering practices •  Co-author of „OSGi für Praktiker“ (Hanser) Swiftmind GmbH http://www.swiftmind.com

•  Enterprise Java, Spring & OSGi consulting •  Spring & OSGi workshops & trainings •  Agile engineering practices workshops

Page 4: How to Use NoSQL - nosqlroadshow.comnosqlroadshow.com/dl/NoSQL-Road-Show/slides/NoSQL... · • POJOs mapped as nodes or relationships – type safe • Works directly Database, typically

4

New Demands on Data Access

•  Structured and unstructured data

•  Massive amounts of data •  Inexpensive horizontal

scaling •  Apps and data in the

cloud •  Social network features •  …

Page 5: How to Use NoSQL - nosqlroadshow.comnosqlroadshow.com/dl/NoSQL-Road-Show/slides/NoSQL... · • POJOs mapped as nodes or relationships – type safe • Works directly Database, typically

5

New Types of Data Stores

Page 6: How to Use NoSQL - nosqlroadshow.comnosqlroadshow.com/dl/NoSQL-Road-Show/slides/NoSQL... · • POJOs mapped as nodes or relationships – type safe • Works directly Database, typically

6

We are not architects! Integrating NoSQL Data Stores #1

http://www.flickr.com/photos/sakeeb/4087246274

Page 7: How to Use NoSQL - nosqlroadshow.comnosqlroadshow.com/dl/NoSQL-Road-Show/slides/NoSQL... · • POJOs mapped as nodes or relationships – type safe • Works directly Database, typically

7

Don’t re-invent the wheel! Integrating NoSQL Data Stores #2

http://www.flickr.com/photos/dmott9/5921728819

Page 8: How to Use NoSQL - nosqlroadshow.comnosqlroadshow.com/dl/NoSQL-Road-Show/slides/NoSQL... · • POJOs mapped as nodes or relationships – type safe • Works directly Database, typically

8

Let’s use Spring! Integrating NoSQL Data Stores #3

http://www.springsource.org/spring-data

Page 9: How to Use NoSQL - nosqlroadshow.comnosqlroadshow.com/dl/NoSQL-Road-Show/slides/NoSQL... · • POJOs mapped as nodes or relationships – type safe • Works directly Database, typically

9

Spring Data

•  Same goals as the Spring Framework –  Productivity improvement –  Programming model consistency –  Portability

•  Umbrella for subprojects, specific to a given database •  Mapping support for Java domain objects

•  http://www.springsource.org/spring-data •  http://github.com/SpringSource/spring-data-XXX

Page 10: How to Use NoSQL - nosqlroadshow.comnosqlroadshow.com/dl/NoSQL-Road-Show/slides/NoSQL... · • POJOs mapped as nodes or relationships – type safe • Works directly Database, typically

10

Spring Data – Overview #1

•  JPA •  JDBC Extensions

Relational Databases

• Apache Hadoop

Big Data

• GemFire

Data Grid

• REST

HTTP

Page 11: How to Use NoSQL - nosqlroadshow.comnosqlroadshow.com/dl/NoSQL-Road-Show/slides/NoSQL... · • POJOs mapped as nodes or relationships – type safe • Works directly Database, typically

11

Spring Data – Overview #2

• Redis

Key Value Stores

• Mongo DB

Document Stores

• Neo4J

Graph Databases

• HBase

Column Stores

• Commons

Common Infrastructure

Page 12: How to Use NoSQL - nosqlroadshow.comnosqlroadshow.com/dl/NoSQL-Road-Show/slides/NoSQL... · • POJOs mapped as nodes or relationships – type safe • Works directly Database, typically

12

Spring Data – Key Features

•  Low level data access API abstraction –  MongoTemplate, RiakTemplate, Neo4jTemplate –  Exception translation –  Transaction management

•  Object Mapping (Java to data store) •  Generic Repository Support

–  Basic CRUD, dynamic finders, pagination and sorting •  Spring namespaces •  Cross Store Persistence Programming Model

–  @Entity, @Document, @NodeEntity

Page 13: How to Use NoSQL - nosqlroadshow.comnosqlroadshow.com/dl/NoSQL-Road-Show/slides/NoSQL... · • POJOs mapped as nodes or relationships – type safe • Works directly Database, typically

13

Spring Data MongoDB – Example

Page 14: How to Use NoSQL - nosqlroadshow.comnosqlroadshow.com/dl/NoSQL-Road-Show/slides/NoSQL... · • POJOs mapped as nodes or relationships – type safe • Works directly Database, typically

14

Documents

Stored JSON: { "_id" : ObjectId("4f9886290364b533b3acd4ce"), "_class" : "com.acme.hello.domain.Person", "name" : "Bob", "age" : 33 }

Page 15: How to Use NoSQL - nosqlroadshow.comnosqlroadshow.com/dl/NoSQL-Road-Show/slides/NoSQL... · • POJOs mapped as nodes or relationships – type safe • Works directly Database, typically

15

Spring Data MongoDB – Document

Stored JSON: { "_id" : ObjectId("4f9886290364b533b3acd4ce"), "_class" : "com.example.Person", "name" : "Bob", "age" : 33 }

@Document public class Person { @Id private int id; private String name; private int age; // getters/setters… }

Page 16: How to Use NoSQL - nosqlroadshow.comnosqlroadshow.com/dl/NoSQL-Road-Show/slides/NoSQL... · • POJOs mapped as nodes or relationships – type safe • Works directly Database, typically

16

Spring Data MongoDB – Configuration

Page 17: How to Use NoSQL - nosqlroadshow.comnosqlroadshow.com/dl/NoSQL-Road-Show/slides/NoSQL... · • POJOs mapped as nodes or relationships – type safe • Works directly Database, typically

17

Spring Data MongoDB – Repository

@Repository public class MongoPersonRepository implements BaseRepository<Person> {

@Autowired MongoOperations mongoTemplate; Person createPerson(String name, int age){ if(!mongoTemplate.collectionExists(Person.class)){ mongoTemplate.createCollection(Person.class);

} Person p = new Person(name, age);

mongoTemplate.insert(p) return p;

}

... }

Page 18: How to Use NoSQL - nosqlroadshow.comnosqlroadshow.com/dl/NoSQL-Road-Show/slides/NoSQL... · • POJOs mapped as nodes or relationships – type safe • Works directly Database, typically

18

Spring Data Neo4j – Example

Page 19: How to Use NoSQL - nosqlroadshow.comnosqlroadshow.com/dl/NoSQL-Road-Show/slides/NoSQL... · • POJOs mapped as nodes or relationships – type safe • Works directly Database, typically

19

Graph

Page 20: How to Use NoSQL - nosqlroadshow.comnosqlroadshow.com/dl/NoSQL-Road-Show/slides/NoSQL... · • POJOs mapped as nodes or relationships – type safe • Works directly Database, typically

20

Spring Data Neo4j (SDN)

•  POJOs mapped as nodes or relationships – type safe •  Works directly Database, typically embedded mode •  Data is fetched very fast and lazy •  Stores everything within a @Transaction •  Uses heavily AspectJ magic to enhance the POJOs •  …

Page 21: How to Use NoSQL - nosqlroadshow.comnosqlroadshow.com/dl/NoSQL-Road-Show/slides/NoSQL... · • POJOs mapped as nodes or relationships – type safe • Works directly Database, typically

21

Spring Data Neo4j – Entity

_type_: com.example.Person name: "Alice" age: 42

@NodeEntity public class Person { private String name; private int age; // getters/setters… } Person alice = new Person(); alice.setName("Alice"); alice.setAge(42); alice.persist();

Node

Page 22: How to Use NoSQL - nosqlroadshow.comnosqlroadshow.com/dl/NoSQL-Road-Show/slides/NoSQL... · • POJOs mapped as nodes or relationships – type safe • Works directly Database, typically

22

Spring Data Neo4j – NodeEntity

@NodeEntity public class Person { private String name; private int yearOfBirth; @RelatedTo(type = "KNOWS", direction = Direction.OUTGOING) private Set<Person> knownPersons; public void knows(Person p) { knownPersons.add(p); } public Set<Person> getFriends() { return knownPersons; } } Person alice = ...; alice.knows(bob); alice.knows(carol);

Alice Bob KNOWS

Carol

KNOWS

Page 23: How to Use NoSQL - nosqlroadshow.comnosqlroadshow.com/dl/NoSQL-Road-Show/slides/NoSQL... · • POJOs mapped as nodes or relationships – type safe • Works directly Database, typically

23

Spring Data Neo4j – Relationship

@RelationshipEntity public class Knows { private int sinceYear; public Knows since(int year) { this.sinceYear = year; return this; } } @NodeEntity public class Person { public Known knows(Person p) { return this.relateTo(p, Knows.class, "KNOWS"); } } Person alice = ...; Person bob ...; alice.knows(bob).since(2012);

Alice Bob KNOWS since: 2012

Page 24: How to Use NoSQL - nosqlroadshow.comnosqlroadshow.com/dl/NoSQL-Road-Show/slides/NoSQL... · • POJOs mapped as nodes or relationships – type safe • Works directly Database, typically

24

Spring Data Neo4j – Repository

public interface PersonRepository extends GraphRepository<Person> {

@Query("start person = {0} match ... return ...")

Iterable<Product> getOwnedServices(Person person);

Iterable<Person> findByName(String name);

Iterable<Person> findByNameLike(String name);

}

@Autowired

PersonRepository personRepository;

Person alice = personRepository.findByName("Alice");

Page 25: How to Use NoSQL - nosqlroadshow.comnosqlroadshow.com/dl/NoSQL-Road-Show/slides/NoSQL... · • POJOs mapped as nodes or relationships – type safe • Works directly Database, typically

25

Spring Data Neo4j – Querying

•  Several possibilities implemented to query the graph –  Neo4j API –  Traversal descriptions –  Graph algorithms –  Index queries –  Cypher queries

Page 26: How to Use NoSQL - nosqlroadshow.comnosqlroadshow.com/dl/NoSQL-Road-Show/slides/NoSQL... · • POJOs mapped as nodes or relationships – type safe • Works directly Database, typically

26

Use a data model which really matches to your data … Give it a try!

http://www.flickr.com/photos/juniorvelo/3267647833

Page 27: How to Use NoSQL - nosqlroadshow.comnosqlroadshow.com/dl/NoSQL-Road-Show/slides/NoSQL... · • POJOs mapped as nodes or relationships – type safe • Works directly Database, typically

27

NoSQL für Java Enterprise Anwendungen mit Spring Data 05.09.2012 /ch/open Workshop-Tage

4th – 6th September

http://www.flickr.com/photos/4nitsirk/5211251578

Page 28: How to Use NoSQL - nosqlroadshow.comnosqlroadshow.com/dl/NoSQL-Road-Show/slides/NoSQL... · • POJOs mapped as nodes or relationships – type safe • Works directly Database, typically

28

Q & A

Patrick Baumgartner patrick.baumgartner [at] swiftmind [dot] com http://www.swiftmind.com http://www.twitter.com/patbaumgartner