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

Maciej Walkowiak

March 20, 2013
Tweet

More Decks by Maciej Walkowiak

Other Decks in Programming

Transcript

  1. Spring Data MongoDB
    Maciej Walkowiak

    View Slide

  2. Agenda
    NoSQL
    MongoDB
    Spring Data
    Spring Data MongoDB

    View Slide

  3. NoSQL

    View Slide

  4. NoSQL

    View Slide

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

    View Slide

  6. NoSQL
    schemaless
    non-relational
    cluster friendly
    Common characteristics

    View Slide

  7. View Slide

  8. document oriented
    full index support
    auto-sharding
    BSON
    map reduce
    non-transactional
    document linking
    geospatial indexes

    View Slide

  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",
    }

    View Slide

  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"))
    }

    View Slide

  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" : "[email protected]"
    }
    }

    View Slide

  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") }
    })

    View Slide

  13. MongoDB + Java

    View Slide

  14. MongoDB + Java

    View Slide

  15. MongoDB + Java
    Morphia Spring Data MongoDB

    View Slide

  16. Spring Data

    View Slide

  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

    View Slide

  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

    View Slide

  19. JPA
    MongoDB
    Redis
    Hadoop
    Neo4j
    HBase
    Spring Data
    Supported databases

    View Slide

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

    View Slide

  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"))
    }

    View Slide

  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 comments
    = new ArrayList();
    private Set tags = new
    HashSet();
    @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"))
    }

    View Slide

  23. Template
    interface MongoOperations {
    T findOne(Query query, Class entityClass);
    T findAndModify(Query query, Update update, Class entityClass);
    long count(Query query, String collectionName);
    void save(Object objectToSave);
    WriteResult updateMulti(Query query, Update update, Class> entityClass);
    void remove(Query query, Class entityClass);
    GroupByResults group(String inputCollectionName, GroupBy groupBy, Class
    entityClass);
    MapReduceResults mapReduce(Query query, String inputCollectionName, String
    mapFunction, String reduceFunction, Class entityClass);
    CommandResult executeCommand(DBObject command);
    ...
    }

    View Slide

  24. Repositories
    DAO in DDD
    CrudRepository
    PagingAndSortingRepository
    declarative find methods

    View Slide

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

    View Slide

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

    View Slide

  27. Repositories
    public interface UserRepository extends PagingAndSortingRepository{
    User findByName(String name);
    List 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, PostRepositoryCustom {
    @Query("{$where : 'this.content.length > 20'}")
    List findLongPosts();
    @Query("{$where : 'this.content.length > ?0'}")
    List findPostsLongerThan(int length);
    }

    View Slide

  28. Map Reduce
    // map
    function () {
    emit(this.description, 1);
    }

    View Slide

  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);
    }

    View Slide

  30. Map Reduce
    MapReduceResults 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);
    }

    View Slide

  31. QueryDSL
    Type-Safe criteria API

    View Slide

  32. QueryDSL
    @Generated("com.mysema.query.codegen.EntitySerializer")
    public class QPost extends EntityPathBase {
    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 comments = this.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

    View Slide

  33. QueryDSL
    postRepository.findAll(QPost.post.content.contains("hello")));
    @Generated("com.mysema.query.codegen.EntitySerializer")
    public class QPost extends EntityPathBase {
    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 comments = this.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

    View Slide

  34. Demo

    View Slide

  35. MongoDB as a Service
    0.5GB for free

    View Slide

  36. Q&A

    View Slide

  37. Contact
    [email protected]
    @MaciejWalkowiak
    http://maciejwalkowiak.pl/

    View Slide