If you can't read please download the document
Upload
corneil-du-plessis
View
403
Download
2
Embed Size (px)
Citation preview
CLIQUE PARA EDITAR O FORMATO DO TEXTO DO TTULO
Clique para editar o formato do texto da estrutura de tpicos2. Nvel da estrutura de tpicos3. Nvel da estrutura de tpicos4. Nvel da estrutura de tpicos5. Nvel da estrutura de tpicos6. Nvel da estrutura de tpicos7. Nvel da estrutura de tpicos
Polyglot persistence with Spring Data
Corneil du Plessis
[email protected]://about.me/corneil@corneil
Quotes
Everything should be made as simple as possible, but not simpler.Albert Einstein
Definitions
Persistence
the action or fact of persisting
the quality or state of being persistent; especially : perseverance
the quality that allows someone to continue doing something or trying to do something even though it is difficult or opposed by other people
the state of occurring or existing beyond the usual, expected, or normal time
Definitions
Polyglotknowing or using several languages
computer program or script written in a valid form of multiple programming languages
Polyglot Sample
#define a /*
#
#define b */
#include
#define main() int main(void)
#define printf printf(
#define true )
#define function
function main()
{
printf "Hello, world!\n"true/* 2> /dev/null | grep -v
true*/;
return 0;
}
#define c /*
main
#*/
What is going on in persistence?
RDBMS doesn't scale to Internet (millions of users and billions of transactions).
Ability to interact with multiple data stores depending on specific use cases
MongoDB or Couchbase for Customer data
RDBMS for financial transactions
Elasticsearch, Solr, Cassandra or HBase for audit trails and analytics and search
Redis and Gemfire for shared state
Neo4J, OrientDB for Graph Data
RDF Stores for Semantic Reasoning
Hadoop for Big Data
Simple JDBC Example
import java.sql.*;public class SimpleDemoJDBC { public Person
getPerson(String userId, Connection conn) throws SQLException
{
Person result = null;
PreparedStatement pstmt = conn.prepareStatement(
"SELECT userId, email, name FROM persons WHERE userId = ?");
pstmt.setString(1, userId);
ResultSet rs = pstmt.executeQuery();
if(rs.next()) {
result = new Person(rs.getString(1), rs.getString(2),
rs.getString(3));
}
rs.close();
pstmt.close();
return result;
} public void updatePerson(Person person, Connection conn) throws
SQLException { Statement stmt = conn.prepareStatement(
"UPDATE persons SET email = ?, name = ? WHERE userId = ?");
stmt.setString(1, person.getEmail());
stmt.setString(2, person.getName());
stmt.setString(3, person.getUserId());
stmt.executeUpdate();
stmt.close(); }}
SQLJ what happened there?
import java.sql.*;public class SimpleDemoSQLJ {
public Person getPerson(String userId) throws SQLException {
Person person = new Person();
#sql { SELECT userId, email, name INTO
:person.userId, :person.email, :person.userId
FROM persons WHERE userId = :userId
};
return person;
}
public void updatePerson(Person person) throws SQLException {
#sql { UPDATE persons SET email = :person.email,
name = :person.name
WHERE userId = :person.userId
};
}
}
Syntax may be incorrect
MongoDB Sample
DB db = mongo.getDB("MyData");
DBCollection persons = db.getCollection("Persons");
public Person findPerson(String userId) {
Iterable documents = persons.find(
new Document("userId", userId));
for(Document doc : documents) {
return new Person(doc.get("userId", String.class),
doc.get("name", String.class),
doc.get("email", String.class));
}
return null;
}
public void updatePerson(Person person) {
Document doc = new Document(userId, person.getUserId());
doc.put(email, person.getEmail());
doc.put(name, person.getName());
Document query = new Document("userId", person.getUserId());
Document update = new Document($set, doc);
persons.update(query, update);
}
What are we talking about?
Improve programmer productivity.
Polyglot persistence using one language.
The language of Spring Data.
Spring for programmer productivity
Spring does not provide ORM
Spring does not provide Database
Spring as inversion of control container
Spring templates for JMS, JDBC, Hibernate, JPA, MongoDB and much more.
Why Spring Data?
Best practice indicates you should have Data Access Components or a Data Service Layer.
How often do you make simple mistakes interacting with EntityManager?
Different API for each store.
It should be easier...
What is Spring Data?
Spring Data is an open source project at http://projects.spring.io/spring-data/ managed by Pivotal Software.
Spring Data relies on Spring Framework.
Spring Data provides a set of common patterns for persisting data using existing libraries.
Spring Data provides a Repository Interface
Spring Data provides finder methods
Spring Data provides support for QueryDSL
Spring Data simplifies persistence
Spring Data enables polyglot persistence
Spring Data sub projects
Commons
JPA
JDBC Extensions
MongoDB
Neo4J
Redis
HBase
Hadoop
Solr
GemFire
REST provider
Community ProjectsOrientDB
RDF
Couchbase
Elasticsearch
Cassandra
DynamoDB
How Spring Data?
Configure Spring Data for specific technology
Define Entity as per relevant library or framework
Declare Repository Interface
Spring Data provides Repository Implementation
Spring Framework injects Repository implementation
Spring Data Samples Configuration
EntityManagerFactory as normal for JPA
OR
@EnableJpaRepositories({"com.springframework.data.demo"})
Spring Data Sample Entity
@Entity
public class User {
@Id
Long id;
String fullName;
String gender;
Date dateOfBirth;
}
Getters and Setters removed for brevity
Spring Data Sample Repository
public interface UserRepository
extends CrudRepository {
}
Crud Repository provides type-safe methods for save, delete, load
Spring Data Sample - Injection
@Service
public class MyDataServiceImpl {
@AutoWired
protected UserRepository userRepo;
public void createUser(User user) {
userRepo.save(user);
}
}
Spring Data Sample Finders
public interface UserRepository
extends CrudRepository {
List findByGender(String genderCode);
List findByDateOfBirthBetween(
Date startDate, Date endDate);
}Crud Repository provides query predicates based on method name
like:findByNameOrderByDateOfBirthDesc
findByNameAndGender
Finder method tokensAnd, Or, Is, Equals, Between, LessThan, LessThanEqual, GreaterThan, GreaterThanEqual, After, Before, IsNull, IsNotNull, NotNull, Like, NotLike, StartingWith, EndingWith, Containing, Not, In, NotIn, True, False, IgnoreCase
Spring Data Query
Specific to technology
Annotated finder method@Query("select * from User u where
u.gender = ?")
List findUsersForGender(String code)
Getters and Setters removed for brevity
Spring Data Sample QueryDSL
@Entity
@QueryEntity
public class User {
@Id
private Long id;
private String fullName;
private String gender;
private Date dateOfBirth;
}
QueryDSL generates QUser for creating type-safe query.
class UserRepository extends
QueryDslPredicateExecutor,
CrudRepository {
}
import static Quser.*;
List users = userRepo.findAll(user.gender.eq('M'));
List users = userRepo.findAll(user.fullName.like("%abc%"));
Spring Data Paging and Sorting
class UserRepository extends
PagingAndSortingRepository {
}
...
Pageble page = new PageRequest(1,20);
Page users = userRepo.findAll(user.gender.eq("M"), page);
Create Pageable in UI and add to method.
Add Pageble to finders
Add Sort to finders
Page can be used to retrieve pages.
Slice can only retrieve next slice.
Spring Data Notes
Shared code for multiple technologies like MongoDB, JPA, Neo4J etc.
Entity Mapping is usually specific to technology.
Common Repository methods can be implemented.
Paging and Sorting support.
Spring Data Demo and Questions
JPA With Hibernate
MongoDB
QueryDSL
Demo code at
https://github.com/corneil/spring-data-demo