SlideShare a Scribd company logo
1 of 47
Download to read offline
Spring Data
                          Thomas Risberg
                               VMware
                           trisberg@vmware.com



Tuesday, April 17, 12                            1
•expert in NO SQL                       • committer on Spring Framework
      • currently works for VMware’s Cloud    • former Oracle DBA
        Foundry team on framework integration • former Java J2EE developer
      • member of Spring Data team              email: trisberg@vmware.com
                                                twitter: @trisberg
Tuesday, April 17, 12                                                           2
Tuesday, April 17, 12   3
New demands on data access ...
     • massive amounts of data • social networking features
     • structured and          • inexpensive horizontal scaling
       unstructured data       • deploying apps in the cloud
     • real-time analysis      • ...




                                                    image courtesy of Bitcurrent

Tuesday, April 17, 12                                                              4
New types of data stores ...
                                           Column/

                            • Redis
                                                     • Cassandra
                            • Riak
                                                     • HBase




                               • MongoDB
                               • CouchDB             • Neo4j


            Hadoop




Tuesday, April 17, 12                                              5
Spring Framework built-in
                           data access support
                        • Transaction abstractions, Data access
                          exceptions
                        • JDBC - JdbcTemplate
                        • ORM - Hibernate, JPA support
                        • OXM - Object to XML mapping
                        • Cache support (Spring 3.1)
Tuesday, April 17, 12                                             6
Spring Data
               • Bring classic Spring benefits to all databases
                        ✓ Productivity

                        ✓ Programming model consistency
               • Conventions based generic repository support
               • Mapping between Java domain objects and data
                        store
               • Support for a wide range of new databases
Tuesday, April 17, 12                                            7
Spring Data sub-projects
                        • JPA Repository
                        • JDBC Extensions
                        • Redis (Key Value)
                        • MongoDB (Document)
                        • Neo4j (Graph)
                        • Gemfire (Data Grid)
                        • Apache Hadoop (Big Data)
                        • Riak (Key Value - in development)
                        • Cassandra (Column Store - planned)
Tuesday, April 17, 12                                          8
Spring Data Building
                                 Blocks
                        • Low level data access APIs
                         ✓ MongoTemplate, RedisTemplate ...
                        • Object Mapping (Java to Datastore)
                        • Generic Repository support
                        • Cross Store Persistence Programming
                          model


Tuesday, April 17, 12                                           9
Finding Spring Data
              • GitHub: https://github.com/SpringSource
              • Web page:
                        http://www.springsource.org/spring-data

              • Forum:
                        http://forum.springsource.org/forumdisplay.php?f=80


Tuesday, April 17, 12                                                         10
Three databases for
                            today’s talk

                        •   Relational database


                        •   Document database


                        •   Graph database

Tuesday, April 17, 12                             11
Three persistence
              strategies for today’s talk
                        • Conventions based persistence --
                          Repositories

                        • Lower-level Template approach
                        • Cross-store persistence using JPA and
                          a non-relational database


Tuesday, April 17, 12                                             12
Repository



                            http://www.sxc.hu/photo/1020163




Tuesday, April 17, 12                                         13
Repository
          Mediates between the domain and data mapping layers using
          a collection-like interface for accessing domain objects.
          http://martinfowler.com/eaaCatalog/repository.html




                        http://msdn.microsoft.com/en-us/library/ff649690.aspx




Tuesday, April 17, 12                                                           14
Spring Data
                                     Repository
                        • Generic repository implementation
                        • Basic CRUD (create, read, update and
                          delete) methods
                        • Generating code for dynamic find methods
                            defined in repository interface like
                            findByName, findByAgeBetween etc.
                        •   Pagination and sorting support
                        •   Currently JPA, Mongo and Neo4j
                            implementations
Tuesday, April 17, 12                                               15
Repository<T, ID extends Serializable>

        CrudRepository<T, ID extends Serializable>




                                      PagingAndSortingRepository<T, ID extends Serializable>




Tuesday, April 17, 12                                                                          16
Query Methods




Tuesday, April 17, 12                   17
<persistence>
   <?xml version="1.0" encoding="UTF-8"?>
   <persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
       http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
     <persistence-unit name="test">
       <properties>
         <property name="hibernate.dialect"
                    value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
         <property name="hibernate.hbm2ddl.auto" value="update"/>
         <property name="hibernate.ejb.naming_strategy"
                    value="org.hibernate.cfg.ImprovedNamingStrategy"/>
       </properties>
     </persistence-unit>
   </persistence>




Tuesday, April 17, 12                                                          18
<configuration>
  <jpa:repositories base-package="org.springframework.data.demo.repository" />

  <tx:annotation-driven transaction-manager="transactionManager"/>

  <context:component-scan base-package="org.springframework.data.demo">
    <context:exclude-filter expression="org.springframework.stereotype.Controller"
        type="annotation"/>
  </context:component-scan>

  <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
  </bean>

  <bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="persistenceUnitName" value="test"/>
    <property name="jpaVendorAdapter">
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="generateDdl" value="true" />
      </bean>
    </property>
  </bean>




Tuesday, April 17, 12                                                                        19
r
                         Fo
                        lt
                   ui
               B




       <beans profile="default">
         <bean class="org.apache.commons.dbcp.BasicDataSource"
                destroy-method="close" id="dataSource">
           <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
           <property name="url" value="jdbc:mysql://localhost/test"/>
           <property name="username" value="root"/>
           <property name="password" value=""/>
           <property name="testOnBorrow" value="true"/>
         </bean>
       </beans>

       <beans profile="cloud">
         <cloud:data-source id="dataSource"/>
       </beans>




Tuesday, April 17, 12                                                         20
App using
                        JPA Repository




                              CODE
           https://github.com/trisberg/jpa-bookshelf-repository
Tuesday, April 17, 12                                             21
{
      "title" : "Spring Data REST in Action",
      "price" : 22.55,
      "_links" : [ {
        "rel" : "author",
        "href" : "http://localhost:8080/data/author"
      }, {
        "rel" : "self",
        "href" : "http://localhost:8080/data/author/2"
      } ],
      "categories" : [ "Spring", "Java" ],
      "published" : "2012-04-24"
  }

                                            {
                                                "_links" : [ {
                                                 "rel" : "Book",
          {                                      "href" : "http://localhost:8080/data/author/2/books/1234567890"
              "name" : "Jon Brisbin",           }]
              "_links" : [ {                  }
                "rel" : "books",
                "href" : "http://localhost:8080/data/author/2/books"
              }, {
                "rel" : "self",
                "href" : "http://localhost:8080/data/author/2"
              }]
          }                            https://github.com/SpringSource/spring-data-rest
Tuesday, April 17, 12                                                                                              22
Tuesday, April 17, 12   23
Application       Document
                                                Oriented
               High                                 { author: “steve”,
                                                     date: new Date(),

           Performance                           text: “About MongoDB...”,
                                                  tags: [“tech”, “database”]}




                        Horizontally Scalable
Tuesday, April 17, 12                                                           24
Documents

                        > p = {author:   “roger”,
                                 date:   new Date(),
                                 text:   “about mongoDB...”,
                                 tags:   [“tech”, “databases”]}

                        > db.posts.save(p)




Tuesday, April 17, 12                                             25
Querying
                        > db.posts.find()

                        >   { _id : ObjectId("4c4ba5c0672c685e5e8aabf3"),
                            author : "roger",
                              date : "Sat Jul 24 2010 19:47:11",
                              text : "About MongoDB...",
                              tags : [ "tech", "databases" ] }




Tuesday, April 17, 12                                                       26
Secondary Indexes

                            //   1 means ascending, -1 means descending
                            > db.posts.ensureIndex({author: 1})
                            > db.posts.find({author: 'roger'})

                        >   { _id : ObjectId("4c4ba5c0672c685e5e8aabf3"),
                            author : "roger",
                             ... }




Tuesday, April 17, 12                                                       27
Conditional Query
                               Operators
                         $all, $exists, $mod, $ne, $in, $nin, $nor, $or, $size,
                         $type, $lt, $lte, $gt, $gte

                        // find posts with any tags
                        > db.posts.find( {tags: {$exists: true }} )

                        // find posts matching a regular expression
                        > db.posts.find( {author: /^rog*/i } )

                        // count posts by author
                        > db.posts.find( {author: ‘roger’} ).count()




Tuesday, April 17, 12                                                             28
Atomic Updates
                         $set, $unset, $inc, $push, $pushAll,
                         $pull, $pullAll, $bit


                        > comment = { author: “fred”,
                                      date: new Date(),
                                      text: “Best Movie Ever”}

                        > db.posts.update( { _id: “...” },
                        	 	 	 	       $push: {comments: comment} );




Tuesday, April 17, 12                                                 29
Nested Documents
                            {   _id : ObjectId("4c4ba5c0672c685e5e8aabf3"),
                                author : "roger",
                                date : "Sat Apr 24 2011 19:47:11",
                                text : "About MongoDB...",
                                tags : [ "tech", "databases" ],
                                comments : [
                        	              {
                        	              	 author : "Fred",
                                       	
                        	              	 date : "Sat Apr 25 2010 20:51:03 GMT-0700",
                                       	
                        	              	 text : "Best Post Ever!"
                                       	
                        	              	
                                       }
                                 ]
                        }




Tuesday, April 17, 12                                                                  30
Advanced indexing
                        // Index nested documents
                        > db.posts.ensureIndex( “comments.author”: 1)
                        > db.posts.find({‘comments.author’:’Fred’})

                        // Index on tags (multi-key index)
                        > db.posts.ensureIndex( tags: 1)
                        > db.posts.find( { tags: ‘tech’ } )

                        // geospatial index
                        > db.posts.ensureIndex( “author.location”: “2d” )
                        > db.posts.find( “author.location”: { $near : [22,42] } )




Tuesday, April 17, 12                                                               31
Rich Documents
                        {   _id : ObjectId("4c4ba5c0672c685e5e8aabf3"),

                            line_items : [ { sku: ‘tt-123’,
                                             name: ‘Coltrane: Impressions’ },
                                           { sku: ‘tt-457’,
                                             name: ‘Davis: Kind of Blue’ } ],

                            address : { name: ‘Banker’,
                                        street: ‘111 Main’,
                                        zip: 10010 },

                            payment: { cc: 4567,
                                       exp: Date(2011, 7, 7) },

                            subtotal: 2355
                        }



Tuesday, April 17, 12                                                           32
Getting started with

             $   wget http://fastdl.mongodb.org/osx/mongodb-osx-x86_64-2.0.4.tgz
             $   tar xvzf mongodb-osx-x86_64-2.0.4.tgz
             $   cd mongodb-osx-x86_64-2.0.4
             $   ./bin/mongod --dbpath /Users/trisberg/Data/mongodb/
             Sun Apr 15 11:33:11 [initandlisten] MongoDB starting : pid=34692 port=27017 dbpath=/Users/trisberg/Data/
             mongodb/ 64-bit host=Montserrat.local
             Sun Apr 15 11:33:11 [initandlisten] db version v2.0.4, pdfile version 4.5
             Sun Apr 15 11:33:11 [initandlisten] git version: 329f3c47fe8136c03392c8f0e548506cb21f8ebf
             Sun Apr 15 11:33:11 [initandlisten] build info: Darwin erh2.10gen.cc 9.8.0 Darwin Kernel Version 9.8.0:
             Wed Jul 15 16:55:01 PDT 2009; root:xnu-1228.15.4~1/RELEASE_I386 i386 BOOST_LIB_VERSION=1_40
             Sun Apr 15 11:33:11 [initandlisten] options: { dbpath: "/Users/trisberg/Data/mongodb/" }
             Sun Apr 15 11:33:11 [initandlisten] journal dir=/Users/trisberg/Data/mongodb/journal
             Sun Apr 15 11:33:11 [initandlisten] recover : no journal files present, no recovery needed
             Sun Apr 15 11:33:12 [websvr] admin web console waiting for connections on port 28017
             Sun Apr 15 11:33:12 [initandlisten] waiting for connections on port 27017




Tuesday, April 17, 12                                                                                                   33
&
           $ vmc create-service mongodb mongo-books
           $ vmc tunnel mongo-books mongo
           Password: *****
           Binding Service [mongo-books]: OK
           Stopping Application: OK
           Staging Application: OK
           Starting Application: OK
           Getting tunnel connection info: OK

           Service connection info:
             username : 97da4cc2-8919-42f9-8bdb-11f0e565c2b6
             password : 61b04646-4f9f-4804-8360-20662c2a2c95
             name     : db

           Starting tunnel to mongo-books on port 10000.
           Launching 'mongo --host localhost --port 10000 -u 97da4cc2-8919-42f9-8bdb-11f0e565c2b6 -p
           61b04646-4f9f-4804-8360-20662c2a2c95 db'

           MongoDB shell version: 2.0.1
           connecting to: localhost:10000/db
           > show collections
           author
           book
           system.indexes
           system.users
           > db.author.find()
           { "_id" : ObjectId("4f8af65d942bb7cb40b29764"), "_class" : "org.springframework.data.demo.domain.Author",
           "name" : "Craig Walls" }
           { "_id" : ObjectId("4f8af65d942bb7cb40b29765"), "_class" : "org.springframework.data.demo.domain.Author",
           "name" : "Kristina Chodorow" }
           ...



Tuesday, April 17, 12                                                                                                  34
Spring Data MongoDB
        ✓ MongoTemplate
              •         MongoConverter interface for mapping Mongo documents
              •         MappingMongoConverter for POJO mapping support,
                        leveraging Spring TypeConverters
              •         Annotation based mapping (@Document, @Id, @DbRef)
              •         Connection affinity callbacks
              •         Exception translation
              •         Support for GridFS and Map Reduce
        ✓ MongoRepository
              •         Similar support as JPA Repositories

Tuesday, April 17, 12                                                          35
<configuration>
          <bean id="mongoTemplate"
              class="org.springframework.data.mongodb.core.MongoTemplate">
            <constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
          </bean>

          <context:component-scan base-package="org.springframework.data.demo">
            <context:exclude-filter
                expression="org.springframework.stereotype.Controller"
                type="annotation"/>
          </context:component-scan>




Tuesday, April 17, 12                                                             36
r
                         Fo
                        lt
                   ui
               B




      <beans profile="default">
        <mongo:db-factory host="localhost" port="27017" dbname="db"/>
      </beans>

      <beans profile="cloud">
        <cloud:mongo-db-factory id="mongoDbFactory" write-concern="NORMAL"/>
      </beans>




Tuesday, April 17, 12                                                          37
Document store using
                          MongoTemplate




                            LIVE CODE
        https://github.com/trisberg/mongo-bookshelf-template
Tuesday, April 17, 12                                          38
• DB is a collection of graph nodes, relationships
              • Nodes and relationships have properties
              • Querying is done via a traversal API
              • Indexes on node/relationship properties
              • Written in Java, can be embedded
              • Transactional (ACID)
              • Master-Slave replication
              • Standalone using REST API
Tuesday, April 17, 12                                              39
Neo4j Data Model




Tuesday, April 17, 12                      40
Features
           •       Support for property graphs (nodes connected via
                   relationships, each with arbitrary properties)
           •       Transparent mapping of annotated POJO entities (using
                   AspectJ
           •       Neo4jTemplate with convenient API, exception translation
                   and optional transaction management
           •       Supports the Cypher and Gremlin query languages
           •       Dynamic type projections (duck typing)
           •       Spring Data Repositories Support
           •       Cross-store support for partial JPA – Graph Entities

Tuesday, April 17, 12                                                         41
Cross-store
           • JPA data and “NOSQL” data can share a data model
           • Separate the persistence provider by using annotations
                  – could be the entire Entity
                  – or, some of the fields of an Entity
           • We call this cross-store persistence
                  – One transaction manager to coordinate the “NOSQL” store
                    with the JPA relational database
                  – AspectJ support to manage the “NOSQL” entities and fields
                        • holds on to changed values in “change sets” until the
                          transaction commits for non-transactional data stores



Tuesday, April 17, 12                                                             42
A cross-store scenario ...
         You have a traditional web app using JPA to persist data to
           a relational database




Tuesday, April 17, 12                                                  43
JPA Data Model
                                Restaurant                           UserAccount
                        @Entity                       @Entity
                        public class Restaurant {     @Table(name = "user_account")
                            @Id @GeneratedValue       public class UserAccount {
                            private Long id;              @Id @GeneratedValue
                            private String name;          private Long id;
                            private String city;          private String userName;
                            private String state;         private String firstName;
                            private String zipCode;       private String lastName;
                                                          @Temporal(TemporalType.TIMESTAMP)
                                                          private Date birthDate;
                                                          @ManyToMany(cascade = CascadeType.ALL)
                                                          private Set<Restaurant> favorites;




Tuesday, April 17, 12                                                                              44
Cross-store
                                       Data Model
                                  Restaurant                              UserAccount
                         @Entity                           @Entity
                         @NodeEntity(partial = true)       @Table(name = "user_account")
                         public class Restaurant {         @NodeEntity(partial = true)
                           @Id @GeneratedValue             public class UserAccount {
                           private Long id;                  @Id @GeneratedValue
                           private String name;              private Long id;
                           private String city;              private String userName;
                           private String state;             private String firstName;
                           private String zipCode;           private String lastName;
                                                             @Temporal(TemporalType.TIMESTAMP)
                                                             private Date birthDate;
                                                             @ManyToMany(cascade = CascadeType.ALL)
                               Recommendation                private Set<Restaurant> favorites;

                        @RelationshipEntity                  @GraphProperty
                        public class Recommendation {        String nickname;
                          @StartNode                         @RelatedTo(type = "friends",
                          private UserAccount user;               elementClass = UserAccount.class)
                          @EndNode                           Set<UserAccount> friends;
                          private Restaurant restaurant;     @RelatedToVia(type = "recommends",
                          private int stars;                      elementClass = Recommendation.class)
                          private String comment;            Iterable<Recommendation> recommendations;




Tuesday, April 17, 12                                                                                    45
Cross-store persistence
                    with JPA/Neo4j




                             DEMO
            https://github.com/SpringSource/spring-data-neo4j/tree/
       2.0.1.RELEASE/spring-data-neo4j-examples/myrestaurants-social
Tuesday, April 17, 12                                                  46
Questions?




                        http://www.sxc.hu/photo/860327




Tuesday, April 17, 12                                    47

More Related Content

What's hot

Nonrelational Databases
Nonrelational DatabasesNonrelational Databases
Nonrelational DatabasesUdi Bauman
 
ODI11g, Hadoop and "Big Data" Sources
ODI11g, Hadoop and "Big Data" SourcesODI11g, Hadoop and "Big Data" Sources
ODI11g, Hadoop and "Big Data" SourcesMark Rittman
 
Russell 2012 introduction to spring integration and spring batch
Russell 2012   introduction to spring integration and spring batchRussell 2012   introduction to spring integration and spring batch
Russell 2012 introduction to spring integration and spring batchGaryPRussell
 
Non Relational Databases
Non Relational DatabasesNon Relational Databases
Non Relational DatabasesChris Baglieri
 
NoSQL Data Architecture Patterns
NoSQL Data ArchitecturePatternsNoSQL Data ArchitecturePatterns
NoSQL Data Architecture PatternsMaynooth University
 
NoSQL databases and managing big data
NoSQL databases and managing big dataNoSQL databases and managing big data
NoSQL databases and managing big dataSteven Francia
 
BDM9 - Comparison of Oracle RDBMS and Cloudera Impala for a hospital use case
BDM9 - Comparison of Oracle RDBMS and Cloudera Impala for a hospital use caseBDM9 - Comparison of Oracle RDBMS and Cloudera Impala for a hospital use case
BDM9 - Comparison of Oracle RDBMS and Cloudera Impala for a hospital use caseDavid Lauzon
 
Apache Tajo - An open source big data warehouse
Apache Tajo - An open source big data warehouseApache Tajo - An open source big data warehouse
Apache Tajo - An open source big data warehousehadoopsphere
 
NoSQL and MapReduce
NoSQL and MapReduceNoSQL and MapReduce
NoSQL and MapReduceJ Singh
 
NoSQL Architecture Overview
NoSQL Architecture OverviewNoSQL Architecture Overview
NoSQL Architecture OverviewChristopher Foot
 
Deep-Dive into Big Data ETL with ODI12c and Oracle Big Data Connectors
Deep-Dive into Big Data ETL with ODI12c and Oracle Big Data ConnectorsDeep-Dive into Big Data ETL with ODI12c and Oracle Big Data Connectors
Deep-Dive into Big Data ETL with ODI12c and Oracle Big Data ConnectorsMark Rittman
 
NOSQL Databases types and Uses
NOSQL Databases types and UsesNOSQL Databases types and Uses
NOSQL Databases types and UsesSuvradeep Rudra
 
NoSQL Now! NoSQL Architecture Patterns
NoSQL Now! NoSQL Architecture PatternsNoSQL Now! NoSQL Architecture Patterns
NoSQL Now! NoSQL Architecture PatternsDATAVERSITY
 

What's hot (20)

NOSQL Overview
NOSQL OverviewNOSQL Overview
NOSQL Overview
 
Nonrelational Databases
Nonrelational DatabasesNonrelational Databases
Nonrelational Databases
 
ODI11g, Hadoop and "Big Data" Sources
ODI11g, Hadoop and "Big Data" SourcesODI11g, Hadoop and "Big Data" Sources
ODI11g, Hadoop and "Big Data" Sources
 
Russell 2012 introduction to spring integration and spring batch
Russell 2012   introduction to spring integration and spring batchRussell 2012   introduction to spring integration and spring batch
Russell 2012 introduction to spring integration and spring batch
 
Hdfs Dhruba
Hdfs DhrubaHdfs Dhruba
Hdfs Dhruba
 
Non Relational Databases
Non Relational DatabasesNon Relational Databases
Non Relational Databases
 
NoSQL Basics and MongDB
NoSQL Basics and  MongDBNoSQL Basics and  MongDB
NoSQL Basics and MongDB
 
NoSQL Basics - a quick tour
NoSQL Basics - a quick tourNoSQL Basics - a quick tour
NoSQL Basics - a quick tour
 
NoSQL Data Architecture Patterns
NoSQL Data ArchitecturePatternsNoSQL Data ArchitecturePatterns
NoSQL Data Architecture Patterns
 
NoSQL databases and managing big data
NoSQL databases and managing big dataNoSQL databases and managing big data
NoSQL databases and managing big data
 
NoSQL
NoSQLNoSQL
NoSQL
 
BDM9 - Comparison of Oracle RDBMS and Cloudera Impala for a hospital use case
BDM9 - Comparison of Oracle RDBMS and Cloudera Impala for a hospital use caseBDM9 - Comparison of Oracle RDBMS and Cloudera Impala for a hospital use case
BDM9 - Comparison of Oracle RDBMS and Cloudera Impala for a hospital use case
 
NoSQL databases
NoSQL databasesNoSQL databases
NoSQL databases
 
Apache Tajo - An open source big data warehouse
Apache Tajo - An open source big data warehouseApache Tajo - An open source big data warehouse
Apache Tajo - An open source big data warehouse
 
NoSQL and MapReduce
NoSQL and MapReduceNoSQL and MapReduce
NoSQL and MapReduce
 
NoSQL Architecture Overview
NoSQL Architecture OverviewNoSQL Architecture Overview
NoSQL Architecture Overview
 
Mongo db
Mongo dbMongo db
Mongo db
 
Deep-Dive into Big Data ETL with ODI12c and Oracle Big Data Connectors
Deep-Dive into Big Data ETL with ODI12c and Oracle Big Data ConnectorsDeep-Dive into Big Data ETL with ODI12c and Oracle Big Data Connectors
Deep-Dive into Big Data ETL with ODI12c and Oracle Big Data Connectors
 
NOSQL Databases types and Uses
NOSQL Databases types and UsesNOSQL Databases types and Uses
NOSQL Databases types and Uses
 
NoSQL Now! NoSQL Architecture Patterns
NoSQL Now! NoSQL Architecture PatternsNoSQL Now! NoSQL Architecture Patterns
NoSQL Now! NoSQL Architecture Patterns
 

Similar to Spring Data NHJUG April 2012

CloudFoundry and MongoDb, a marriage made in heaven
CloudFoundry and MongoDb, a marriage made in heavenCloudFoundry and MongoDb, a marriage made in heaven
CloudFoundry and MongoDb, a marriage made in heavenPatrick Chanezon
 
Thomas risberg mongosv-2012-spring-data-cloud-foundry
Thomas risberg mongosv-2012-spring-data-cloud-foundryThomas risberg mongosv-2012-spring-data-cloud-foundry
Thomas risberg mongosv-2012-spring-data-cloud-foundrytrisberg
 
Navigating NoSQL in cloudy skies
Navigating NoSQL in cloudy skiesNavigating NoSQL in cloudy skies
Navigating NoSQL in cloudy skiesshnkr_rmchndrn
 
How to use NoSQL in Enterprise Java Applications - NoSQL Roadshow Zurich
How to use NoSQL in Enterprise Java Applications - NoSQL Roadshow ZurichHow to use NoSQL in Enterprise Java Applications - NoSQL Roadshow Zurich
How to use NoSQL in Enterprise Java Applications - NoSQL Roadshow ZurichPatrick Baumgartner
 
No SQL : Which way to go? Presented at DDDMelbourne 2015
No SQL : Which way to go?  Presented at DDDMelbourne 2015No SQL : Which way to go?  Presented at DDDMelbourne 2015
No SQL : Which way to go? Presented at DDDMelbourne 2015Himanshu Desai
 
Open Science Days 2014 - Becker - Repositories and Linked Data
Open Science Days 2014 - Becker - Repositories and Linked DataOpen Science Days 2014 - Becker - Repositories and Linked Data
Open Science Days 2014 - Becker - Repositories and Linked DataPascal-Nicolas Becker
 
NoSQL Data Stores in Research and Practice - ICDE 2016 Tutorial - Extended Ve...
NoSQL Data Stores in Research and Practice - ICDE 2016 Tutorial - Extended Ve...NoSQL Data Stores in Research and Practice - ICDE 2016 Tutorial - Extended Ve...
NoSQL Data Stores in Research and Practice - ICDE 2016 Tutorial - Extended Ve...Felix Gessert
 
SWIB14 Weaving repository contents into the Semantic Web
SWIB14 Weaving repository contents into the Semantic WebSWIB14 Weaving repository contents into the Semantic Web
SWIB14 Weaving repository contents into the Semantic WebPascal-Nicolas Becker
 
Demystifying data engineering
Demystifying data engineeringDemystifying data engineering
Demystifying data engineeringThang Bui (Bob)
 
Lviv EDGE 2 - NoSQL
Lviv EDGE 2 - NoSQLLviv EDGE 2 - NoSQL
Lviv EDGE 2 - NoSQLzenyk
 
Big Data (NJ SQL Server User Group)
Big Data (NJ SQL Server User Group)Big Data (NJ SQL Server User Group)
Big Data (NJ SQL Server User Group)Don Demcsak
 
A Survey of Advanced Non-relational Database Systems: Approaches and Applicat...
A Survey of Advanced Non-relational Database Systems: Approaches and Applicat...A Survey of Advanced Non-relational Database Systems: Approaches and Applicat...
A Survey of Advanced Non-relational Database Systems: Approaches and Applicat...Qian Lin
 
Big Data Analytics: Finding diamonds in the rough with Azure
Big Data Analytics: Finding diamonds in the rough with AzureBig Data Analytics: Finding diamonds in the rough with Azure
Big Data Analytics: Finding diamonds in the rough with AzureChristos Charmatzis
 
Graph basedrdf storeforapachecassandra
Graph basedrdf storeforapachecassandraGraph basedrdf storeforapachecassandra
Graph basedrdf storeforapachecassandraRavindra Ranwala
 
SQL? NoSQL? NewSQL?!? What’s a Java developer to do? - JDC2012 Cairo, Egypt
SQL? NoSQL? NewSQL?!? What’s a Java developer to do? - JDC2012 Cairo, EgyptSQL? NoSQL? NewSQL?!? What’s a Java developer to do? - JDC2012 Cairo, Egypt
SQL? NoSQL? NewSQL?!? What’s a Java developer to do? - JDC2012 Cairo, EgyptChris Richardson
 

Similar to Spring Data NHJUG April 2012 (20)

CloudFoundry and MongoDb, a marriage made in heaven
CloudFoundry and MongoDb, a marriage made in heavenCloudFoundry and MongoDb, a marriage made in heaven
CloudFoundry and MongoDb, a marriage made in heaven
 
Thomas risberg mongosv-2012-spring-data-cloud-foundry
Thomas risberg mongosv-2012-spring-data-cloud-foundryThomas risberg mongosv-2012-spring-data-cloud-foundry
Thomas risberg mongosv-2012-spring-data-cloud-foundry
 
Navigating NoSQL in cloudy skies
Navigating NoSQL in cloudy skiesNavigating NoSQL in cloudy skies
Navigating NoSQL in cloudy skies
 
How to use NoSQL in Enterprise Java Applications - NoSQL Roadshow Zurich
How to use NoSQL in Enterprise Java Applications - NoSQL Roadshow ZurichHow to use NoSQL in Enterprise Java Applications - NoSQL Roadshow Zurich
How to use NoSQL in Enterprise Java Applications - NoSQL Roadshow Zurich
 
No SQL : Which way to go? Presented at DDDMelbourne 2015
No SQL : Which way to go?  Presented at DDDMelbourne 2015No SQL : Which way to go?  Presented at DDDMelbourne 2015
No SQL : Which way to go? Presented at DDDMelbourne 2015
 
NoSQL, which way to go?
NoSQL, which way to go?NoSQL, which way to go?
NoSQL, which way to go?
 
Sql vs nosql
Sql vs nosqlSql vs nosql
Sql vs nosql
 
Architecting Your First Big Data Implementation
Architecting Your First Big Data ImplementationArchitecting Your First Big Data Implementation
Architecting Your First Big Data Implementation
 
Open Science Days 2014 - Becker - Repositories and Linked Data
Open Science Days 2014 - Becker - Repositories and Linked DataOpen Science Days 2014 - Becker - Repositories and Linked Data
Open Science Days 2014 - Becker - Repositories and Linked Data
 
NoSQL Data Stores in Research and Practice - ICDE 2016 Tutorial - Extended Ve...
NoSQL Data Stores in Research and Practice - ICDE 2016 Tutorial - Extended Ve...NoSQL Data Stores in Research and Practice - ICDE 2016 Tutorial - Extended Ve...
NoSQL Data Stores in Research and Practice - ICDE 2016 Tutorial - Extended Ve...
 
SWIB14 Weaving repository contents into the Semantic Web
SWIB14 Weaving repository contents into the Semantic WebSWIB14 Weaving repository contents into the Semantic Web
SWIB14 Weaving repository contents into the Semantic Web
 
Demystifying data engineering
Demystifying data engineeringDemystifying data engineering
Demystifying data engineering
 
Sql no sql
Sql no sqlSql no sql
Sql no sql
 
Taming NoSQL with Spring Data
Taming NoSQL with Spring DataTaming NoSQL with Spring Data
Taming NoSQL with Spring Data
 
Lviv EDGE 2 - NoSQL
Lviv EDGE 2 - NoSQLLviv EDGE 2 - NoSQL
Lviv EDGE 2 - NoSQL
 
Big Data (NJ SQL Server User Group)
Big Data (NJ SQL Server User Group)Big Data (NJ SQL Server User Group)
Big Data (NJ SQL Server User Group)
 
A Survey of Advanced Non-relational Database Systems: Approaches and Applicat...
A Survey of Advanced Non-relational Database Systems: Approaches and Applicat...A Survey of Advanced Non-relational Database Systems: Approaches and Applicat...
A Survey of Advanced Non-relational Database Systems: Approaches and Applicat...
 
Big Data Analytics: Finding diamonds in the rough with Azure
Big Data Analytics: Finding diamonds in the rough with AzureBig Data Analytics: Finding diamonds in the rough with Azure
Big Data Analytics: Finding diamonds in the rough with Azure
 
Graph basedrdf storeforapachecassandra
Graph basedrdf storeforapachecassandraGraph basedrdf storeforapachecassandra
Graph basedrdf storeforapachecassandra
 
SQL? NoSQL? NewSQL?!? What’s a Java developer to do? - JDC2012 Cairo, Egypt
SQL? NoSQL? NewSQL?!? What’s a Java developer to do? - JDC2012 Cairo, EgyptSQL? NoSQL? NewSQL?!? What’s a Java developer to do? - JDC2012 Cairo, Egypt
SQL? NoSQL? NewSQL?!? What’s a Java developer to do? - JDC2012 Cairo, Egypt
 

Recently uploaded

What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 

Recently uploaded (20)

What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 

Spring Data NHJUG April 2012

  • 1. Spring Data Thomas Risberg VMware trisberg@vmware.com Tuesday, April 17, 12 1
  • 2. •expert in NO SQL • committer on Spring Framework • currently works for VMware’s Cloud • former Oracle DBA Foundry team on framework integration • former Java J2EE developer • member of Spring Data team email: trisberg@vmware.com twitter: @trisberg Tuesday, April 17, 12 2
  • 4. New demands on data access ... • massive amounts of data • social networking features • structured and • inexpensive horizontal scaling unstructured data • deploying apps in the cloud • real-time analysis • ... image courtesy of Bitcurrent Tuesday, April 17, 12 4
  • 5. New types of data stores ... Column/ • Redis • Cassandra • Riak • HBase • MongoDB • CouchDB • Neo4j Hadoop Tuesday, April 17, 12 5
  • 6. Spring Framework built-in data access support • Transaction abstractions, Data access exceptions • JDBC - JdbcTemplate • ORM - Hibernate, JPA support • OXM - Object to XML mapping • Cache support (Spring 3.1) Tuesday, April 17, 12 6
  • 7. Spring Data • Bring classic Spring benefits to all databases ✓ Productivity ✓ Programming model consistency • Conventions based generic repository support • Mapping between Java domain objects and data store • Support for a wide range of new databases Tuesday, April 17, 12 7
  • 8. Spring Data sub-projects • JPA Repository • JDBC Extensions • Redis (Key Value) • MongoDB (Document) • Neo4j (Graph) • Gemfire (Data Grid) • Apache Hadoop (Big Data) • Riak (Key Value - in development) • Cassandra (Column Store - planned) Tuesday, April 17, 12 8
  • 9. Spring Data Building Blocks • Low level data access APIs ✓ MongoTemplate, RedisTemplate ... • Object Mapping (Java to Datastore) • Generic Repository support • Cross Store Persistence Programming model Tuesday, April 17, 12 9
  • 10. Finding Spring Data • GitHub: https://github.com/SpringSource • Web page: http://www.springsource.org/spring-data • Forum: http://forum.springsource.org/forumdisplay.php?f=80 Tuesday, April 17, 12 10
  • 11. Three databases for today’s talk • Relational database • Document database • Graph database Tuesday, April 17, 12 11
  • 12. Three persistence strategies for today’s talk • Conventions based persistence -- Repositories • Lower-level Template approach • Cross-store persistence using JPA and a non-relational database Tuesday, April 17, 12 12
  • 13. Repository http://www.sxc.hu/photo/1020163 Tuesday, April 17, 12 13
  • 14. Repository Mediates between the domain and data mapping layers using a collection-like interface for accessing domain objects. http://martinfowler.com/eaaCatalog/repository.html http://msdn.microsoft.com/en-us/library/ff649690.aspx Tuesday, April 17, 12 14
  • 15. Spring Data Repository • Generic repository implementation • Basic CRUD (create, read, update and delete) methods • Generating code for dynamic find methods defined in repository interface like findByName, findByAgeBetween etc. • Pagination and sorting support • Currently JPA, Mongo and Neo4j implementations Tuesday, April 17, 12 15
  • 16. Repository<T, ID extends Serializable> CrudRepository<T, ID extends Serializable> PagingAndSortingRepository<T, ID extends Serializable> Tuesday, April 17, 12 16
  • 18. <persistence> <?xml version="1.0" encoding="UTF-8"?> <persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"> <persistence-unit name="test"> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/> <property name="hibernate.hbm2ddl.auto" value="update"/> <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/> </properties> </persistence-unit> </persistence> Tuesday, April 17, 12 18
  • 19. <configuration> <jpa:repositories base-package="org.springframework.data.demo.repository" /> <tx:annotation-driven transaction-manager="transactionManager"/> <context:component-scan base-package="org.springframework.data.demo"> <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/> </context:component-scan> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="persistenceUnitName" value="test"/> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="generateDdl" value="true" /> </bean> </property> </bean> Tuesday, April 17, 12 19
  • 20. r Fo lt ui B <beans profile="default"> <bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost/test"/> <property name="username" value="root"/> <property name="password" value=""/> <property name="testOnBorrow" value="true"/> </bean> </beans> <beans profile="cloud"> <cloud:data-source id="dataSource"/> </beans> Tuesday, April 17, 12 20
  • 21. App using JPA Repository CODE https://github.com/trisberg/jpa-bookshelf-repository Tuesday, April 17, 12 21
  • 22. { "title" : "Spring Data REST in Action", "price" : 22.55, "_links" : [ { "rel" : "author", "href" : "http://localhost:8080/data/author" }, { "rel" : "self", "href" : "http://localhost:8080/data/author/2" } ], "categories" : [ "Spring", "Java" ], "published" : "2012-04-24" } { "_links" : [ { "rel" : "Book", { "href" : "http://localhost:8080/data/author/2/books/1234567890" "name" : "Jon Brisbin", }] "_links" : [ { } "rel" : "books", "href" : "http://localhost:8080/data/author/2/books" }, { "rel" : "self", "href" : "http://localhost:8080/data/author/2" }] } https://github.com/SpringSource/spring-data-rest Tuesday, April 17, 12 22
  • 24. Application Document Oriented High { author: “steve”, date: new Date(), Performance text: “About MongoDB...”, tags: [“tech”, “database”]} Horizontally Scalable Tuesday, April 17, 12 24
  • 25. Documents > p = {author: “roger”, date: new Date(), text: “about mongoDB...”, tags: [“tech”, “databases”]} > db.posts.save(p) Tuesday, April 17, 12 25
  • 26. Querying > db.posts.find() > { _id : ObjectId("4c4ba5c0672c685e5e8aabf3"), author : "roger", date : "Sat Jul 24 2010 19:47:11", text : "About MongoDB...", tags : [ "tech", "databases" ] } Tuesday, April 17, 12 26
  • 27. Secondary Indexes // 1 means ascending, -1 means descending > db.posts.ensureIndex({author: 1}) > db.posts.find({author: 'roger'}) > { _id : ObjectId("4c4ba5c0672c685e5e8aabf3"), author : "roger", ... } Tuesday, April 17, 12 27
  • 28. Conditional Query Operators $all, $exists, $mod, $ne, $in, $nin, $nor, $or, $size, $type, $lt, $lte, $gt, $gte // find posts with any tags > db.posts.find( {tags: {$exists: true }} ) // find posts matching a regular expression > db.posts.find( {author: /^rog*/i } ) // count posts by author > db.posts.find( {author: ‘roger’} ).count() Tuesday, April 17, 12 28
  • 29. Atomic Updates $set, $unset, $inc, $push, $pushAll, $pull, $pullAll, $bit > comment = { author: “fred”, date: new Date(), text: “Best Movie Ever”} > db.posts.update( { _id: “...” }, $push: {comments: comment} ); Tuesday, April 17, 12 29
  • 30. Nested Documents { _id : ObjectId("4c4ba5c0672c685e5e8aabf3"), author : "roger", date : "Sat Apr 24 2011 19:47:11", text : "About MongoDB...", tags : [ "tech", "databases" ], comments : [ { author : "Fred", date : "Sat Apr 25 2010 20:51:03 GMT-0700", text : "Best Post Ever!" } ] } Tuesday, April 17, 12 30
  • 31. Advanced indexing // Index nested documents > db.posts.ensureIndex( “comments.author”: 1) > db.posts.find({‘comments.author’:’Fred’}) // Index on tags (multi-key index) > db.posts.ensureIndex( tags: 1) > db.posts.find( { tags: ‘tech’ } ) // geospatial index > db.posts.ensureIndex( “author.location”: “2d” ) > db.posts.find( “author.location”: { $near : [22,42] } ) Tuesday, April 17, 12 31
  • 32. Rich Documents { _id : ObjectId("4c4ba5c0672c685e5e8aabf3"), line_items : [ { sku: ‘tt-123’, name: ‘Coltrane: Impressions’ }, { sku: ‘tt-457’, name: ‘Davis: Kind of Blue’ } ], address : { name: ‘Banker’, street: ‘111 Main’, zip: 10010 }, payment: { cc: 4567, exp: Date(2011, 7, 7) }, subtotal: 2355 } Tuesday, April 17, 12 32
  • 33. Getting started with $ wget http://fastdl.mongodb.org/osx/mongodb-osx-x86_64-2.0.4.tgz $ tar xvzf mongodb-osx-x86_64-2.0.4.tgz $ cd mongodb-osx-x86_64-2.0.4 $ ./bin/mongod --dbpath /Users/trisberg/Data/mongodb/ Sun Apr 15 11:33:11 [initandlisten] MongoDB starting : pid=34692 port=27017 dbpath=/Users/trisberg/Data/ mongodb/ 64-bit host=Montserrat.local Sun Apr 15 11:33:11 [initandlisten] db version v2.0.4, pdfile version 4.5 Sun Apr 15 11:33:11 [initandlisten] git version: 329f3c47fe8136c03392c8f0e548506cb21f8ebf Sun Apr 15 11:33:11 [initandlisten] build info: Darwin erh2.10gen.cc 9.8.0 Darwin Kernel Version 9.8.0: Wed Jul 15 16:55:01 PDT 2009; root:xnu-1228.15.4~1/RELEASE_I386 i386 BOOST_LIB_VERSION=1_40 Sun Apr 15 11:33:11 [initandlisten] options: { dbpath: "/Users/trisberg/Data/mongodb/" } Sun Apr 15 11:33:11 [initandlisten] journal dir=/Users/trisberg/Data/mongodb/journal Sun Apr 15 11:33:11 [initandlisten] recover : no journal files present, no recovery needed Sun Apr 15 11:33:12 [websvr] admin web console waiting for connections on port 28017 Sun Apr 15 11:33:12 [initandlisten] waiting for connections on port 27017 Tuesday, April 17, 12 33
  • 34. & $ vmc create-service mongodb mongo-books $ vmc tunnel mongo-books mongo Password: ***** Binding Service [mongo-books]: OK Stopping Application: OK Staging Application: OK Starting Application: OK Getting tunnel connection info: OK Service connection info: username : 97da4cc2-8919-42f9-8bdb-11f0e565c2b6 password : 61b04646-4f9f-4804-8360-20662c2a2c95 name : db Starting tunnel to mongo-books on port 10000. Launching 'mongo --host localhost --port 10000 -u 97da4cc2-8919-42f9-8bdb-11f0e565c2b6 -p 61b04646-4f9f-4804-8360-20662c2a2c95 db' MongoDB shell version: 2.0.1 connecting to: localhost:10000/db > show collections author book system.indexes system.users > db.author.find() { "_id" : ObjectId("4f8af65d942bb7cb40b29764"), "_class" : "org.springframework.data.demo.domain.Author", "name" : "Craig Walls" } { "_id" : ObjectId("4f8af65d942bb7cb40b29765"), "_class" : "org.springframework.data.demo.domain.Author", "name" : "Kristina Chodorow" } ... Tuesday, April 17, 12 34
  • 35. Spring Data MongoDB ✓ MongoTemplate • MongoConverter interface for mapping Mongo documents • MappingMongoConverter for POJO mapping support, leveraging Spring TypeConverters • Annotation based mapping (@Document, @Id, @DbRef) • Connection affinity callbacks • Exception translation • Support for GridFS and Map Reduce ✓ MongoRepository • Similar support as JPA Repositories Tuesday, April 17, 12 35
  • 36. <configuration> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/> </bean> <context:component-scan base-package="org.springframework.data.demo"> <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/> </context:component-scan> Tuesday, April 17, 12 36
  • 37. r Fo lt ui B <beans profile="default"> <mongo:db-factory host="localhost" port="27017" dbname="db"/> </beans> <beans profile="cloud"> <cloud:mongo-db-factory id="mongoDbFactory" write-concern="NORMAL"/> </beans> Tuesday, April 17, 12 37
  • 38. Document store using MongoTemplate LIVE CODE https://github.com/trisberg/mongo-bookshelf-template Tuesday, April 17, 12 38
  • 39. • DB is a collection of graph nodes, relationships • Nodes and relationships have properties • Querying is done via a traversal API • Indexes on node/relationship properties • Written in Java, can be embedded • Transactional (ACID) • Master-Slave replication • Standalone using REST API Tuesday, April 17, 12 39
  • 40. Neo4j Data Model Tuesday, April 17, 12 40
  • 41. Features • Support for property graphs (nodes connected via relationships, each with arbitrary properties) • Transparent mapping of annotated POJO entities (using AspectJ • Neo4jTemplate with convenient API, exception translation and optional transaction management • Supports the Cypher and Gremlin query languages • Dynamic type projections (duck typing) • Spring Data Repositories Support • Cross-store support for partial JPA – Graph Entities Tuesday, April 17, 12 41
  • 42. Cross-store • JPA data and “NOSQL” data can share a data model • Separate the persistence provider by using annotations – could be the entire Entity – or, some of the fields of an Entity • We call this cross-store persistence – One transaction manager to coordinate the “NOSQL” store with the JPA relational database – AspectJ support to manage the “NOSQL” entities and fields • holds on to changed values in “change sets” until the transaction commits for non-transactional data stores Tuesday, April 17, 12 42
  • 43. A cross-store scenario ... You have a traditional web app using JPA to persist data to a relational database Tuesday, April 17, 12 43
  • 44. JPA Data Model Restaurant UserAccount @Entity @Entity public class Restaurant { @Table(name = "user_account") @Id @GeneratedValue public class UserAccount { private Long id; @Id @GeneratedValue private String name; private Long id; private String city; private String userName; private String state; private String firstName; private String zipCode; private String lastName; @Temporal(TemporalType.TIMESTAMP) private Date birthDate; @ManyToMany(cascade = CascadeType.ALL) private Set<Restaurant> favorites; Tuesday, April 17, 12 44
  • 45. Cross-store Data Model Restaurant UserAccount @Entity @Entity @NodeEntity(partial = true) @Table(name = "user_account") public class Restaurant { @NodeEntity(partial = true) @Id @GeneratedValue public class UserAccount { private Long id; @Id @GeneratedValue private String name; private Long id; private String city; private String userName; private String state; private String firstName; private String zipCode; private String lastName; @Temporal(TemporalType.TIMESTAMP) private Date birthDate; @ManyToMany(cascade = CascadeType.ALL) Recommendation private Set<Restaurant> favorites; @RelationshipEntity @GraphProperty public class Recommendation { String nickname; @StartNode @RelatedTo(type = "friends", private UserAccount user; elementClass = UserAccount.class) @EndNode Set<UserAccount> friends; private Restaurant restaurant; @RelatedToVia(type = "recommends", private int stars; elementClass = Recommendation.class) private String comment; Iterable<Recommendation> recommendations; Tuesday, April 17, 12 45
  • 46. Cross-store persistence with JPA/Neo4j DEMO https://github.com/SpringSource/spring-data-neo4j/tree/ 2.0.1.RELEASE/spring-data-neo4j-examples/myrestaurants-social Tuesday, April 17, 12 46
  • 47. Questions? http://www.sxc.hu/photo/860327 Tuesday, April 17, 12 47