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

NoSQL? Have It Your Way!

NoSQL? Have It Your Way!

Presented at JavaOne 2017

The future of data persistence is polyglot, and this future is now. Although SQL used to be the lingua franca of database access, newer NoSQL data stores each come with their own API and query language.

This session explores the approaches Java developers have for accessing NoSQL stores, from native APIs over lightweight abstractions to object mapping libraries. What do you favor, full flexibility or easy portability and reuse of known persistence APIs? When is an object mapper the right tool for the job, and how does event streaming fit into the picture? And are there any emerging standards?

There is no one-size-fits-all solution, but the presentation will prepare you to ask the right questions and find good answers, depending on your needs.

8e25c0ca4bf25113bd9c0ccc5d118164?s=128

Gunnar Morling

October 03, 2017
Tweet

Transcript

  1. NoSQL? Have It Your Way! Gunnar Morling @gunnarmorling 1

  2. Agenda NoSQL - What's so different? Low-level APIs Object mappers

    Emerging standards Q & A 2
  3. Gunnar Morling Open source software engineer at Red Hat Debezium

    Hibernate Spec Lead for Bean Validation 2.0 Other projects: ModiTect, MapStruct gunnar@hibernate.org @gunnarmorling http://in.relation.to/gunnar-morling/ 3
  4. NoSQL - It's about choices Gains Common Sacrifices New data

    modelling paradigms Horizontal scalability Eventual Consistency Referential integrity Transactions Static schemas (Advanced queries) 4
  5. 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
  6. Driver APIs 6

  7. Driver APIs Java APIs String-based Object-based HTTP Specific interfaces and

    query languages 7
  8. Driver APIs 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
  9. Driver APIs 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
  10. Rather low-level, not portable Developer in charge of overall consistency

    Learning curve Driver APIs Pros and Cons Full power Full flexibility Exact semantics + 10
  11. Object Mappers 11

  12. 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
  13. Store-specific Mappers Coming with the driver or SDK Cassandra Neo4j

    DynamoDBMapper Separate project, e.g. for MongoDB Morphia, MongoJack, Jongo, etc. 13
  14. Store-specific Mappers Example: Cassandra @Table( keyspace = "movies_ks", name =

    "movies", writeConsistency = "QUORUM" ) public class Movie { @PartitionKey private UUID id; private String title; // ... } 14
  15. Store-specific Mappers 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
  16. Store-specific Mappers Pros Support specific features Use the language of

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

    one mapper per DB "Come and go" - 17
  18. Store-agnostic Mappers 18

  19. Store-agnostic Mappers Spring Data Based on JPA Hibernate OGM EclipseLink

    NoSQL Kundera DataNucleus 19
  20. 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
  21. Spring Data 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
  22. JPA-based Mappers Wait, JPA?! Domain model layer is applicable Associations

    Session allows for optimizations Rich eco-system Bean Validation, full-text search etc. Tooling 22
  23. 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
  24. Example: Hibernate OGM 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
  25. 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
  26. Use generic terms Danger of "over-promising" Some functionality can't be

    exposed, e.g. async JPA-based Mappers Pros and Cons (Re-)using one familiar API Enable polyglot persistence Generic query support Can manage associations Often based on established code bases + 26
  27. Standards 27

  28. 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
  29. https://www.javaworld.com/article/2071239/scripting-jvm- languages/the-time-for-nosql-standards-is-now.html 2012 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
  30. Standards NoSQL support in Java EE 9 announced at J1

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

    2016... https://javaee.github.io/javaee-spec/download/JavaEE9.pdf 31
  32. 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
  33. 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
  34. 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
  35. Summary 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
  36. 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
  37. 37