Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Introducing the R2DBC async Java connector

79 views

Published on

Not too long ago, a reactive variant of the JDBC driver was released, known as Reactive Relational Database Connectivity (R2DBC for short). While R2DBC started as an experiment to enable integration of SQL databases into systems that use reactive programming models, it now specifies a full-fledged service-provider interface that can be used to retrieve data from a target data source.

In this session, we’ll take a look at the new MariaDB R2DBC connector and examine the advantages of fully reactive, non-blocking development with MariaDB. And, of course, we’ll dive in and get a first-hand look at what it’s like to use the new connector with some live coding!

Published in: Data & Analytics
  • Be the first to comment

Introducing the R2DBC async Java connector

  1. 1. Introducing the R2DBC connector Reactive Programming with MariaDB
  2. 2. Who is this guy? Rob Hedgpeth Developer Evangelist robh@mariadb.com @probablyrealrob rhedgpeth
  3. 3. Agenda ● Reactive Programming ○ What is it? ○ Why is it important? ● R2DBC ○ What is it? ○ How can you use it? ● Demo Non-blocking Database Euphoria
  4. 4. Reactive Programming
  5. 5. What is it?
  6. 6. @mariadb reactive programming /rēˈaktiv/ /ˈprōˌgramiNG/ A declarative programming paradigm concerned with data streams and the propagation of change.
  7. 7. Data Streams A B Stream of Data C @mariadb
  8. 8. What is a stream? @mariadb Variables changes in data Starting event initiates stream of data Error disruption in data Completion all data is processed Time
  9. 9. Reactive Streams @mariadb ● An initiative started in 2013 by several companies (e.g. Netflix, Pivotal, Lightbend and many others) ● A specification ○ API types ○ Technology Compatibility Kit (TCK) ● The goal was to provide a standard for asynchronous stream processing with non-blocking back pressure.
  10. 10. Back Pressure @mariadb Data
  11. 11. Back Pressure @mariadb A B Push stream of N data items Request N data items Publisher Subscriber
  12. 12. Back Pressure @mariadb Publisher Subscriber Request (10) Send Request (1) Send Send Request (3)
  13. 13. Reactive Streams API @mariadb Publisher Subscriber Subscription onSubscribe request(n) cancel() onNext* (onError | onComplete)
  14. 14. Reactive Streams API @mariadb public interface Publisher<T> { public void subscribe(Subscriber<? super T> s); } public interface Subscriber<T> { public void onSubscribe(Subscription s); public void onNext(T t); public void onError(Throwable t); public void onComplete(); } public interface Subscription { public void request(long n); public void cancel(); } public interface Processor<T, R> extends Subscriber<T>, Publisher<R> { }
  15. 15. Why is it important?
  16. 16. Reactive Programming @mariadb ● The next frontier in Java (and the JVM) for high-efficiency applications ● Fundamentally non-blocking ○ Often paired with asynchronous behaviors ○ ...but is completely agnostic to sync/async ● The key takeaway - back pressure Async != Reactive
  17. 17. JDBC Limitations @mariadb Reactive App
  18. 18. R2DBC
  19. 19. What is it?
  20. 20. Reactive Relational Database Connectivity R2 DB C
  21. 21. Design principles* @mariadb 1. Be completely non-blocking, all the way to the database 2. Utilize Reactive Streams Types and Patterns 3. Provide a minimal set of operations that are implementation specific 4. Enable “humane” APIs to be built on top of the driver * Ben Hale, Creator of R2DBC
  22. 22. Goals @mariadb ✓ Fit seamlessly into Reactive JVM platforms ✓ Offer vendor-neutral access to standard features ✓ Embrace vendor-specific features ✓ Keep the focus on SQL ✓ Keep it simple ○ Provide a foundation for tools and higher-level API’s ○ Compliance should be unambiguous and easy to identify
  23. 23. Reactive App
  24. 24. Why a service-provider interface (SPI)? @mariadb ● One of JDBC’s biggest failings was that the same API had to serve as both humane API for users as well as an inhumane API for alternative clients like JPA, Jdbi, etc. ○ API that users didn’t like using ○ Driver authors didn’t like implementing ● It also lead to drivers duplicating effort ○ ? binding ○ URL parsing
  25. 25. URL Parsing @mariadb r2dbc:a-driver:pipes://localhost:3306/my_database?locale=en_US scheme driver protocol authority path query
  26. 26. R2DBC SPI Compliance @mariadb io.r2dbc.spi.ConnectionFactory io.r2dbc.spi.ConnectionFactoryMetadata io.r2dbc.spi.ConnectionFactoryProvider io.r2dbc.spi.Result io.r2dbc.spi.Row io.r2dbc.spi.RowMetadata io.r2dbc.spi.Batch Fully Support Partially Support io.r2dbc.spi.Connection io.r2dbc.spi.Statement io.r2dbc.spi.ColumnMetaData https://github.com/r2dbc/r2dbc-spi io.r2dbc.spi.ConnectionFactory io.r2dbc.spi.ConnectionFactoryMetadata io.r2dbc.spi.ConnectionFactoryProvider io.r2dbc.spi.Result io.r2dbc.spi.Row io.r2dbc.spi.RowMetadata io.r2dbc.spi.Batch io.r2dbc.spi.Connection io.r2dbc.spi.Statement io.r2dbc.spi.ColumnMetaData
  27. 27. SPI - Connection Factory @mariadb package io.r2dbc.spi; import org.reactivestreams.Publisher; public interface ConnectionFactory { Publisher<? extends Connection> create(); ConnectionFactoryMetadata getMetadata(); }
  28. 28. SPI - Connection @mariadb package io.r2dbc.spi; import org.reactivestreams.Publisher; public interface Connection { Publisher<Void> beginTransaction(); Publisher<Void> close(); Publisher<Void> commitTransaction(); Batch createBatch(); Statement createStatement(String sql); ConnectionMetadata getMetadata(); ... } Lots of other methods
  29. 29. SPI - Statement @mariadb package io.r2dbc.spi; import org.reactivestreams.Publisher; public interface Statement { Publisher<? extends Result> execute(); Statement add(); Statement bind(int index, Object value); Statement bind(String name, Object value); Statement bindNull(int index, Class<?> type); Statement bindNull(String name, Class<?> type); }
  30. 30. SPI - Result @mariadb package io.r2dbc.spi; import org.reactivestreams.Publisher; import java.util.function.BiFunction; public interface Result { Publisher<Integer> getRowsUpdated(); <T> Publisher<T> map(BiFunction<Row, RowMetadata, ? extends T> mappingFunction); }
  31. 31. SPI - Row @mariadb package io.r2dbc.spi; import java.util.function.BiFunction; public interface Row { @Nullable <T> T get(int index, Class<T> type); @Nullable <T> T get(String name, Class<T> type); ... } A couple default methods
  32. 32. How can you use it?
  33. 33. Establishing a connection @mariadb // Configuration the Connection config = MariadbConnectionConfiguration.builder() .host(props.getProperty("host")) .port(Integer.parseInt(props.getProperty("port"))) .username(props.getProperty("username")) .password(props.getProperty("password")) .build();
  34. 34. Establishing a connection @mariadb // Instantiate Connection Factory connFactory = new MariadbConnectionFactory(config); // Instantiate Connection conn = connFactory.create().block();
  35. 35. SELECT @mariadb Statement select = conn.createStatement("select * from todo.tasks"); Flux<Task> = Flux.from(select.execute()) .flatMap( res -> res.map( (row, metadata) -> { int id = row.get(0, Integer.class); String description = row.get(1, String.class); Boolean completed = row.get(2, Boolean.class); return new Task(id,description,completed); }) );
  36. 36. INSERT (Prepared) @mariadb Statement insert = conn.createStatement("insert into tasks (description) values (?)"); insert.bind(0, task.getDescription()); Mono.from(insert.execute()).subscribe();
  37. 37. But... @mariadb
  38. 38. Demo A hands on look at the new MariaDB R2DBC Connector
  39. 39. @mariadb
  40. 40. Roadmap @mariadb ● Performance ● Fast batch using MariaDB bulk (processing) ● GeoJSON data type ● Pluggable types for MariaDB 10.5 (JSON, INET4, INET6, BOOLEAN, ...)
  41. 41. https://mariadb.com/products/skysql/get-started/ $500 credit to get started @mariadb
  42. 42. developers@mariadb.com @mariadb mariadb-corporation Thank you! https://github.com/mariadb-corporation/mariadb-connector-r2dbc https://github.com/mariadb-corporation/developer-examples Open Source Developer Examples

×