Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Spring Data MongoDB

Spring Data MongoDB

Slides from Spring Data MongoDB speech given to Szczecin Java User Group on 20 of March 2013

2da55e1342d18933f0bcaf804a02f200?s=128

Maciej Walkowiak

March 20, 2013
Tweet

More Decks by Maciej Walkowiak

Other Decks in Programming

Transcript

  1. Spring Data MongoDB Maciej Walkowiak

  2. Agenda NoSQL MongoDB Spring Data Spring Data MongoDB

  3. NoSQL

  4. NoSQL

  5. key-value store document oriented column family based graph NoSQL

  6. NoSQL schemaless non-relational cluster friendly Common characteristics

  7. None
  8. document oriented full index support auto-sharding BSON map reduce non-transactional

    document linking geospatial indexes
  9. post collection entry author collection entry { "_id" : ObjectId("5146227a0364038863be7514"),

    "title" : "post title", "content" : "hello jug", "createdAt" : ISODate("2013-03-17T20:07:22.455Z"), "tags" : [ "mongodb", "jug", "szczecin" ], } { "_id" : ObjectId("514622790364038863be7513"), "name" : "maciej walkowiak", }
  10. post collection entry linked with author { "_id" : ObjectId("5146227a0364038863be7514"),

    "title" : "post title", "content" : "hello world coders", "createdAt" : ISODate("2013-03-17T20:07:22.455Z"), "tags" : [ "mongodb", "jug", "szczecin" ], "author" : DBRef("author", ObjectId("514622790364038863be7513")) }
  11. post collection entry with embedded author { "_id" : ObjectId("51476f8d03646a6b3186f081"),

    "title" : "post title", "content" : "hello world coders", "createdAt" : ISODate("2013-03-18T19:48:29.667Z"), "tags" : [ "mongodb", "jug", "szczecin" ], "author" : { "name" : "maciej walkowiak", "email" : "walkowiak.maciej@yahoo.com" } }
  12. db.post.find(); db.post.findOne({ _id: ObjectId("5146227a0364038863be7514") }); db.place.find({ coords : { $near:

    [-110,32], $maxDistance : 10/111.12 } }) db.post.find({ tags: "szczecin", createdAt: { $gt : ISODate("2013-03-01") } })
  13. MongoDB + Java

  14. MongoDB + Java

  15. MongoDB + Java Morphia Spring Data MongoDB

  16. Spring Data

  17. Spring Data Same programming model for many databases simple things

    simple, complex things possible One API to rule them all - nope reduce amount boilerplate code
  18. Spring Data Same programming model for many databases simple things

    simple, complex things possible One API to rule them all - nope reduce amount boilerplate code
  19. JPA MongoDB Redis Hadoop Neo4j HBase Spring Data Supported databases

  20. mapping template repositories Map Reduce QueryDSL Cross-Store Spring Data

  21. Mapping { "_id" : ObjectId("5149471a0364c59b4e743584"), "_class" : "pl.maciejwalkowiak.jug.mongo.Post", "post_title" :

    "post title", "content" : "hello world coders", "createdAt" : ISODate("2013-03-20T05:20:26.985Z"), "comments" : [ ], "tags" : [ "mongodb", "jug", "szczecin" ], "version" : 0, "author" : DBRef("user", ObjectId("5149471a0364c59b4e743583")) }
  22. Mapping @Document public class Post { @Id private ObjectId id;

    @DBRef private User author; @Field("post_title") private String title; private String content; @CreatedDate private DateTime createdAt; private List<Comment> comments = new ArrayList<Comment>(); private Set<String> tags = new HashSet<String>(); @Version private Integer version; ... } { "_id" : ObjectId("5149471a0364c59b4e743584"), "_class" : "pl.maciejwalkowiak.jug.mongo.Post", "post_title" : "post title", "content" : "hello world coders", "createdAt" : ISODate("2013-03-20T05:20:26.985Z"), "comments" : [ ], "tags" : [ "mongodb", "jug", "szczecin" ], "version" : 0, "author" : DBRef("user", ObjectId("5149471a0364c59b4e743583")) }
  23. Template interface MongoOperations { <T> T findOne(Query query, Class<T> entityClass);

    <T> T findAndModify(Query query, Update update, Class<T> entityClass); long count(Query query, String collectionName); void save(Object objectToSave); WriteResult updateMulti(Query query, Update update, Class<?> entityClass); <T> void remove(Query query, Class<T> entityClass); <T> GroupByResults<T> group(String inputCollectionName, GroupBy groupBy, Class<T> entityClass); <T> MapReduceResults<T> mapReduce(Query query, String inputCollectionName, String mapFunction, String reduceFunction, Class<T> entityClass); CommandResult executeCommand(DBObject command); ... }
  24. Repositories DAO in DDD CrudRepository PagingAndSortingRepository declarative find methods

  25. Repositories public interface UserRepository extends PagingAndSortingRepository<User, ObjectId>{ User findByName(String name);

    List<User> findByNameLike(String name, Pageable pageable); }
  26. Repositories public interface UserRepository extends PagingAndSortingRepository<User, ObjectId>{ User findByName(String name);

    List<User> findByNameLike(String name, Pageable pageable); } public interface PostRepository extends CrudRepository<Post, ObjectId>, PostRepositoryCustom { @Query("{$where : 'this.content.length > 20'}") List<Post> findLongPosts(); @Query("{$where : 'this.content.length > ?0'}") List<Post> findPostsLongerThan(int length); }
  27. Repositories public interface UserRepository extends PagingAndSortingRepository<User, ObjectId>{ User findByName(String name);

    List<User> findByNameLike(String name, Pageable pageable); } @Repository public class PostRepositoryImpl implements PostRepositoryCustom { @Autowired private MongoOperations mongoOperations; public Post foo() { return mongoOperations.findOne(Query.query(Criteria.where("title").exists(true)), Post.class); public interface PostRepository extends CrudRepository<Post, ObjectId>, PostRepositoryCustom { @Query("{$where : 'this.content.length > 20'}") List<Post> findLongPosts(); @Query("{$where : 'this.content.length > ?0'}") List<Post> findPostsLongerThan(int length); }
  28. Map Reduce // map function () { emit(this.description, 1); }

  29. Map Reduce // reduce function (key, values) { var sum

    = 0; for (var i = 0; i < values.length; i++) sum += values[i]; return sum; } // map function () { emit(this.description, 1); }
  30. Map Reduce MapReduceResults<ValueObject> results = mongoOperations.mapReduce("collectionName", "classpath:map.js", "classpath:reduce.js", ValueObject.class); //

    reduce function (key, values) { var sum = 0; for (var i = 0; i < values.length; i++) sum += values[i]; return sum; } // map function () { emit(this.description, 1); }
  31. QueryDSL Type-Safe criteria API

  32. QueryDSL @Generated("com.mysema.query.codegen.EntitySerializer") public class QPost extends EntityPathBase<Post> { private static

    final long serialVersionUID = -1782980951; private static final PathInits INITS = PathInits.DIRECT; public static final QPost post = new QPost("post"); public final QUser author; public final ListPath<Comment, QComment> comments = this.<Comment, QComment>createList("comments", Comment.class, QComment.class, PathInits.DIRECT); public final StringPath content = createString("content"); public final org.joda.time.QDateTime createdAt; public final org.bson.types.QObjectId id; Type-Safe criteria API
  33. QueryDSL postRepository.findAll(QPost.post.content.contains("hello"))); @Generated("com.mysema.query.codegen.EntitySerializer") public class QPost extends EntityPathBase<Post> { private

    static final long serialVersionUID = -1782980951; private static final PathInits INITS = PathInits.DIRECT; public static final QPost post = new QPost("post"); public final QUser author; public final ListPath<Comment, QComment> comments = this.<Comment, QComment>createList("comments", Comment.class, QComment.class, PathInits.DIRECT); public final StringPath content = createString("content"); public final org.joda.time.QDateTime createdAt; public final org.bson.types.QObjectId id; Type-Safe criteria API
  34. Demo

  35. MongoDB as a Service 0.5GB for free

  36. Q&A

  37. Contact walkowiak.maciej@yahoo.com @MaciejWalkowiak http://maciejwalkowiak.pl/