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

Test (to be deleted)

Gunnar Morling
March 16, 2018
79

Test (to be deleted)

Gunnar Morling

March 16, 2018
Tweet

Transcript

  1. NoSQL? Have It Your Way! NoSQL? Have It Your Way!

    Gunnar Morling Gunnar Morling @gunnarmorling @gunnarmorling 1
  2. Gunnar Morling Gunnar Morling Open source software engineer at Red

    Hat Debezium Hibernate Spec Lead for Bean Validation 2.0 Other projects: ModiTect, MapStruct [email protected] @gunnarmorling http://in.relation.to/gunnar-morling/ 3
  3. NoSQL - It's about choices NoSQL - It's about choices

    Gains Common Sacrifices New data modelling paradigms Horizontal scalability Eventual Consistency Referential integrity Transactions Static schemas (Advanced queries) 4
  4. NoSQL - Common Taxonomy NoSQL - Common Taxonomy Graph Key/Value

    Column Document Cassandra, HBase Neo4j, OrientDB Infinispan, Redis MongoDB, CouchDB > 225 NoSQL DBs on Some options: nosql-database.org developer.okta.com/blog/2017/09/08/nosql- options-for-java-developers 5
  5. Driver APIs Driver APIs Example: Cassandra Example: Cassandra Cluster c

    = Cluster.builder().addContactPoint( "127.0.0.1 ").build(); Session session = c.connect( "movies_ks" ); session.execute( "CREATE TABLE movies (id uuid PRIMARY KEY, ...)" ); session.execute( "INSERT INTO movies (id, title, year) VALUES (?, ?, ?)", UUIDs.random(), "To catch a thief", 1955 ); ResultSet rs = session.execute( "SELECT * from movies" ); rs.forEach(row ­> { System.out.println( rs.getString( "title" ) ) }); 8
  6. Driver APIs Driver APIs Example: MongoDB Example: MongoDB MongoClient client

    = new MongoClient( "localhost", 27017 ); MongoDatabase db = mongoClient.getDatabase( "movies_db" ); MongoCollection<Document> movies = db.getCollection( "movies" ); Document movie = new Document( "title", "To catch a thief" ) .append( "year", 1955 ); movies.insertOne( movie ); MongoCursor<Document> cursor = collection.find().iterator(); while ( cursor.hasNext() ) { System.out.println( cursor.next().toJson() ); } 9
  7. Rather low-level, not portable Developer in charge of overall consistency

    Learning curve Driver APIs Driver APIs Pros and Cons Pros and Cons Full power Full flexibility Exact semantics + - 10
  8. Object Mappers Object Mappers Let you think in terms of

    domain objects Manifest the schema in your app Deal with type conversions and query result mappings Often provide listener support Can help to follow best practices DB-specific or agnostic 12
  9. Store-specific Mappers Store-specific Mappers Coming with the driver or SDK

    Cassandra Neo4j DynamoDBMapper Separate project, e.g. for MongoDB Morphia, MongoJack, Jongo, etc. 13
  10. Store-specific Mappers Store-specific Mappers Example: Cassandra Example: Cassandra @Table( keyspace

    = "movies_ks", name = "movies", writeConsistency = "QUORUM" ) public class Movie { @PartitionKey private UUID id; private String title; // ... } 14
  11. Store-specific Mappers Store-specific Mappers Example: Cassandra Example: Cassandra MappingManager manager

    = new MappingManager( session ); Mapper<Movie> mapper = manager.mapper( Movie.class ); UUID id = ...; Movie movie = new Movie( id, "To catch a thief", 1955 ); mapper.save( movie ); // ... Movie loaded = mapper.get( id ); 15
  12. Store-specific Mappers Store-specific Mappers Pros Pros Support specific features Use

    the language of their store Just expose supported functionality Async support Perceived as "lightweight" + 16
  13. Store-specific Mappers Store-specific Mappers Cons Cons Tie you to the

    given store Require one mapper per DB "Come and go" - 17
  14. Spring Data Spring Data Supports: GemFire, MongoDB, Redis, Cassandra etc.

    Community-supported: AeroSpike, CouchBase, DynamoDB, Hazelcast, Neo4j, Elasticsearch Templates for basic operations Repositories and derived queries 20
  15. Spring Data Spring Data Example Example public interface MovieRepository extends

    MongoRepository<Movie, String> { Movie findByTitle(String title); List<Movie> findByYear(int year); } @Autowired private MovieRepository repository; repository.save( new Movie( 123, "To catch a thief", 1955 ); for ( Movie customer : repository.findAll() ) { System.out.println( customer ); } System.out.println( repository.findByTitle( ... ) ); 21
  16. JPA-based Mappers JPA-based Mappers Wait, JPA?! Wait, JPA?! Domain model

    layer is applicable Associations Session allows for optimizations Rich eco-system Bean Validation, full-text search etc. Tooling 22
  17. Example: Hibernate OGM Example: Hibernate OGM Support for Infinispan, MongoDB,

    Neo4j Cassandra, Redis, CouchDB, Ignite, EHCache Supports JPA idioms as far as possible Add store-specific options and APIs 23
  18. Example: Hibernate OGM Example: Hibernate OGM Configuration of store-specific options

    Configuration of store-specific options public void configure(Configurable configurable) { configurable.configureOptionsFor( MongoDB.class ) .writeConcern( WriteConcernType.REPLICA_ACKNOWLEDGED ) .readPreference( ReadPreferenceType.NEAREST ) .entity( Animal.class ) .writeConcern( new RequiringReplicaCountOf( 3 ) ) .associationStorage( AssociationStorageType.ASSOCIATION_DOCUMENT ); } 24
  19. Hibernate OGM - Queries Hibernate OGM - Queries Native queries,

    e.g. using Neo4j's Cypher: EntityManager em = ... String query = "MATCH ( n:Poem { name:'Portia', author:'Oscar Wilde' } ) " "RETURN n " + "ORDER BY n.name"; List<Poem> poems = em.createNativeQuery( query, Poem.class ) .getResultList(); JPQL query translation Full-text search 25
  20. Use generic terms Danger of "over-promising" Some functionality can't be

    exposed, e.g. async JPA-based Mappers JPA-based Mappers Pros and Cons Pros and Cons (Re-)using one familiar API Enable polyglot persistence Generic query support Can manage associations Often based on established code bases + - 26
  21. Standards Standards “ The time for NoSQL standards is now.

    NoSQL is the heir apparent to RDBMS, but needs a standard query language and APIs to advance https://www.javaworld.com/article/2071239/scripting-jvm- languages/the-time-for-nosql-standards-is-now.html 28
  22. https://www.javaworld.com/article/2071239/scripting-jvm- languages/the-time-for-nosql-standards-is-now.html 2012 Standards Standards “ The time for NoSQL

    standards is now. NoSQL is the heir apparent to RDBMS, but needs a standard query language and APIs to advance 29
  23. Standards Standards NoSQL support in Java EE 9 announced at

    J1 2016... https://javaee.github.io/javaee-spec/download/JavaEE9.pdf 30
  24. Standards Standards NoSQL support in Java EE 9 announced at

    J1 2016... https://javaee.github.io/javaee-spec/download/JavaEE9.pdf 31
  25. Standards - JNoSQL Standards - JNoSQL Communication API Low-level APIs

    for key-value, column family, document oriented and graph databases "Think JDBC" Mapping API OxM based on top of communication API Integrates with CDI and Bean Validation Async support 32
  26. JNoSQL - Mapping API JNoSQL - Mapping API @Entity("Person") public

    class Person { @Column private long id; @Column private String name; // ... } @Inject private DocumentTemplateAsync template; Person person = ...; template.insert( person, p ­> System.out.println( "Inserted: " + p ) ); DocumentQuery query = select() .from( "Person" ).where( Document.of( "name","Bob" ).build(); template.select( query, p ­> System.out.println( "Found: " + p ) ); 33
  27. JNoSQL - Learning more at J1 JNoSQL - Learning more

    at J1 " JNoSQL: The Definitive Solution for Java and NoSQL Database" Thursday, Oct 05, 1:00 p.m "Choosing a NoSQL API and Database to Avoid Tombstones and Dragons in Game On!" Wednesday, Oct 04, 12:45 p.m 34
  28. Summary Summary Questions to ask Questions to ask Do you

    work with a domain model? Are you unsure on your store yet? Are you using multiple stores, or RDBMS + NoSQL? Will you benefit from derived queries? Are you using a graph database? Would you benefit from RDBMS JSON support? 35
  29. Resources Resources - Hibernate OGM - JNoSql API - NoSQL

    options for Java developers @gunnarmorling hibernate.org/ogm jnosql.org developer.okta.com/blog/2017/09/08/nosql-options-for- java-developers Q & A 36
  30. 37