Spring Data, Jongo & Co. Java Persistenz-Frameworks für MongoDB



Similar documents
MongoDB Developer and Administrator Certification Course Agenda

@tobiastrelle. codecentric AG 1

Getting Started with MongoDB

Document Oriented Database

.NET User Group Bern

MongoDB: document-oriented database

L7_L10. MongoDB. Big Data and Analytics by Seema Acharya and Subhashini Chellappan Copyright 2015, WILEY INDIA PVT. LTD.

Spring Data MongoDB - Reference Documentation

Please ask questions! Have people used non-relational dbs before? MongoDB?

How graph databases started the multi-model revolution

An Empirical Study of NoSQL Databases by Using MongoDB Databases

MongoDB. An introduction and performance analysis. Seminar Thesis

NoSQL in der Cloud Why? Andreas Hartmann

Introduction to NoSQL and MongoDB. Kathleen Durant Lesson 20 CS 3200 Northeastern University

Cloudant Querying Options

NoSQL web apps. w/ MongoDB, Node.js, AngularJS. Dr. Gerd Jungbluth, NoSQL UG Cologne,

ADVANCED DATABASES PROJECT. Juan Manuel Benítez V Gledys Sulbaran

Scaling up = getting a better machine. Scaling out = use another server and add it to your cluster.

NOSQL INTRODUCTION WITH MONGODB AND RUBY GEOFF

NoSQL - What we ve learned with mongodb. Paul Pedersen, Deputy CTO paul@10gen.com DAMA SF December 15, 2011

Building Your First MongoDB Application

In Memory Accelerator for MongoDB

Easy-Cassandra User Guide

Programming Against Hybrid Databases with Java Handling SQL and NoSQL. Brian Hughes IBM

The MongoDB Tutorial Introduction for MySQL Users. Stephane Combaudon April 1st, 2014

NoSQL Database - mongodb

Dr. Chuck Cartledge. 15 Oct. 2015

CSCC09F Programming on the Web. Mongo DB

An Open Source NoSQL solution for Internet Access Logs Analysis

Spring Data JDBC Extensions Reference Documentation

MongoDB in the NoSQL and SQL world. Horst Rechner Berlin,

MONGODB - THE NOSQL DATABASE

Application of NoSQL Database in Web Crawling

Building a RESTful service with MongoDB and Spring MVC

Table of Content. Introduction to ObjectDB and JPA. Object Model: Types, Entities, Primary Keys. Database Connections

DbSchema Tutorial with Introduction in MongoDB

Mongo: some travel note Notes taken by Dario Varotto about MongoDB while doing mongo101p, mongo102 and mongo101js courses by MongoDB university.

Framework Adoption for Java Enterprise Application Development

Performance Evaluation of Java Object Relational Mapping Tools

PERFORMANCE EVALUATION OF JAVA OBJECT-RELATIONAL MAPPING TOOLS HASEEB YOUSAF. (Under the Direction of John A. Miller)

Sharding with postgres_fdw

Java and RDBMS. Married with issues. Database constraints

NoSQL Databases. Nikos Parlavantzas

MongoDB Aggregation and Data Processing Release 3.0.4

Spring Data Cassandra - Reference Documentation

Why NoSQL? Your database options in the new non- relational world IBM Cloudant 1

How SourceForge is Using MongoDB

MongoDB Aggregation and Data Processing

MongoDB. The Definitive Guide to. The NoSQL Database for Cloud and Desktop Computing. Apress8. Eelco Plugge, Peter Membrey and Tim Hawkins

Big data and urban mobility

How To Write A Nosql Database In Spring Data Project

NoSQL: Going Beyond Structured Data and RDBMS

NOSQL DATABASE SYSTEMS

Big Data & Data Science Course Example using MapReduce. Presented by Juan C. Vega

MongoDB. Or how I learned to stop worrying and love the database. Mathias Stearn. N*SQL Berlin October 22th, gen

NORWEGIAN UNIVERSITY OF SCIENCE AND TECHNOLOGY DEPARTMENT OF CHEMICAL ENGINEERING ADVANCED PROCESS SIMULATION. SQL vs. NoSQL

Benchmarking Couchbase Server for Interactive Applications. By Alexey Diomin and Kirill Grigorchuk

MongoDB and Couchbase

Introduction to MongoDB. Kristina Chodorow

SURVEY ON MONGODB: AN OPEN- SOURCE DOCUMENT DATABASE

Lecture Data Warehouse Systems

Big Data. Facebook Wall Data using Graph API. Presented by: Prashant Patel Jaykrushna Patel

Cloud Scale Distributed Data Storage. Jürmo Mehine

1. Introduction What is Slice? Background Why Slice? Purpose of this Document Intended Audience...

Perl & NoSQL Focus on MongoDB. Jean-Marie Gouarné jmgdoc@cpan.org

Can the Elephants Handle the NoSQL Onslaught?

these three NoSQL databases because I wanted to see a the two different sides of the CAP

Hacettepe University Department Of Computer Engineering BBM 471 Database Management Systems Experiment

Overview of Databases On MacOS. Karl Kuehn Automation Engineer RethinkDB

Agile Best Practices and Patterns for Success on an Agile Software development project.

Lag Sucks! R.J. Lorimer Director of Platform Engineering Electrotank, Inc. Robert Greene Vice President of Technology Versant Corporation

Analytics March 2015 White paper. Why NoSQL? Your database options in the new non-relational world

Hibernate Language Binding Guide For The Connection Cloud Using Java Persistence API (JAP)

3 Case Studies of NoSQL and Java Apps in the Real World

On- Prem MongoDB- as- a- Service Powered by the CumuLogic DBaaS Platform

Lecture 21: NoSQL III. Monday, April 20, 2015

Schemaless NoSQL Data Stores Object-NoSQL Mappers to the Rescue?

Weaving Stored Procedures into Java at Zalando

Rapid Application Development. and Application Generation Tools. Walter Knesel

Enterprise Java Web Application Frameworks & Sample Stack Implementation

Chapter 9 Java and SQL. Wang Yang wyang@njnet.edu.cn

Enabling development teams to move fast. PostgreSQL at Zalando

Brad Dayley. NoSQL with MongoDB

Performance Analysis for NoSQL and SQL

EclipseLink. Solutions Guide for EclipseLink Release 2.5

Attacking MongoDB. Firstov Mihail

Scaling with MongoDB. by Michael Schurter Scaling with MongoDB by Michael Schurter - OS Bridge,

Developing modular Java applications

Leveraging the Power of SOLR with SPARK. Johannes Weigend QAware GmbH Germany pache Big Data Europe September 2015

Visualizing MongoDB Objects in Concept and Practice

Comparisons Between MongoDB and MS-SQL Databases on the TWC Website

Comparing SQL and NOSQL databases

A COMPARATIVE STUDY OF NOSQL DATA STORAGE MODELS FOR BIG DATA

Transcription:

Spring Data, Jongo & Co. Java Persistenz-Frameworks für MongoDB Tobias.Trelle@codecentric.de @tobiastrelle codecentric AG 1

Tobias Trelle - Senior IT Consultant @ codecentric AG (Düsseldorf) - Organisator MongoDB Usergruppe Düsseldorf - Autor MongoDB-Buch (dpunkt-verlag) codecentric AG 2

Where have all my tables gone ORM is dead ODM long live codecentric AG 3

MongoDB? NoSQL-Datenbank / Open Source Dokumentenorientiert Hochperformant, horizontal skalierbar (scale-out) Replication & Sharding out-of-the-box Map/Reduce Geospatial Indexes / Queries codecentric AG 4

Grundkonzept MongoDB-Server Server Relationales Pendant Tabelle Zeile Spalte Database Collection Document Field Aber Flexibles Schema - Arrays - Rekursiv codecentric AG 5

Document { } title: "MongoDB" versions: [ ] { major: 2, minor: 6}, { major: 2, minor: 4} codecentric AG 6

JSON vs. BSON { hello: "MongoDB" } \x18\x00\x00\x00 \x02 hello\x00 \x08\x00\x00 x00\x00mongodb\x00 \x00 codecentric AG 7

CRUD = IFUR insert( ) find( ), findone( ) update( ) remove() codecentric AG 8

Java Persistenz mit MongoDB MongoDB Java Driver Spring Data MongoDB Morphia Jongo EclipseLink for MongoDB codecentric AG 9

Java Persistenz mit MongoDB Konnektivität codecentric AG 10

Use Case db.order.find( {"items.quantity":? } ) codecentric AG 11

Mongo Java Driver codecentric AG 12

MongoDB Drivers One wire protocol for all client languages A driver implementation per language Responsibilities: Converting language dependent data structures BSON Generating ObjectId for _id field Overview: http://www.mongodb.org/display/docs/drivers codecentric AG 13

Java Driver One JAR w/o further dependencies: <dependency> <groupid>org.mongodb</ </groupid> <artifactid>mongo-java-driver</artifactid> <version>2.11.3</version </dependency> github: https://github.com/mongodb/mongo-java-driver codecentric AG 14

Java Driver: API Overview codecentric AG 15

Java Driver: Connect to MongoDB import com.mongodb.mongoclient; // Default: localhost:27017 mongo = new MongoClient(); // Sharding: mongos server mongo = new MongoClient("mongos01" "mongos01", 4711); // Replica set mongo = new MongoClient(Arrays. Arrays.asList( new ServerAddress("replicant01" "replicant01", 10001), new ServerAddress("replicant02" "replicant02", 10002), new ServerAddress("replicant03" "replicant03", 10003) )); codecentric AG 16

Java Driver: Database / Collection import com.mongodb.db; import com.mongodb.dbcollection; DB db = mongo.getdb("test"); DBCollection collection = db.getcollection("foo"); codecentric AG 17

Java Driver: Documents import com.mongodb.basicdbobject; import com.mongodb.dbobject; // insert document DBObject doc = new BasicDBObject(); doc.put("date", new Date()); doc.put("i", 42); collection.insert(doc); codecentric AG 18

Java Driver: Queries import com.mongodb.dbcursor; DBCursor cursor; cursor = collection.find(); // all documents // documents w/ {i: 42} cursor = collection.find( new BasicDBObject("i", 42) ); document = cursor.next();... codecentric AG 19

Java Driver: Order Use Case DB db = mongo.getdb("test"); DBCollection collection = db.getcollection("order" "order"); DBObject order; List<DBObject> items = new ArrayList<DBObject>(); DBObject item; // order order = new BasicDBObject(); order.put("date", new Date()); order.put("custinfo", "Tobias Trelle"); order.put("items", items); // items item = new BasicDBObject(); item.put("quantity", 1); item.put("price", 47.11); item.put("desc", "Item #1"); items.add(item); item = new BasicDBObject(); item.put("quantity", 2); item.put("price", 42.0); item.put("desc", "Item #2"); items.add(item); collection.insert(order); codecentric AG 20

Java Driver: Order Use Case DB db = mongo.getdb("test"); DBCollection collection = db.getcollection("order"); DBObject query; DBObject document; DBCursor cursor; query = new BasicDBObject("items.quantity", 2); cursor = collection.find(query); while ( cursor.hasnext() ) { document = cursor.next(); println(document); } codecentric AG 21

Spring Data MongoDB codecentric AG 22

Spring Data MongoDB Fact Sheet Vendor License Documentation Main Features Pivotal / SpringSource Apache License, Version 2.0 http://www.springsource.org/spring-data/mongodb Repository Support Object/Document Mapping Templating codecentric AG 23

Spring Data Common patterns for RDBMS and NoSQL data stores Spring Data CrudRepository PagingAndSortingRepository Spring Data JPA Spring Data MongoDB Spring Data Neo4j Spring Data JpaRepository MongoRepository GraphRepository MongoTemplate Neo4jTemplate Embedded REST JPA Mongo Java Driver JDBC RDBMS MongoDB Neo4j Quelle: http://www.infoq.com/articles/spring-data-intro codecentric AG 24

Spring Data MongoDB Templating Resource abstraction Configure connections to mongod / mongos Collection lifecycle ( create, drop) Map/Reduce / Aggregation node(s) Object Mapping Annotation based: @Document, @Field, @Index etc. Classes are mapped to collections, Java Objects to documents Repository Support Queries are derived from methods signatures Annotated Queries codecentric AG 25

Spring Data MongoDB: Configuration <!-- Connection to MongoDB server --> <mongo:db-factory host="localhost" " port="27017" dbname="test" /> <!-- MongoDB Template --> <bean id="mongotemplate class="org.springframework.data.mongodb.core.mongotemplate"> <constructor-arg name="mongodbfactory" ref="mongodbfactory"/> </bean> <!-- Package w/ automagic repositories --> <mongo:repositories base-package=" "mongodb" /> codecentric AG 26

Spring Data MongoDB: Template Detailed configuration of a MongoDB connection: <mongo:mongo host="${mongo.host}" port="${mongo.port}"> <mongo:options connections-per-host="${mongo.connectionsperhost} threads-allowed-to-block-for-connectionconnection multiplier="${mongo.threadsallowedtoblockforconnectionmultiplier} connect-timeout="${mongo.connecttimeout} max-wait-time="${mongo.maxwaittime} auto-connect-retry="${mongo.autoconnectretry} socket-keep-alive="${mongo.socketkeepalive} socket-timeout="${mongo.sockettimeout} slave-ok="${mongo.slaveok} write-number="1 write-timeout="0 write-fsync="true"/> </mongo:mongo> <mongo:db-factory dbname= "test" mongo-ref="mongo"/> codecentric AG 27

Spring Data MongoDB: Object Mapping public class Order { } @Id private String id; private Date date; @Field("custInfo") private String customerinfo; List<Item> items;... public class Item { private int quantity; private double price; @Field("desc") private String description;... } codecentric AG 28

Spring Data MongoDB: Repository Support public interface OrderRepository extends MongoRepository<Order, String> { List<Order> findbyitemsquantity(int quantity); @Query( value = "{ custinfo:?0 }", fields = "{_id:0, items:1}") List<Order> findonlyitems(string name); } codecentric AG 29

Spring Data MongoDB: Additional Goodies Map/Reduce / Aggregation framework Index Management Support for GridFS Geopspatial indexes / queries Optimistic Locking codecentric AG 30

Morphia codecentric AG 31

Morphia Fact Sheet Vendor License Documentation Main Features MongoDB Inc. Apache License, Version 2.0 https://github.com/mongodb/morphia Object/Document Mapping Custom Query API DAO support codecentric AG 32

Morphia: Object Mapping public class Order { } @Id private ObjectId id; private Date date; @Property("custInfo") private String customerinfo; @Embedded List<Item> items;... public class Item { } private int quantity; private double price; @Property("desc") private String description;... codecentric AG 33

Morphia: Queries public class OrderDao extends BasicDAO<Order, ObjectId> { } List<Order> findbyitemsquantity(int quantity) { return find( createquery().filter("items.quantity", quantity)).aslist(); } List<Order> findbyitemspricegreaterthan(double price) { return find( createquery().field("items.price").greaterthan(price) ).aslist(); } codecentric AG 34

Morphia: Queries public class OrderDao extends BasicDAO<Order, ObjectId> { // Projection List<Item> finditems(string customername) { List<Order> orders = find(createquery(). field("custinfo").equal(customername). retrievedfields(true, "items") ).aslist(); } } // check for null in production code! return orders.get(0).getitems(); codecentric AG 35

Morphia: Custom query syntax why? Morphia Mongo Query = $eq!=, <> $neq >, <, >=,<= $gt, $lt, $gte, $lte in, nin $in, $nin elem $elemmatch. codecentric AG 36

Jongo codecentric AG 37

Jongo Fact Sheet Developer License Documentation Main Features Benoît Guérout, Yves Amsellem Apache License, Version 2.0 http://jongo.org/ Object/Document Mapping Custom Query API codecentric AG 38

Jongo: Object Mapping public class Order { } private ObjectId id; private Date date; @JsonProperty("custInfo") private String customerinfo; List<Item> items; public class Item { private int quantity; private double price; @JsonProperty("desc") private String description; } codecentric AG 39

Jongo: Queries // Java driver API MongoClient mc = new MongoClient(); DB db = mc.getdb("odm_jongo"); // Jongo API entry point Jongo jongo = new Jongo(db); MongoCollection orders = jongo.getcollection("order"); // no DAO needed Iterable<Order> result = orders.find("{\"items.quantity\": ": #}", 2).as(Order.class); // Projection Iterable<X> result = orders.find().fields("{_id:0, date:1, custinfo:1}").as(x.class); codecentric AG 40

EclipseLink NoSQL codecentric AG 41

EclipseLink NoSQL MongoDB Fact Sheet Vendor License Documentation Main Features Eclipse Foundation Eclipse Public License (EPL) http://www.eclipse.org/eclipselink/documentation/2.4/con cepts/nosql.htm JPA API (Subset) JPQL Query Language / Native Queries codecentric AG 42

EclipseLink NoSQL Implements JPA API (subset) JP-QL query are translated to native datastore queries Oracle NoSQL, MongoDB codecentric AG 43

EclipseLink NoSQL MongoDB: Configuration <persistence version="2.0" > <persistence-unit name="mongodb"> <class>hibernate.order</class> <class>hibernate.item</class> <properties> <property name="eclipselink.target-database" value="org.eclipse.persistence.nosql.adapters.mongo.mongoplatform"/> <property name="eclipselink.nosql.connection-spec" value="org.eclipse.persistence.nosql.adapters.mongo.mongoconnectionspec"/> <property name="eclipselink.nosql.property.mongo.port" value="27017"/> <property name="eclipselink.nosql.property.mongo.host eclipselink.nosql.property.mongo.host" value="127.0.0.1"/> <property name="eclipselink.nosql.property.mongo.db" value="odm_eclipselink"/> <property name="eclipselink.logging.level" value="fine"/> </properties> </persistence-unit> </persistence> codecentric AG 44

EclipseLink NoSQL MongoDB : Object Mapping @Entity @NoSql(dataFormat=DataFormatType.MAPPED MAPPED) public class Order { @GeneratedValue @Id @Field(name = "_id") private String id; private Date date; @Column(name = "custinfo") private String customerinfo; @ElementCollection private List<Item> items; codecentric AG 45

EclipseLink NoSQL MongoDB : Object Mapping @Embeddable @NoSql(dataFormat=DataFormatType.MAPPED MAPPED) public class Item { private int quantity; private double price; @Column(name="desc") private String description;... codecentric AG 46

EclipseLink NoSQL MongoDB : Queries // JPQL Order order = em.createquery createquery( "SELECT o FROM Order o JOIN o.items i WHERE i.quantity = 2", Order.class).getSingleResult(); // native Order order = (Order)em.createNativeQuery.createNativeQuery( "db.order.findone({_id: : \"" + id + "\"})", Order.class).getSingleResult(); codecentric AG 47

EclipseLink NoSQL MongoDB Problems w/ nested native queries Uses relational API, TX required(?) codecentric AG 48

More JPA Providers for MongoDB Hibernate OGM Very early beta No support for queries Data Nucleus codecentric AG 49

Hibernate OGM: OgmEntityManager codecentric AG 50

Judge yourself Spring Data MongoDB https://github.com/ttrelle/spring-data-examples Morphia https://github.com/ttrelle/morphia-mongodb-examples Jongo https://github.com/ttrelle/jongo-examples EclipseLink https://github.com/ttrelle/eclipselink-mongodb-examples codecentric AG 51

Summary Java Driver -- Spring Data MongoDB EclipseLink Jongo Morphia ODM Annotations Custom JPA + Custom JPQL: @Named(Native)Query + EntityManager Jackson Custom Queries Nested BasicDBObject s Interface w/ - derived queries - Native queries JSON queries via collection wrapper BasicDAO super class w/ 2 flavours of fluent API codecentric AG 52

Which one should I use? JPA EclipseLink - API mismatch Spring Data MongoDB - Ready for production - Active community Morphia Mature The better driver Jongo JDBC MongoDB Java Driver - Ready for production - Supported by MongoDB Inc. MongoDB codecentric AG 53

MUG Düsseldorf MongoDB User Group Düsseldorf https://www.xing.com/net/mongodb-dus @MongoDUS codecentric AG 54

QUESTIONS? Tobias Trelle codecentric AG Merscheider Str. 1 42699 Solingen tel +49 (0) 212.233628.47 fax +49 (0) 212.233628.79 mail Tobias.Trelle@codecentric.de twitter @tobiastrelle www.codecentric.de blog.codecentric.de/en/author/tobias-trelle www.xing.com/net/mongodb-dus codecentric AG 55