SlideShare a Scribd company logo
Copyright © 2019 Oracle and/or its affiliates.
Copyright © 2019 Oracle and/or its affiliates.
MySQL Connector/J in the
Making of Modern Applications
Connector/J Developer and Team Lead
MySQL Middleware and Clients
September 17, 2019
Filipe Silva
The following is intended to outline our general product direction. It is intended for information purposes
only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code,
or functionality, and should not be relied upon in making purchasing decisions. The development,
release, timing, and pricing of any features or functionality described for Oracle’s products may change
and remains at the sole discretion of Oracle Corporation.
Statements in this presentation relating to Oracle’s future plans, expectations, beliefs, intentions and
prospects are “forward-looking statements” and are subject to material risks and uncertainties. A detailed
discussion of these factors and other risks that affect our business is contained in Oracle’s Securities and
Exchange Commission (SEC) filings, including our most recent reports on Form 10-K and Form 10-Q
under the heading “Risk Factors.” These filings are available on the SEC’s website or on Oracle’s website
at http://www.oracle.com/investor. All information in this presentation is current as of September 2019
and Oracle undertakes no duty to update any statement in light of new information or future events.
Safe Harbor
Copyright © 2019 Oracle and/or its affiliates.
Copyright © 2019 Oracle and/or its affiliates.
Filipe Silva
Connector/J Developer and Team Lead
MySQL Middleware and Clients
September 17, 2019
filipe.silva@oracle.com
What Developers Want?
{“topic”: 1}
{“topic”: 2}
{“topic”: 3}
{“topic”: 4}
MySQL Provides!
Copyright © 2019 Oracle and/or its affiliates.
Modern Applications Architecture
How Data Flows and Looks Like?
JSON, the Binding Glue
NoSQL to the Rescue
{“topic”: 5}
{“topic”: 6}
{“topic”: 7}
{“topic”: 8}
MySQL Document Store
What is the X DevAPI?
Connector/J with X DevAPI
Closing Comments, Q & A
Copyright © 2019 Oracle and/or its affiliates.
What Developers Want?
Developers demand challenging,
interesting projects.
In a quest for learning something new,
developers are more than willing to explore
new tools, technologies or processes.
The ultimate goal is to do more with less.
 Ever-growing demand for containerized microservices
 Extreme scalability, extreme decoupling and extreme agility
 In isolation, a single microservice could be seen almost as an
independent, lightweight, minimalist 3-tier architecture
 Except that they usually are stateless and pipeline-ready
Copyright © 2019 Oracle and/or its affiliates.
Modern Applications Architecture
Copyright © 2019 Oracle and/or its affiliates.
Microservices Architecture
User
Interface
Data Store
Data Store
Data StoreMicroservice
Microservice
Microservice
Microservice
Microservice
Microservice
Copyright © 2019 Oracle and/or its affiliates.
3-Tier Architecture
DataLogicPresentation
User Interface
Web/Application
Servers
Database Servers
UI JavaScript
Frameworks
Workflow
Systems &
Business Logic
Relational and
NoSQL
Databases
How Data Flows?
Client
requests
data or
operation
Server
receives the
client
request
Sever
processes
operation
Server builds
and replies a
response
Client
renders the
response
Data Store
Copyright © 2019 Oracle and/or its affiliates.
JSON
JSON
JSON
JSON
How Data Looks Like?
Copyright © 2019 Oracle and/or its affiliates.
{
"_id": "5bc1195b6f36a842a4f65b24",
"age": 21,
"name": {
"first": "Vicky",
"last": "Leonard"
},
"company": "CodeONE",
"email": "vicky.leonard@codeone.com",
"phone": "+1 (864) 428-3897",
"address": "506 Broadway, Jeff, Ohio, 2249",
"tags": ["java", "development", "database“]
}
From a DBA perspective From a developer perspective
 JSON: “JavaScript Object Notation”
 Name/value pairs, array data and other serializable types
 Cross-platform serialization format
 Widely used in web services and microservices
 Standardized as ECMA-404 and RFC 8259
 Natively supported by MySQL
 Specific data type and multiple functions to operate on JSON values and
columns
Copyright © 2019 Oracle and/or its affiliates.
JSON, the Binding Glue
Copyright © 2019 Oracle and/or its affiliates.
 Many types of NoSQL
systems, little uniformity
among them
 Key-Value Database
Oracle NoSQL Database, Redis, Riak, Memcached
 Document-oriented Database
Apache CouchDB, MongoDB, Couchbase
 Wide Column Store
Bigtable, Apache Cassandra, Apache HBase
 Graph
Neo4j, JanusGraph, AllegroGraph
 NoSQL: “Not Only SQL”
 Developers don’t like SQL
 NoSQL promises to scale and
make applications agile
 “Schema-free”/“Schema-less”
built on distributed systems
 Relaxed ACID principles
NoSQL to the Rescue
Copyright © 2019 Oracle and/or its affiliates.
MySQL Provides!
We hear our customers.
Always evolving, sometimes re-inventing
itself, MySQL keeps pace with the trends
and drives for success of its users.
Either improving existing features or
providing new ones, either increasing its
impressive toolset or stepping into the
Cloud, MySQL provides!
Copyright © 2019 Oracle and/or its affiliates.
Business OwnersOperationsDevelopers
What If…
Schema and schema-less co-existed together in the same technology stack?
Simultaneous
Schema-less and
schema based
development
Rapid prototyping
Document based
model
Transactions
Comprehensive
tooling ecosystem
Performance
management
Robust replication,
backup and restore
Simpler application
schema upgrades
Don’t lose my data
 ACID compliant
Capture all my data
 Extensible, Scalable
Products on
schedule
 Affordable time to
market
 Fast development
Copyright © 2019 Oracle and/or its affiliates.
SQL
NoSQL
NoSQL
+
SQL
=
An easy, straightforward way
of working with JSON
documents in MySQL
Copyright © 2019 Oracle and/or its affiliates.
MySQL Document
Store
 Store unstructured data using a MySQL database
 No explicit schemas and data types
 Data lives in JSON columns…
 That are abstracted away from the user, which only deals with documents
 CRUD operations…
 SQL not mandatory but available
 Consistency and data integrity…
 Fully ACID compliant MySQL RDBMS and InnoDB engine in the background
 Documents and relational tables mixed together
Copyright © 2019 Oracle and/or its affiliates.
MySQL Document Store
Copyright © 2019 Oracle and/or its affiliates.
MySQL Document Store
MySQL Server
X Plugin
Client Application
X DevAPI
(Connector)
MySQL Router
MySQL Protocol X Protocol
X DevAPI
High-level database API to
develop CRUD-based
applications with off-the-shelf
NoSQL document operations.
Available in many popular
programming languages and in
the MySQL Shell. Documents
and relational tables in one
single place.
Provides many expert-level
features, including raw SQL,
transactions, locking, prepared
statements and more.
Empowered by clustered
MySQL setups seamlessly
integrated with InnoDB
clusters and MySQL Router.
Copyright © 2019 Oracle and/or its affiliates.
MySQL Router
MySQL Router is
an integral part of
the InnoDB cluster,
a solution for high
availability that
relies on MySQL
Group Replication.
A standalone,
lightweight,
middleware for
transparent
routing between
applications and
MySQL Servers.
Provides high
availability and
scalability to client
applications and is
available for Linux,
MacOS and
Windows.
Copyright © 2019 Oracle and/or its affiliates.
X Plugin
The MySQL server
plugin that
supports the entire
MySQL Document
Store solution on
the server side.
Document storage
layer on existing
MySQL installations
by enabling the
bridge between the
X DevAPI and the
core MySQL engine.
Implements a
new client-server
protocol – the X
Protocol.
Copyright © 2019 Oracle and/or its affiliates.
X Protocol
A new client-server
protocol based on
Google Protocol
Buffers. Uses
improved data
streaming technics
such as Vectored
I/O or Pipelining.
Simple packets with
structured content. SQL
replaced by CRUD
operation trees with
embedded parameter
binding. Messages can
be pipelined and
wrapped by conditional
expectations.
Security baked-in
with TLS enabled
by default. No
information leaks
out to non-
authenticated
users.
Copyright © 2019 Oracle and/or its affiliates.
Copyright © 2019 Oracle and/or its affiliates.
Mysqlx.Crud.Find {
collection { name: "collection_name", schema: “schema_name" }
data_model: DOCUMENT
criteria {
type: OPERATOR
operator {
name: "=="
param { type: IDENT, identifier { name: "_id" } }
param {
type: LITERAL,
literal { type: V_STRING, v_string: { value: "some_string" } }
} } } }
Protocol Buffer Message Sample
MySQL’s cross-platform,
language agnostic, API for
clients connecting to MySQL
servers as a Document Store
and classic RDBMS
Copyright © 2019 Oracle and/or its affiliates.
What is the
X DevAPI?
Copyright © 2019 Oracle and/or its affiliates.
 Synchronous and Asynchronous
operation executions
 Secure by default (TLS+SHA2)
 Transactions, save points and
row-locking
 Auto-magic prepared
statements
 Connection pooling and fail-
over
 Integrated in the MySQL Shell
 Enables MySQL Document Store
to client applications
 Offers schema-less data storage
development
 Fluent API
 Programming language agnostic
 Intuitive CRUD methods
 Raw SQL also available
 Domain specific expression
language
Key Features
Schema-less Documents
Copyright © 2019 Oracle and/or its affiliates.
 Documents are JSON structures
 Open standard, language-independent file format
 Human-readable text
 Uniquely identified by one attribute – "_id" – that exists in all
documents
Schema-less Collections
Copyright © 2019 Oracle and/or its affiliates.
 Collections are containers of Documents
 The Document structure within a single Collection can vary
significantly
 Technically, a Collection is an InnoDB table
 One regular column of type JSON – "doc"
 One virtual column for document id – "_id", also the table primary key
 Multiple virtual columns that grab data from the JSON structures for
indexing and enforcing constraints
Fluent API
Copyright © 2019 Oracle and/or its affiliates.
 Code flows from a single point of entry – getSession()
 First-class support for IDE hints and auto-completion
 Operations encapsulated in specialized and semantic methods
 Nice scaffolding for repeating tasks
 Smaller SQL injection surface area
 Code becomes more readable, maintainable, and even testable
 Common standard between many popular programming
languages
CRUD Operations
Copyright © 2019 Oracle and/or its affiliates.
 Modern and versatile
CRUD API
 Semantic Document Store
methods
 Manage data without
writing a single line of SQL
JS> users.add({ name: "Filipe" }).add({ name: "Alex" })
Query OK, 2 items affected (0.0373 sec)
JS> users.find()
[
{
"_id": "00005b50ced40000000000000001",
"name": "Filipe"
},
{
"_id": "00005b50ced40000000000000002",
"name": "Alex"
}
]
2 documents in set (0.0009 sec)
JS> users.modify("true").set("team", "connector-java")
Query OK, 2 items affected (0.0751 sec)
JS> users.find('name = "Filipe"')
[
{
"_id": "00005b50ced40000000000000001",
"name": "Filipe",
"team": "connector-java"
}
]
1 document in set (0.0026 sec)
Raw SQL
Copyright © 2019 Oracle and/or its affiliates.
 Suitable for ETL, structured analytics and reporting
 Open door to features not yet in the X DevAPI, such as:
 DDL operations for relational tables
 Managing key constraints
 Joining tables
JS> session.sql('CREATE TABLE data1 (doc JSON)')
JS> session.sql('ALTER TABLE data1 ADD COLUMN name VARCHAR(100) GENERATED ALWAYS AS doc->>"$.name“
VIRTUAL UNIQUE KEY NOT NULL')
JS> session.sql('SELECT DISTINCT t1.name, t2.address FROM data1 t1 JOIN data2 t2 ON t1.name = t2.name
WHERE t2.age > 21').execute()
Method Chaining and Flexible Parameters
Copyright © 2019 Oracle and/or its affiliates.
 Repeated method calls with individual arguments
 A single list of a variable number of arguments
JS> collection.add({ name: "Ann" }).add({ name: "Peter" })
JS> collection.add([{ name: "John" }, { name: "Mark" }])
JS> collection.find('name = :name').bind('name', 'Ann')
JS> collection.find('name = :name').bind({ name: "John" })
JS> collection.find().fields('name', 'age')
JS> collection.find().fields(['name', 'age'])
Domain Specific Expressions Language
Copyright © 2019 Oracle and/or its affiliates.
 Meaningful and human readable
 Resemble closely SQL expressions – subset of it
 Support for most of MySQL operators and functions
 Document paths and table/column reference identifiers
 Named placeholders
 Common between all official connector implementations
 Translated to expression trees in the X Protocol
Secure by Default
Copyright © 2019 Oracle and/or its affiliates.
 TLS is enabled by default in TCP based sessions
 Additional server certificate validation options
 Default options can be overridden on-demand
 SHA-1 and SHA-2 password hashing, used in non-secure
connections
 Support for the main server authentication plugins
 caching_sha2_password
 sha256_password
 mysql_native_password
Transactions and Savepoints
Copyright © 2019 Oracle and/or its affiliates.
 Session-level atomic
operations
 Create, commit or rollback
transactions in the scope of
a session
 Create, release or rollback to
intermediate savepoints in
those transactions
try {
session.startTransaction()
// run some operations (1)
session.createSavepoint("here")
// run more operations (2)
session.releaseSavepoint("here")
session.commit()
} catch (err) {
try {
session.rollbackTo("here") // go to (2)
} catch (err) {
session.rollback() // revert the entire thing
}
}
Row (Document) Locking
Copyright © 2019 Oracle and/or its affiliates.
 Reads determine isolation level in the presence of concurrent
transactions
 Exclusive and Shared locks with two operation modes besides
DEFAULT (active waiting until unlocked):
 NOWAIT, terminates with error
 SKIP_LOCKED, skips locked rows/documents
Auto-magic Prepared Statements
Copyright © 2019 Oracle and/or its affiliates.
 Repeated executions of same statement causes it to be server-
side prepared
 New values are passed via parameter binding
 Changes in statement definition triggers re-prepare on next
execution
 Paging operations (limit() and offset()) only trigger re-prepare the
first time they are set.
Connection Pooling
Copyright © 2019 Oracle and/or its affiliates.
 Out-of-the-box connection pooling support – no external
libraries needed
 Reduce overhead for applications that open many connections
 Pooled connections are automatically managed by a Client
object
 Simplified setup: timeouts and maximum size
MySQL’s flagship connector for
Java programming language
that combines the traditional
JDBC API and the new MySQL
X DevAPI in one single driver
Copyright © 2019 Oracle and/or its affiliates.
Connector/J with
X DevAPI
 MySQL develops and maintains two Connector/J versions –
8.0 and 5.1
 Open source hosted in GitHub
 https://github.com/mysql/mysql-connector-j
 Available from MySQL downloads page and maven Central
Repository
 Connector/J 8.0 is GA and the recommended version
Copyright © 2019 Oracle and/or its affiliates.
MySQL Connector/J Overview
 Available since Aug. 2015
 Type 4 JDBC driver
 Compliant with JDBC 4.2 (Java 8.0)
 Requires Java 8 or above
 Supports MySQL Protocol and X Protocol
 Implements the X DevAPI
 Compatible with MySQL 5.5 and above
Copyright © 2019 Oracle and/or its affiliates.
MySQL Connector/J 8.0
Copyright © 2019 Oracle and/or its affiliates.
X DevAPI Connetion Objects
Package: com.mysql.cj.xdevapi
ClientFactory Static factory to create Client objects with specific pooling and session configurations
Client Creates sessions and manages a pool of Session instances
SessionFactory Static factory to create Session objects with specific session configurations
Session Establishes and maintains X Protocol connections
Copyright © 2019 Oracle and/or its affiliates.
Main Data Objects
Package: com.mysql.cj.xdevapi
Session Entry point for all data operations; Provides means to manage schemas and enables raw SQL
operations
Schema Equivalent to a MySQL database; Manages collections and tables
Collection Document storage and management; It’s a MySQL table with a specific structure
Table Classic MySQL table
Result Result of data manipulation operations (.add(), .insert(), .modify(), …)
DocResult Result of document fetching from collections
DbDoc Single document obtained from a DocResult
RowResult Result of selecting data from tables (via table.select())
SqlResult Result of selecting data from raw SQL (via session.sql())
Row Single row of data from a RowResult or SqlResult
Copyright © 2019 Oracle and/or its affiliates.
JSON Data and Parsing
Package: com.mysql.cj.xdevapi
JsonParser Transforms JSON documents in string form into DbDoc or JsonArray objects
DbDoc Represents a JSON object, i.e., a collection of name/value pairs
JsonArray Represents a JSON ordered list of values
JsonString Represents a JSON string
JsonNumber Represents a JSON number
JsonLiteral Represents one of the three JSON literals: true, false and null
JsonValue Interface that represents a generic JSON value, i.e., any of the above except
JsonParser
Copyright © 2019 Oracle and/or its affiliates.
Show Case
Back-end system from managing
“guests” that book into the historical…
Hotel Alcatraz
the ultimate luxury destination!
Core X DevAPI Features
Copyright © 2019 Oracle and/or its affiliates.
Copyright © 2019 Oracle and/or its affiliates.
 X DevAPI URL format:
mysqlx://<user>:<pass>@<host>:<port>/<schema>?<property=value>&...
Creating a Session
SessionFactory sf = new SessionFactory();
Session session1 = sf.getSession("mysqlx://demousr:demopwd@localhost");
Session session2 = sf.getSession(
"mysqlx://demousr:demopwd@localhost?xdevapi.ssl-mode=DISABLED");
// Your code goes here!
session1.close();
session2.close();
Copyright © 2019 Oracle and/or its affiliates.
 Connection pooling options:
{ pooling : { enabled: <bool>, maxSize: <int>, maxIdleTime: <int>, queueTimeOut: <int> } }
Creating a Pooled Session
ClientFactory cf = new ClientFactory();
Client client = cf.getClient("mysqlx://demousr:demopwd@localhost:33060",
"{ "pooling" : { "enabled": true, "maxSize": 10 } }");
Session session = client.getSession();
// Your code goes here!
session.close();
client.close();
Copyright © 2019 Oracle and/or its affiliates.
Creating and Managing Schemas
session.createSchema("alcatraz", true); // reuse existing
session.createSchema("escape");
List<Schema> schemas = session.getSchemas();
schemas.forEach(System.out::println);
| Schema(alcatraz)
| Schema(escape)
// Schema "escape" was a mistake, let’s drop it
session.dropSchema("escape");
session.getSchemas().forEach(System.out::println);
| Schema(alcatraz)
Copyright © 2019 Oracle and/or its affiliates.
session.createSchema("alcatraz", true); // reuse existing
session.createSchema("escape");
List<Schema> schemas = session.getSchemas();
schemas.forEach(System.out::println);
| Schema(alcatraz)
| Schema(escape)
// Schema "escape" was a mistake, let’s drop it
session.dropSchema("escape");
session.getSchemas().forEach(System.out::println);
| Schema(alcatraz)
Creating and Managing Schemas
SQL> show databases;
+--------------------+
| Database |
+--------------------+
| alcatraz |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.0041 sec)
SQL>
Copyright © 2019 Oracle and/or its affiliates.
Session session =
sf.getSession("mysqlx://demousr:demopwd@localhost/alcatraz");
Schema schema = session.getDefaultSchema();
Collection colR = schema.createCollection("rooms", true);
Collection colI = schema.createCollection("checkins", true);
Collection colO = schema.createCollection("checkouts");
schema.getCollections().forEach(System.out::println);
| Collection(alcatraz.checkins)
| Collection(alcatraz.checkouts)
| Collection(alcatraz.rooms)
Creating and Managing Collections …
Copyright © 2019 Oracle and/or its affiliates.
Creating and Managing Collections …
Session session =
sf.getSession("mysqlx://demousr:demopwd@localhost/alcatraz");
Schema schema = session.getDefaultSchema();
Collection colR = schema.createCollection("rooms", true);
Collection colI = schema.createCollection("checkins", true);
Collection colO = schema.createCollection("checkouts");
schema.getCollections().forEach(System.out::println);
| Collection(alcatraz.checkins)
| Collection(alcatraz.checkouts)
| Collection(alcatraz.rooms)
SQL> use alcatraz
Default schema set to `alcatraz`.
SQL> show tables;
+--------------------+
| Tables_in_alcatraz |
+--------------------+
| checkins |
| checkouts |
| rooms |
+--------------------+
3 rows in set (0.0045 sec)
SQL>
Copyright © 2019 Oracle and/or its affiliates.
schema.getCollections("check%").forEach(System.out::println);
| Collection(alcatraz.checkins)
| Collection(alcatraz.checkouts)
System.out.println(schema.getCollectionAsTable("rooms"));
| Table(alcatraz.rooms)
// Collection "checkouts" was a mistake, let’s drop it
schema.dropCollection("checkouts");
… Creating and Managing Collections
Copyright © 2019 Oracle and/or its affiliates.
schema.getCollections("check%").forEach(System.out::println);
| Collection(alcatraz.checkins)
| Collection(alcatraz.checkouts)
System.out.println(schema.getCollectionAsTable("rooms"));
| Table(alcatraz.rooms)
// Collection "checkouts" was a mistake, let’s drop it
schema.dropCollection("checkouts");
… Creating and Managing Collections
SQL> show tables;
+--------------------+
| Tables_in_alcatraz |
+--------------------+
| checkins |
| rooms |
+--------------------+
2 rows in set (0.0057 sec)
SQL> desc rooms;
+-------+---------------+------+-----+---------+------------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------+------+-----+---------+------------------+
| doc | json | YES | | NULL | |
| _id | varbinary(32) | NO | PRI | NULL | STORED GENERATED |
+-------+---------------+------+-----+---------+------------------+
2 rows in set (0.0053 sec)
SQL>
Copyright © 2019 Oracle and/or its affiliates.
{
"name": "George Kelly Barnes",
"nick": "Machine Gun Kelly",
"inmate": 117,
"crimes":
[ "Robbery", "Murder" ]
},
{
"name": "Robert Franklin Stroud",
"nick": "Birdman",
"inmate": 594,
"crimes":
[ "Murder" ]
}
]
[
{
"name": "Arthur Barker",
"nick": "Doc",
"inmate": 268,
"crimes":
[ "Kidnapping", "Murder" ]
},
{
"name": "Alphonse AL Capone",
"nick": "Scarface",
"inmate": 85,
"crimes":
[ "Mafia", "Tax evasion" ]
},
Sample Data
Copyright © 2019 Oracle and/or its affiliates.
String g1 = "{"name": "Arthur Barker", " +
""nick": "Doc", "inmate": 268}";
DbDoc d1 = JsonParser.parseDoc(g1);
String g2 = "{"name": "AL Capone", "nick": " +
""Scarface", "inmate": 85, "crimes": ["Mafia"]}";
DbDoc d2 = JsonParser.parseDoc(g2);
AddResult res = col.add(d1).add(d2).execute();
res.getGeneratedIds().forEach(System.out::println);
| 00005d7381020000000000000001
| 00005d7381020000000000000002
Adding Documents …
Copyright © 2019 Oracle and/or its affiliates.
String g3 = "{"name": "George Kelly Barnes", " +
""nick": "Machine Gun Kelly", "inmate": 117, " +
""crimes": ["Robbery", "Murder"]}";
String g4 = "{"name": "Robert Franklin Stroud", "nick": " +
""Batman", "inmate": 594, "crimes": ["Murder"]}";
CompletableFuture<AddResult> cfRes = col.add(g3, g4).executeAsync();
res = cfRes.get();
res.getGeneratedIds().forEach(System.out::println);
| 00005d7381020000000000000003
| 00005d7381020000000000000004
… Adding Documents
Copyright © 2019 Oracle and/or its affiliates.
… Adding Documents
String g3 = "{"name": "George Kelly Barnes", " +
""nick": "Machine Gun Kelly", "inmate": 117, " +
""crimes": ["Robbery", "Murder"]}";
String g4 = "{"name": "Robert Franklin Stroud", "nick": " +
""Batman", "inmate": 594, "crimes": ["Murder"]}";
CompletableFuture<AddResult> cfRes = col.add(g3, g4).executeAsync();
res = cfRes.get();
res.getGeneratedIds().forEach(System.out::println);
| 00005d7381020000000000000003
| 00005d7381020000000000000004
SQL> select _id, LEFT(doc, 54) from alcatraz.checkins;
+------------------------------+--------------------------------------------------------+
| _id | LEFT(doc, 54) |
+------------------------------+--------------------------------------------------------+
| 00005d7381020000000000000001 | {"_id": "00005d7381020000000000000001", "name": "Arthu |
| 00005d7381020000000000000002 | {"_id": "00005d7381020000000000000002", "name": "AL Ca |
| 00005d7381020000000000000003 | {"_id": "00005d7381020000000000000003", "name": "Georg |
| 00005d7381020000000000000004 | {"_id": "00005d7381020000000000000004", "name": "Rober |
+------------------------------+--------------------------------------------------------+
4 rows in set (0.0006 sec)
SQL>
Copyright © 2019 Oracle and/or its affiliates.
JsonArray crimes = JsonParser.parseArray(new
StringReader("["Kidnapping", "Murder"]"));
ModifyStatement modSt = col.modify("inmate = :im").bind("im", 268);
Result res = modSt.set("crimes", crimes).execute();
System.out.println(res.getAffectedItemsCount());
| 1
// {"_id":"...01","crimes":["Kidnapping","Murder"],"inmate":268,...}
Modifying Documents …
Copyright © 2019 Oracle and/or its affiliates.
res = modSt.bind("im", 85).arrayAppend("crimes", "Tax Evasion").patch(
"{"name": CONCAT("Alphonse ", $.name), "crimes": $.crimes}")
.execute();
System.out.println(res.getAffectedItemsCount());
| 1
// {"_id":"...2","crimes":["Mafia","Tax Evasion"],"name":"Alphonse AL
Capone,...}
res = col.modify("_id LIKE '%004'").set("nick", "Birdman").execute();
System.out.println(res.getAffectedItemsCount());
| 1
// {"_id":"...4","nick":"Birdman",...}
… Modifying Documents
Copyright © 2019 Oracle and/or its affiliates.
… Modifying Documents
res = modSt.bind("im", 85).arrayAppend("crimes", "Tax Evasion").patch(
"{"name": CONCAT("Alphonse ", $.name), "crimes": $.crimes}")
.execute();
System.out.println(res.getAffectedItemsCount());
| 1
// {"_id":"...2","crimes":["Mafia","Tax Evasion"],"name":"Alphonse AL
Capone,...}
res = col.modify("_id LIKE ‘%004'").set("nick", "Birdman").execute();
System.out.println(res.getAffectedItemsCount());
| 1
// {"_id":"...4","nick":"Birdman",...}
SQL> select doc->'$.name', doc->'$.nick', doc->'$.crimes' from checkins;
+--------------------------+---------------------+--------------------------+
| doc->'$.name' | doc->'$.nick' | doc->'$.crimes' |
+--------------------------+---------------------+--------------------------+
| "Arthur Barker" | "Doc" | ["Kidnapping", "Murder"] |
| "Alphonse AL Capone" | "Scarface" | ["Mafia", "Tax Evasion"] |
| "George Kelly Barnes" | "Machine Gun Kelly" | ["Robbery", "Murder"] |
| "Robert Franklin Stroud" | "Birdman" | ["Murder"] |
+--------------------------+---------------------+--------------------------+
4 rows in set (0.0032 sec)
SQL>
Copyright © 2019 Oracle and/or its affiliates.
// Find all documents:
col.find().execute().forEach(System.out::println);
| {"_id":"...01","name":"Arthur Barker","nick":"Doc",...}
| {"_id":"...02","name":"Alphonse AL Capone","nick":"Scarface",...}
| {"_id":"...03","name":"George Kelly Barnes","nick":"Machine Gun Kelly",...}
| {"_id":"...04","name":"Robert Franklin Stroud","nick":"Birdman",...}
// Find documents with "length(crimes) = 1"
DocResult res = col.find("JSON_LENGTH($.crimes) = :cr").bind("cr", 1)
.execute();
System.out.println(res.count());
| 1
res.forEach(System.out::println);
| {"_id":"...04","crimes":["Murder"],"name":"Robert Franklin Stroud"...}
Fetching Documents …
Copyright © 2019 Oracle and/or its affiliates.
// Using projections:
col.find("inmate > 200").fields("UPPER(nick) AS nk, LEFT(name, 6) AS nm")
.execute().forEach(System.out::println);
| {"nk":"DOC","nm":"Arthur"}
| {"nk":"BIRDMAN","nm":"Robert"}
col.find("inmate > 200").fields(Expression.expr("{'nk': UPPER(nick), " +
"'nm': LEFT(name, 6)}")).execute(); // Alternative to previous
// Find in array values, sort by "inmate":
col.find("'Murder' IN $.crimes").sort("inmate ASC").execute()
.forEach(System.out::println);
| {"_id":"...03","crimes":["Robbery","Murder"],"inmate":117,...}
| {"_id":"...01","crimes":["Kidnapping","Murder"],"inmate":268,...}
| {"_id":"...004","crimes":["Murder"],"inmate":594,...}
… Fetching Documents
Copyright © 2019 Oracle and/or its affiliates.
col.add("{"name": "O. J. Simpson", "nick": "The Juice", " +
""inmate": 32, "crimes": ["Murder"]}").execute();
col.add("{"name": "O. J. Simpson", "nick": "The Juice", " +
""inmate": 32, "crimes": ["Robbery"]}").execute();
col.add("{"name": "O. J. Simpson", "nick": "The Juice", " +
""inmate": 32, "crimes": ["Kidnapping"]}").execute();
Result res = col.removeOne("00005d7381020000000000000004");
System.out.println(res.getAffectedItemsCount());
| 1
res = col.remove("nick LIKE '%Juice%'").execute();
System.out.println(res.getAffectedItemsCount());
| 2
Deleting Documents
Advanced and Other X
DevAPI Features
Copyright © 2019 Oracle and/or its affiliates.
Copyright © 2019 Oracle and/or its affiliates.
Document/Row Locking Reads
Session session1 = sf.getSession("mysqlx://demousr:demopwd@localhost/alcatraz");
Session session2 = sf.getSession("mysqlx://demousr:demopwd@localhost/alcatraz");
Collection col1 = session1.getDefaultSchema().getCollection("checkins");
Collection col2 = session2.getDefaultSchema().getCollection("checkins");
session1.startTransaction();
session2.startTransaction();
col1.find("_id = '00005d7381020000000000000001'").fields("name AS nm, nick AS nk, " +
"inmate AS im").lockShared().execute().forEach(System.out::println);
| {"im":268,"nk":"Doc","nm":"Arthur Barker"}
col2.find().fields("name AS nm, nick AS nk, inmate AS im")
.lockExclusive(LockContention.SKIP_LOCKED).execute().forEach(System.out::println);
| {"im":85,"nk":"Scarface","nm":"Alphonse AL Capone"}
| {"im":117,"nk":"Machine Gun Kelly","nm":"George Kelly Barnes"}
| {"im":594,"nk":"Birdman","nm":"Robert Franklin Stroud"}
Copyright © 2019 Oracle and/or its affiliates.
 Also supports indexing JSON array fields:
{ fields: [ { field: "$.crimes", type: "TEXT(50)", array: true } ] }
Indexes on Documents
Session session = sf.getSession("mysqlx://demousr:demopwd@localhost/alcatraz");
Collection col = session.getDefaultSchema().getCollection("checkins");
col.createIndex("byInmate",
"{"fields": [{"field": "$.inmate", "type": "INTEGER"}]}");
session.close();
Copyright © 2019 Oracle and/or its affiliates.
 Also supports indexing JSON array fields:
{ fields: [ { field: "$.crimes", type: "TEXT(50)", array: true } ] }
Indexes on Documents
Session session = sf.getSession("mysqlx://demousr:demopwd@localhost/alcatraz");
Collection col = session.getDefaultSchema().getCollection("checkins");
col.createIndex("byInmate",
"{"fields": [{"field": "$.inmate", "type": "INTEGER"}]}");
session.close();
SQL> show create table checkins;
+----------+---------------------------------------------------------------------------------+
| Table | Create Table |
+----------+---------------------------------------------------------------------------------+
| checkins | CREATE TABLE `checkins` (
`doc` json DEFAULT NULL,
`_id` varbinary(32) GENERATED ALWAYS AS
(json_unquote(json_extract(`doc`,_utf8mb4'$._id'))) STORED NOT NULL,
`$ix_i_92928B93F64A9D89EA3915487DC534EA3E9DBE62` int(11) GENERATED ALWAYS AS
(json_extract(`doc`,_utf8mb4'$.inmate')) VIRTUAL,
PRIMARY KEY (`_id`),
KEY `byInmate` (`$ix_i_92928B93F64A9D89EA3915487DC534EA3E9DBE62`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+----------+---------------------------------------------------------------------------------+
1 row in set (0.0028 sec)
SQL>
Copyright © 2019 Oracle and/or its affiliates.
Working with Tables …
SQL> create table alcatraz.teams (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, name varchar(100));
Query OK, 0 rows affected (0.0655 sec)
SQL>
Consumer<Row> rowPrinter = r -> System.out.println(r.getInt(0) + " -> " + r.getString(1));
Table table = schema.getTable("teams");
InsertResult res = table.insert("name")
.values("Giants").values("Warriors").values("Glens").execute();
System.out.println(res.getAffectedItemsCount() + "/" + res.getAutoIncrementValue());
| 3/1
table.select().execute().forEach(rowPrinter);
| 1 -> Giants
| 2 -> Warriors
| 3 -> Glens
table.select().orderBy("name").execute().forEach(rowPrinter);
| 1 -> Giants
| 3 -> Glens
| 2 -> Warriors
Copyright © 2019 Oracle and/or its affiliates.
… Working with Tables
table.update().where("name = 'Glens'")
.set("name", Expression.expr("CONCAT(name, ' SC')")).execute();
table.select().execute().forEach(rowPrinter);
| 1 -> Giants
| 2 -> Warriors
| 3 -> Glens SC
table.delete().where("name LIKE 'G%'").execute();
table.select().execute().forEach(rowPrinter);
| 2 -> Warriors
table.delete().execute();
table.select().execute().forEach(rowPrinter);
|
Copyright © 2019 Oracle and/or its affiliates.
Raw SQL
SqlResult res = session.sql("INSERT INTO teams (name) " +
"VALUES ('Deltas'), ('Golden Gate')").execute();
System.out.println(res.getAffectedItemsCount() + "/" + res.getAutoIncrementValue());
| 2/4
res = session.sql("SELECT * FROM teams").execute();
res.forEach(rowPrinter);
| 4 --> Deltas
| 5 --> Golden Gate
res.getColumns().stream().map(Column::getType).forEach(System.out::println);
| INT
| STRING
session.sql("DELETE FROM teams").execute();
session.sql("SELECT * FROM teams").execute().forEach(rowPrinter);
|
session.sql("DROP TABLE teams").execute();
Let’s wrap this up…
Copyright © 2019 Oracle and/or its affiliates.
Closing Comments,
Q & A
 Everyone demands quick, time-to-market, scalable, high
available and extremely distributed software solutions
 There are new standards in place – all flows around JSON
 MySQL evolves to a combined SQL + NoSQL solution
 Developers gain a new, CRUD based API for both relational
and document store development available in most popular
programming languages – The X DevAPI
 Connector/J is the option for JDBC and the X DevAPI in Java
Copyright © 2019 Oracle and/or its affiliates.
Closing Comments
Q & A
https://dev.mysql.com
Copyright © 2019 Oracle and/or its affiliates.
The preceding is intended to outline our general product direction. It is intended for information purposes
only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code,
or functionality, and should not be relied upon in making purchasing decisions. The development,
release, timing, and pricing of any features or functionality described for Oracle’s products may change
and remains at the sole discretion of Oracle Corporation.
Statements in this presentation relating to Oracle’s future plans, expectations, beliefs, intentions and
prospects are “forward-looking statements” and are subject to material risks and uncertainties. A detailed
discussion of these factors and other risks that affect our business is contained in Oracle’s Securities and
Exchange Commission (SEC) filings, including our most recent reports on Form 10-K and Form 10-Q
under the heading “Risk Factors.” These filings are available on the SEC’s website or on Oracle’s website
at http://www.oracle.com/investor. All information in this presentation is current as of September 2019
and Oracle undertakes no duty to update any statement in light of new information or future events.
Safe Harbor
Copyright © 2019 Oracle and/or its affiliates.
Session Survey
Help us make the content
even better. Please complete
the session survey in the
Mobile App.
Copyright © 2019 Oracle and/or its affiliates.
Thank You
Copyright © 2019 Oracle and/or its affiliates.
Connector/J Developer and Team Lead
MySQL Middleware and Clients
September 17, 2019
Filipe Silva

More Related Content

PDF
Modern Application Development for the Enterprise
PDF
Oracle SOA Cloud - Skanska Customer Journey
PDF
Virtualized Oracle Real Application Clusters (RAC) - Containers and VMs for RAC
PDF
(Oracle) DBA Skills to Have, to Obtain and to Nurture
PPTX
OData and the future of business objects universes
PDF
Make Your Application “Oracle RAC Ready” & Test For It
PDF
Why Use an Oracle Database?
PDF
Oracle Real Application Clusters (RAC) 12c Rel. 2 - What's Next?
Modern Application Development for the Enterprise
Oracle SOA Cloud - Skanska Customer Journey
Virtualized Oracle Real Application Clusters (RAC) - Containers and VMs for RAC
(Oracle) DBA Skills to Have, to Obtain and to Nurture
OData and the future of business objects universes
Make Your Application “Oracle RAC Ready” & Test For It
Why Use an Oracle Database?
Oracle Real Application Clusters (RAC) 12c Rel. 2 - What's Next?

What's hot (20)

PDF
A Cloud Journey - Move to the Oracle Cloud
PDF
MOUG17 Keynote: What's New from Oracle Database Development
PDF
Oracle Sharding 18c - Technical Overview
PDF
Oracle Real Application Clusters (RAC) 12c Rel. 2 - Operational Best Practices
PDF
Oracle RAC on Engineered Systems
PDF
"Changing Role of the DBA" Skills to Have, to Obtain & to Nurture - Updated 2...
PDF
(Oracle) DBA and Other Skills Needed in 2020
PDF
Oracle Big Data Jam Session #1 - オラクルのビッグデータ系サーバレス・サービスのポートフォリオ
PDF
First Take - Oracle unveils database 18c - the autonomy begins
PDF
Under the Hood of the Smartest Availability Features in Oracle's Autonomous D...
PPTX
Oracle SQL Developer Data Modeler - Version Control Your Designs
PDF
Oracle MAA (Maximum Availability Architecture) 18c - An Overview
PDF
MAA - Best Practices for the Cloud
PPT
ISWC 2012 - Linked Data Meetup
PDF
HA, Scalability, DR & MAA in Oracle Database 21c - Overview
PPTX
OData External Data Integration Strategies for SaaS
PDF
Harnessing the Power of Optimizer Hints
PDF
Oracle RAC - Roadmap for New Features
PDF
MySQL day Dublin - OCI & Application Development
PDF
All-inclusive insights on Building JavaScript microservices with Node!.pdf
A Cloud Journey - Move to the Oracle Cloud
MOUG17 Keynote: What's New from Oracle Database Development
Oracle Sharding 18c - Technical Overview
Oracle Real Application Clusters (RAC) 12c Rel. 2 - Operational Best Practices
Oracle RAC on Engineered Systems
"Changing Role of the DBA" Skills to Have, to Obtain & to Nurture - Updated 2...
(Oracle) DBA and Other Skills Needed in 2020
Oracle Big Data Jam Session #1 - オラクルのビッグデータ系サーバレス・サービスのポートフォリオ
First Take - Oracle unveils database 18c - the autonomy begins
Under the Hood of the Smartest Availability Features in Oracle's Autonomous D...
Oracle SQL Developer Data Modeler - Version Control Your Designs
Oracle MAA (Maximum Availability Architecture) 18c - An Overview
MAA - Best Practices for the Cloud
ISWC 2012 - Linked Data Meetup
HA, Scalability, DR & MAA in Oracle Database 21c - Overview
OData External Data Integration Strategies for SaaS
Harnessing the Power of Optimizer Hints
Oracle RAC - Roadmap for New Features
MySQL day Dublin - OCI & Application Development
All-inclusive insights on Building JavaScript microservices with Node!.pdf
Ad

Similar to MySQL Connector/J in the Making of Modern Applications (20)

PDF
Connector/J Beyond JDBC: the X DevAPI for Java and MySQL as a Document Store
PDF
MySQL Connector/Node.js and the X DevAPI
PDF
Node.js and the MySQL Document Store
PDF
MySQL Day Paris 2016 - MySQL as a Document Store
PDF
MySQL 8.0, what's new ? - Forum PHP 2018
PDF
MySQL Document Store and Node.JS
PDF
MySQL as a Document Store
PDF
MySQL JSON Document Store - A Document Store with all the benefits of a Trans...
PDF
20171104 hk-py con-mysql-documentstore_v1
PDF
MySQL Document Store for Modern Applications
PDF
MySQL 8.0 - What's New ?
ODP
Doc store
PDF
MySQL Document Store (Oracle Code Warsaw 2018)
PDF
MySQL Document Store - A Document Store with all the benefts of a Transactona...
PDF
MySQL Day Paris 2018 - MySQL JSON Document Store
PDF
MySQL Document Store
PPTX
A Step by Step Introduction to the MySQL Document Store
PDF
Python and MySQL 8.0 Document Store
PDF
MySQL Day Paris 2018 - What’s New in MySQL 8.0 ?
PPTX
Python And The MySQL X DevAPI - PyCaribbean 2019
Connector/J Beyond JDBC: the X DevAPI for Java and MySQL as a Document Store
MySQL Connector/Node.js and the X DevAPI
Node.js and the MySQL Document Store
MySQL Day Paris 2016 - MySQL as a Document Store
MySQL 8.0, what's new ? - Forum PHP 2018
MySQL Document Store and Node.JS
MySQL as a Document Store
MySQL JSON Document Store - A Document Store with all the benefits of a Trans...
20171104 hk-py con-mysql-documentstore_v1
MySQL Document Store for Modern Applications
MySQL 8.0 - What's New ?
Doc store
MySQL Document Store (Oracle Code Warsaw 2018)
MySQL Document Store - A Document Store with all the benefts of a Transactona...
MySQL Day Paris 2018 - MySQL JSON Document Store
MySQL Document Store
A Step by Step Introduction to the MySQL Document Store
Python and MySQL 8.0 Document Store
MySQL Day Paris 2018 - What’s New in MySQL 8.0 ?
Python And The MySQL X DevAPI - PyCaribbean 2019
Ad

Recently uploaded (20)

PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
System and Network Administration Chapter 2
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
top salesforce developer skills in 2025.pdf
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Nekopoi APK 2025 free lastest update
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
ai tools demonstartion for schools and inter college
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PPTX
Online Work Permit System for Fast Permit Processing
Odoo Companies in India – Driving Business Transformation.pdf
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Which alternative to Crystal Reports is best for small or large businesses.pdf
Softaken Excel to vCard Converter Software.pdf
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Odoo POS Development Services by CandidRoot Solutions
System and Network Administration Chapter 2
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
top salesforce developer skills in 2025.pdf
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Nekopoi APK 2025 free lastest update
VVF-Customer-Presentation2025-Ver1.9.pptx
Operating system designcfffgfgggggggvggggggggg
ai tools demonstartion for schools and inter college
How to Migrate SBCGlobal Email to Yahoo Easily
CHAPTER 2 - PM Management and IT Context
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
2025 Textile ERP Trends: SAP, Odoo & Oracle
Online Work Permit System for Fast Permit Processing

MySQL Connector/J in the Making of Modern Applications

  • 1. Copyright © 2019 Oracle and/or its affiliates.
  • 2. Copyright © 2019 Oracle and/or its affiliates. MySQL Connector/J in the Making of Modern Applications Connector/J Developer and Team Lead MySQL Middleware and Clients September 17, 2019 Filipe Silva
  • 3. The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, timing, and pricing of any features or functionality described for Oracle’s products may change and remains at the sole discretion of Oracle Corporation. Statements in this presentation relating to Oracle’s future plans, expectations, beliefs, intentions and prospects are “forward-looking statements” and are subject to material risks and uncertainties. A detailed discussion of these factors and other risks that affect our business is contained in Oracle’s Securities and Exchange Commission (SEC) filings, including our most recent reports on Form 10-K and Form 10-Q under the heading “Risk Factors.” These filings are available on the SEC’s website or on Oracle’s website at http://www.oracle.com/investor. All information in this presentation is current as of September 2019 and Oracle undertakes no duty to update any statement in light of new information or future events. Safe Harbor Copyright © 2019 Oracle and/or its affiliates.
  • 4. Copyright © 2019 Oracle and/or its affiliates. Filipe Silva Connector/J Developer and Team Lead MySQL Middleware and Clients September 17, 2019 filipe.silva@oracle.com
  • 5. What Developers Want? {“topic”: 1} {“topic”: 2} {“topic”: 3} {“topic”: 4} MySQL Provides! Copyright © 2019 Oracle and/or its affiliates. Modern Applications Architecture How Data Flows and Looks Like? JSON, the Binding Glue NoSQL to the Rescue {“topic”: 5} {“topic”: 6} {“topic”: 7} {“topic”: 8} MySQL Document Store What is the X DevAPI? Connector/J with X DevAPI Closing Comments, Q & A
  • 6. Copyright © 2019 Oracle and/or its affiliates. What Developers Want? Developers demand challenging, interesting projects. In a quest for learning something new, developers are more than willing to explore new tools, technologies or processes. The ultimate goal is to do more with less.
  • 7.  Ever-growing demand for containerized microservices  Extreme scalability, extreme decoupling and extreme agility  In isolation, a single microservice could be seen almost as an independent, lightweight, minimalist 3-tier architecture  Except that they usually are stateless and pipeline-ready Copyright © 2019 Oracle and/or its affiliates. Modern Applications Architecture
  • 8. Copyright © 2019 Oracle and/or its affiliates. Microservices Architecture User Interface Data Store Data Store Data StoreMicroservice Microservice Microservice Microservice Microservice Microservice
  • 9. Copyright © 2019 Oracle and/or its affiliates. 3-Tier Architecture DataLogicPresentation User Interface Web/Application Servers Database Servers UI JavaScript Frameworks Workflow Systems & Business Logic Relational and NoSQL Databases
  • 10. How Data Flows? Client requests data or operation Server receives the client request Sever processes operation Server builds and replies a response Client renders the response Data Store Copyright © 2019 Oracle and/or its affiliates. JSON JSON JSON JSON
  • 11. How Data Looks Like? Copyright © 2019 Oracle and/or its affiliates. { "_id": "5bc1195b6f36a842a4f65b24", "age": 21, "name": { "first": "Vicky", "last": "Leonard" }, "company": "CodeONE", "email": "vicky.leonard@codeone.com", "phone": "+1 (864) 428-3897", "address": "506 Broadway, Jeff, Ohio, 2249", "tags": ["java", "development", "database“] } From a DBA perspective From a developer perspective
  • 12.  JSON: “JavaScript Object Notation”  Name/value pairs, array data and other serializable types  Cross-platform serialization format  Widely used in web services and microservices  Standardized as ECMA-404 and RFC 8259  Natively supported by MySQL  Specific data type and multiple functions to operate on JSON values and columns Copyright © 2019 Oracle and/or its affiliates. JSON, the Binding Glue
  • 13. Copyright © 2019 Oracle and/or its affiliates.  Many types of NoSQL systems, little uniformity among them  Key-Value Database Oracle NoSQL Database, Redis, Riak, Memcached  Document-oriented Database Apache CouchDB, MongoDB, Couchbase  Wide Column Store Bigtable, Apache Cassandra, Apache HBase  Graph Neo4j, JanusGraph, AllegroGraph  NoSQL: “Not Only SQL”  Developers don’t like SQL  NoSQL promises to scale and make applications agile  “Schema-free”/“Schema-less” built on distributed systems  Relaxed ACID principles NoSQL to the Rescue
  • 14. Copyright © 2019 Oracle and/or its affiliates. MySQL Provides! We hear our customers. Always evolving, sometimes re-inventing itself, MySQL keeps pace with the trends and drives for success of its users. Either improving existing features or providing new ones, either increasing its impressive toolset or stepping into the Cloud, MySQL provides!
  • 15. Copyright © 2019 Oracle and/or its affiliates. Business OwnersOperationsDevelopers What If… Schema and schema-less co-existed together in the same technology stack? Simultaneous Schema-less and schema based development Rapid prototyping Document based model Transactions Comprehensive tooling ecosystem Performance management Robust replication, backup and restore Simpler application schema upgrades Don’t lose my data  ACID compliant Capture all my data  Extensible, Scalable Products on schedule  Affordable time to market  Fast development
  • 16. Copyright © 2019 Oracle and/or its affiliates. SQL NoSQL NoSQL + SQL =
  • 17. An easy, straightforward way of working with JSON documents in MySQL Copyright © 2019 Oracle and/or its affiliates. MySQL Document Store
  • 18.  Store unstructured data using a MySQL database  No explicit schemas and data types  Data lives in JSON columns…  That are abstracted away from the user, which only deals with documents  CRUD operations…  SQL not mandatory but available  Consistency and data integrity…  Fully ACID compliant MySQL RDBMS and InnoDB engine in the background  Documents and relational tables mixed together Copyright © 2019 Oracle and/or its affiliates. MySQL Document Store
  • 19. Copyright © 2019 Oracle and/or its affiliates. MySQL Document Store MySQL Server X Plugin Client Application X DevAPI (Connector) MySQL Router MySQL Protocol X Protocol
  • 20. X DevAPI High-level database API to develop CRUD-based applications with off-the-shelf NoSQL document operations. Available in many popular programming languages and in the MySQL Shell. Documents and relational tables in one single place. Provides many expert-level features, including raw SQL, transactions, locking, prepared statements and more. Empowered by clustered MySQL setups seamlessly integrated with InnoDB clusters and MySQL Router. Copyright © 2019 Oracle and/or its affiliates.
  • 21. MySQL Router MySQL Router is an integral part of the InnoDB cluster, a solution for high availability that relies on MySQL Group Replication. A standalone, lightweight, middleware for transparent routing between applications and MySQL Servers. Provides high availability and scalability to client applications and is available for Linux, MacOS and Windows. Copyright © 2019 Oracle and/or its affiliates.
  • 22. X Plugin The MySQL server plugin that supports the entire MySQL Document Store solution on the server side. Document storage layer on existing MySQL installations by enabling the bridge between the X DevAPI and the core MySQL engine. Implements a new client-server protocol – the X Protocol. Copyright © 2019 Oracle and/or its affiliates.
  • 23. X Protocol A new client-server protocol based on Google Protocol Buffers. Uses improved data streaming technics such as Vectored I/O or Pipelining. Simple packets with structured content. SQL replaced by CRUD operation trees with embedded parameter binding. Messages can be pipelined and wrapped by conditional expectations. Security baked-in with TLS enabled by default. No information leaks out to non- authenticated users. Copyright © 2019 Oracle and/or its affiliates.
  • 24. Copyright © 2019 Oracle and/or its affiliates. Mysqlx.Crud.Find { collection { name: "collection_name", schema: “schema_name" } data_model: DOCUMENT criteria { type: OPERATOR operator { name: "==" param { type: IDENT, identifier { name: "_id" } } param { type: LITERAL, literal { type: V_STRING, v_string: { value: "some_string" } } } } } } Protocol Buffer Message Sample
  • 25. MySQL’s cross-platform, language agnostic, API for clients connecting to MySQL servers as a Document Store and classic RDBMS Copyright © 2019 Oracle and/or its affiliates. What is the X DevAPI?
  • 26. Copyright © 2019 Oracle and/or its affiliates.  Synchronous and Asynchronous operation executions  Secure by default (TLS+SHA2)  Transactions, save points and row-locking  Auto-magic prepared statements  Connection pooling and fail- over  Integrated in the MySQL Shell  Enables MySQL Document Store to client applications  Offers schema-less data storage development  Fluent API  Programming language agnostic  Intuitive CRUD methods  Raw SQL also available  Domain specific expression language Key Features
  • 27. Schema-less Documents Copyright © 2019 Oracle and/or its affiliates.  Documents are JSON structures  Open standard, language-independent file format  Human-readable text  Uniquely identified by one attribute – "_id" – that exists in all documents
  • 28. Schema-less Collections Copyright © 2019 Oracle and/or its affiliates.  Collections are containers of Documents  The Document structure within a single Collection can vary significantly  Technically, a Collection is an InnoDB table  One regular column of type JSON – "doc"  One virtual column for document id – "_id", also the table primary key  Multiple virtual columns that grab data from the JSON structures for indexing and enforcing constraints
  • 29. Fluent API Copyright © 2019 Oracle and/or its affiliates.  Code flows from a single point of entry – getSession()  First-class support for IDE hints and auto-completion  Operations encapsulated in specialized and semantic methods  Nice scaffolding for repeating tasks  Smaller SQL injection surface area  Code becomes more readable, maintainable, and even testable  Common standard between many popular programming languages
  • 30. CRUD Operations Copyright © 2019 Oracle and/or its affiliates.  Modern and versatile CRUD API  Semantic Document Store methods  Manage data without writing a single line of SQL JS> users.add({ name: "Filipe" }).add({ name: "Alex" }) Query OK, 2 items affected (0.0373 sec) JS> users.find() [ { "_id": "00005b50ced40000000000000001", "name": "Filipe" }, { "_id": "00005b50ced40000000000000002", "name": "Alex" } ] 2 documents in set (0.0009 sec) JS> users.modify("true").set("team", "connector-java") Query OK, 2 items affected (0.0751 sec) JS> users.find('name = "Filipe"') [ { "_id": "00005b50ced40000000000000001", "name": "Filipe", "team": "connector-java" } ] 1 document in set (0.0026 sec)
  • 31. Raw SQL Copyright © 2019 Oracle and/or its affiliates.  Suitable for ETL, structured analytics and reporting  Open door to features not yet in the X DevAPI, such as:  DDL operations for relational tables  Managing key constraints  Joining tables JS> session.sql('CREATE TABLE data1 (doc JSON)') JS> session.sql('ALTER TABLE data1 ADD COLUMN name VARCHAR(100) GENERATED ALWAYS AS doc->>"$.name“ VIRTUAL UNIQUE KEY NOT NULL') JS> session.sql('SELECT DISTINCT t1.name, t2.address FROM data1 t1 JOIN data2 t2 ON t1.name = t2.name WHERE t2.age > 21').execute()
  • 32. Method Chaining and Flexible Parameters Copyright © 2019 Oracle and/or its affiliates.  Repeated method calls with individual arguments  A single list of a variable number of arguments JS> collection.add({ name: "Ann" }).add({ name: "Peter" }) JS> collection.add([{ name: "John" }, { name: "Mark" }]) JS> collection.find('name = :name').bind('name', 'Ann') JS> collection.find('name = :name').bind({ name: "John" }) JS> collection.find().fields('name', 'age') JS> collection.find().fields(['name', 'age'])
  • 33. Domain Specific Expressions Language Copyright © 2019 Oracle and/or its affiliates.  Meaningful and human readable  Resemble closely SQL expressions – subset of it  Support for most of MySQL operators and functions  Document paths and table/column reference identifiers  Named placeholders  Common between all official connector implementations  Translated to expression trees in the X Protocol
  • 34. Secure by Default Copyright © 2019 Oracle and/or its affiliates.  TLS is enabled by default in TCP based sessions  Additional server certificate validation options  Default options can be overridden on-demand  SHA-1 and SHA-2 password hashing, used in non-secure connections  Support for the main server authentication plugins  caching_sha2_password  sha256_password  mysql_native_password
  • 35. Transactions and Savepoints Copyright © 2019 Oracle and/or its affiliates.  Session-level atomic operations  Create, commit or rollback transactions in the scope of a session  Create, release or rollback to intermediate savepoints in those transactions try { session.startTransaction() // run some operations (1) session.createSavepoint("here") // run more operations (2) session.releaseSavepoint("here") session.commit() } catch (err) { try { session.rollbackTo("here") // go to (2) } catch (err) { session.rollback() // revert the entire thing } }
  • 36. Row (Document) Locking Copyright © 2019 Oracle and/or its affiliates.  Reads determine isolation level in the presence of concurrent transactions  Exclusive and Shared locks with two operation modes besides DEFAULT (active waiting until unlocked):  NOWAIT, terminates with error  SKIP_LOCKED, skips locked rows/documents
  • 37. Auto-magic Prepared Statements Copyright © 2019 Oracle and/or its affiliates.  Repeated executions of same statement causes it to be server- side prepared  New values are passed via parameter binding  Changes in statement definition triggers re-prepare on next execution  Paging operations (limit() and offset()) only trigger re-prepare the first time they are set.
  • 38. Connection Pooling Copyright © 2019 Oracle and/or its affiliates.  Out-of-the-box connection pooling support – no external libraries needed  Reduce overhead for applications that open many connections  Pooled connections are automatically managed by a Client object  Simplified setup: timeouts and maximum size
  • 39. MySQL’s flagship connector for Java programming language that combines the traditional JDBC API and the new MySQL X DevAPI in one single driver Copyright © 2019 Oracle and/or its affiliates. Connector/J with X DevAPI
  • 40.  MySQL develops and maintains two Connector/J versions – 8.0 and 5.1  Open source hosted in GitHub  https://github.com/mysql/mysql-connector-j  Available from MySQL downloads page and maven Central Repository  Connector/J 8.0 is GA and the recommended version Copyright © 2019 Oracle and/or its affiliates. MySQL Connector/J Overview
  • 41.  Available since Aug. 2015  Type 4 JDBC driver  Compliant with JDBC 4.2 (Java 8.0)  Requires Java 8 or above  Supports MySQL Protocol and X Protocol  Implements the X DevAPI  Compatible with MySQL 5.5 and above Copyright © 2019 Oracle and/or its affiliates. MySQL Connector/J 8.0
  • 42. Copyright © 2019 Oracle and/or its affiliates. X DevAPI Connetion Objects Package: com.mysql.cj.xdevapi ClientFactory Static factory to create Client objects with specific pooling and session configurations Client Creates sessions and manages a pool of Session instances SessionFactory Static factory to create Session objects with specific session configurations Session Establishes and maintains X Protocol connections
  • 43. Copyright © 2019 Oracle and/or its affiliates. Main Data Objects Package: com.mysql.cj.xdevapi Session Entry point for all data operations; Provides means to manage schemas and enables raw SQL operations Schema Equivalent to a MySQL database; Manages collections and tables Collection Document storage and management; It’s a MySQL table with a specific structure Table Classic MySQL table Result Result of data manipulation operations (.add(), .insert(), .modify(), …) DocResult Result of document fetching from collections DbDoc Single document obtained from a DocResult RowResult Result of selecting data from tables (via table.select()) SqlResult Result of selecting data from raw SQL (via session.sql()) Row Single row of data from a RowResult or SqlResult
  • 44. Copyright © 2019 Oracle and/or its affiliates. JSON Data and Parsing Package: com.mysql.cj.xdevapi JsonParser Transforms JSON documents in string form into DbDoc or JsonArray objects DbDoc Represents a JSON object, i.e., a collection of name/value pairs JsonArray Represents a JSON ordered list of values JsonString Represents a JSON string JsonNumber Represents a JSON number JsonLiteral Represents one of the three JSON literals: true, false and null JsonValue Interface that represents a generic JSON value, i.e., any of the above except JsonParser
  • 45. Copyright © 2019 Oracle and/or its affiliates. Show Case Back-end system from managing “guests” that book into the historical… Hotel Alcatraz the ultimate luxury destination!
  • 46. Core X DevAPI Features Copyright © 2019 Oracle and/or its affiliates.
  • 47. Copyright © 2019 Oracle and/or its affiliates.  X DevAPI URL format: mysqlx://<user>:<pass>@<host>:<port>/<schema>?<property=value>&... Creating a Session SessionFactory sf = new SessionFactory(); Session session1 = sf.getSession("mysqlx://demousr:demopwd@localhost"); Session session2 = sf.getSession( "mysqlx://demousr:demopwd@localhost?xdevapi.ssl-mode=DISABLED"); // Your code goes here! session1.close(); session2.close();
  • 48. Copyright © 2019 Oracle and/or its affiliates.  Connection pooling options: { pooling : { enabled: <bool>, maxSize: <int>, maxIdleTime: <int>, queueTimeOut: <int> } } Creating a Pooled Session ClientFactory cf = new ClientFactory(); Client client = cf.getClient("mysqlx://demousr:demopwd@localhost:33060", "{ "pooling" : { "enabled": true, "maxSize": 10 } }"); Session session = client.getSession(); // Your code goes here! session.close(); client.close();
  • 49. Copyright © 2019 Oracle and/or its affiliates. Creating and Managing Schemas session.createSchema("alcatraz", true); // reuse existing session.createSchema("escape"); List<Schema> schemas = session.getSchemas(); schemas.forEach(System.out::println); | Schema(alcatraz) | Schema(escape) // Schema "escape" was a mistake, let’s drop it session.dropSchema("escape"); session.getSchemas().forEach(System.out::println); | Schema(alcatraz)
  • 50. Copyright © 2019 Oracle and/or its affiliates. session.createSchema("alcatraz", true); // reuse existing session.createSchema("escape"); List<Schema> schemas = session.getSchemas(); schemas.forEach(System.out::println); | Schema(alcatraz) | Schema(escape) // Schema "escape" was a mistake, let’s drop it session.dropSchema("escape"); session.getSchemas().forEach(System.out::println); | Schema(alcatraz) Creating and Managing Schemas SQL> show databases; +--------------------+ | Database | +--------------------+ | alcatraz | | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.0041 sec) SQL>
  • 51. Copyright © 2019 Oracle and/or its affiliates. Session session = sf.getSession("mysqlx://demousr:demopwd@localhost/alcatraz"); Schema schema = session.getDefaultSchema(); Collection colR = schema.createCollection("rooms", true); Collection colI = schema.createCollection("checkins", true); Collection colO = schema.createCollection("checkouts"); schema.getCollections().forEach(System.out::println); | Collection(alcatraz.checkins) | Collection(alcatraz.checkouts) | Collection(alcatraz.rooms) Creating and Managing Collections …
  • 52. Copyright © 2019 Oracle and/or its affiliates. Creating and Managing Collections … Session session = sf.getSession("mysqlx://demousr:demopwd@localhost/alcatraz"); Schema schema = session.getDefaultSchema(); Collection colR = schema.createCollection("rooms", true); Collection colI = schema.createCollection("checkins", true); Collection colO = schema.createCollection("checkouts"); schema.getCollections().forEach(System.out::println); | Collection(alcatraz.checkins) | Collection(alcatraz.checkouts) | Collection(alcatraz.rooms) SQL> use alcatraz Default schema set to `alcatraz`. SQL> show tables; +--------------------+ | Tables_in_alcatraz | +--------------------+ | checkins | | checkouts | | rooms | +--------------------+ 3 rows in set (0.0045 sec) SQL>
  • 53. Copyright © 2019 Oracle and/or its affiliates. schema.getCollections("check%").forEach(System.out::println); | Collection(alcatraz.checkins) | Collection(alcatraz.checkouts) System.out.println(schema.getCollectionAsTable("rooms")); | Table(alcatraz.rooms) // Collection "checkouts" was a mistake, let’s drop it schema.dropCollection("checkouts"); … Creating and Managing Collections
  • 54. Copyright © 2019 Oracle and/or its affiliates. schema.getCollections("check%").forEach(System.out::println); | Collection(alcatraz.checkins) | Collection(alcatraz.checkouts) System.out.println(schema.getCollectionAsTable("rooms")); | Table(alcatraz.rooms) // Collection "checkouts" was a mistake, let’s drop it schema.dropCollection("checkouts"); … Creating and Managing Collections SQL> show tables; +--------------------+ | Tables_in_alcatraz | +--------------------+ | checkins | | rooms | +--------------------+ 2 rows in set (0.0057 sec) SQL> desc rooms; +-------+---------------+------+-----+---------+------------------+ | Field | Type | Null | Key | Default | Extra | +-------+---------------+------+-----+---------+------------------+ | doc | json | YES | | NULL | | | _id | varbinary(32) | NO | PRI | NULL | STORED GENERATED | +-------+---------------+------+-----+---------+------------------+ 2 rows in set (0.0053 sec) SQL>
  • 55. Copyright © 2019 Oracle and/or its affiliates. { "name": "George Kelly Barnes", "nick": "Machine Gun Kelly", "inmate": 117, "crimes": [ "Robbery", "Murder" ] }, { "name": "Robert Franklin Stroud", "nick": "Birdman", "inmate": 594, "crimes": [ "Murder" ] } ] [ { "name": "Arthur Barker", "nick": "Doc", "inmate": 268, "crimes": [ "Kidnapping", "Murder" ] }, { "name": "Alphonse AL Capone", "nick": "Scarface", "inmate": 85, "crimes": [ "Mafia", "Tax evasion" ] }, Sample Data
  • 56. Copyright © 2019 Oracle and/or its affiliates. String g1 = "{"name": "Arthur Barker", " + ""nick": "Doc", "inmate": 268}"; DbDoc d1 = JsonParser.parseDoc(g1); String g2 = "{"name": "AL Capone", "nick": " + ""Scarface", "inmate": 85, "crimes": ["Mafia"]}"; DbDoc d2 = JsonParser.parseDoc(g2); AddResult res = col.add(d1).add(d2).execute(); res.getGeneratedIds().forEach(System.out::println); | 00005d7381020000000000000001 | 00005d7381020000000000000002 Adding Documents …
  • 57. Copyright © 2019 Oracle and/or its affiliates. String g3 = "{"name": "George Kelly Barnes", " + ""nick": "Machine Gun Kelly", "inmate": 117, " + ""crimes": ["Robbery", "Murder"]}"; String g4 = "{"name": "Robert Franklin Stroud", "nick": " + ""Batman", "inmate": 594, "crimes": ["Murder"]}"; CompletableFuture<AddResult> cfRes = col.add(g3, g4).executeAsync(); res = cfRes.get(); res.getGeneratedIds().forEach(System.out::println); | 00005d7381020000000000000003 | 00005d7381020000000000000004 … Adding Documents
  • 58. Copyright © 2019 Oracle and/or its affiliates. … Adding Documents String g3 = "{"name": "George Kelly Barnes", " + ""nick": "Machine Gun Kelly", "inmate": 117, " + ""crimes": ["Robbery", "Murder"]}"; String g4 = "{"name": "Robert Franklin Stroud", "nick": " + ""Batman", "inmate": 594, "crimes": ["Murder"]}"; CompletableFuture<AddResult> cfRes = col.add(g3, g4).executeAsync(); res = cfRes.get(); res.getGeneratedIds().forEach(System.out::println); | 00005d7381020000000000000003 | 00005d7381020000000000000004 SQL> select _id, LEFT(doc, 54) from alcatraz.checkins; +------------------------------+--------------------------------------------------------+ | _id | LEFT(doc, 54) | +------------------------------+--------------------------------------------------------+ | 00005d7381020000000000000001 | {"_id": "00005d7381020000000000000001", "name": "Arthu | | 00005d7381020000000000000002 | {"_id": "00005d7381020000000000000002", "name": "AL Ca | | 00005d7381020000000000000003 | {"_id": "00005d7381020000000000000003", "name": "Georg | | 00005d7381020000000000000004 | {"_id": "00005d7381020000000000000004", "name": "Rober | +------------------------------+--------------------------------------------------------+ 4 rows in set (0.0006 sec) SQL>
  • 59. Copyright © 2019 Oracle and/or its affiliates. JsonArray crimes = JsonParser.parseArray(new StringReader("["Kidnapping", "Murder"]")); ModifyStatement modSt = col.modify("inmate = :im").bind("im", 268); Result res = modSt.set("crimes", crimes).execute(); System.out.println(res.getAffectedItemsCount()); | 1 // {"_id":"...01","crimes":["Kidnapping","Murder"],"inmate":268,...} Modifying Documents …
  • 60. Copyright © 2019 Oracle and/or its affiliates. res = modSt.bind("im", 85).arrayAppend("crimes", "Tax Evasion").patch( "{"name": CONCAT("Alphonse ", $.name), "crimes": $.crimes}") .execute(); System.out.println(res.getAffectedItemsCount()); | 1 // {"_id":"...2","crimes":["Mafia","Tax Evasion"],"name":"Alphonse AL Capone,...} res = col.modify("_id LIKE '%004'").set("nick", "Birdman").execute(); System.out.println(res.getAffectedItemsCount()); | 1 // {"_id":"...4","nick":"Birdman",...} … Modifying Documents
  • 61. Copyright © 2019 Oracle and/or its affiliates. … Modifying Documents res = modSt.bind("im", 85).arrayAppend("crimes", "Tax Evasion").patch( "{"name": CONCAT("Alphonse ", $.name), "crimes": $.crimes}") .execute(); System.out.println(res.getAffectedItemsCount()); | 1 // {"_id":"...2","crimes":["Mafia","Tax Evasion"],"name":"Alphonse AL Capone,...} res = col.modify("_id LIKE ‘%004'").set("nick", "Birdman").execute(); System.out.println(res.getAffectedItemsCount()); | 1 // {"_id":"...4","nick":"Birdman",...} SQL> select doc->'$.name', doc->'$.nick', doc->'$.crimes' from checkins; +--------------------------+---------------------+--------------------------+ | doc->'$.name' | doc->'$.nick' | doc->'$.crimes' | +--------------------------+---------------------+--------------------------+ | "Arthur Barker" | "Doc" | ["Kidnapping", "Murder"] | | "Alphonse AL Capone" | "Scarface" | ["Mafia", "Tax Evasion"] | | "George Kelly Barnes" | "Machine Gun Kelly" | ["Robbery", "Murder"] | | "Robert Franklin Stroud" | "Birdman" | ["Murder"] | +--------------------------+---------------------+--------------------------+ 4 rows in set (0.0032 sec) SQL>
  • 62. Copyright © 2019 Oracle and/or its affiliates. // Find all documents: col.find().execute().forEach(System.out::println); | {"_id":"...01","name":"Arthur Barker","nick":"Doc",...} | {"_id":"...02","name":"Alphonse AL Capone","nick":"Scarface",...} | {"_id":"...03","name":"George Kelly Barnes","nick":"Machine Gun Kelly",...} | {"_id":"...04","name":"Robert Franklin Stroud","nick":"Birdman",...} // Find documents with "length(crimes) = 1" DocResult res = col.find("JSON_LENGTH($.crimes) = :cr").bind("cr", 1) .execute(); System.out.println(res.count()); | 1 res.forEach(System.out::println); | {"_id":"...04","crimes":["Murder"],"name":"Robert Franklin Stroud"...} Fetching Documents …
  • 63. Copyright © 2019 Oracle and/or its affiliates. // Using projections: col.find("inmate > 200").fields("UPPER(nick) AS nk, LEFT(name, 6) AS nm") .execute().forEach(System.out::println); | {"nk":"DOC","nm":"Arthur"} | {"nk":"BIRDMAN","nm":"Robert"} col.find("inmate > 200").fields(Expression.expr("{'nk': UPPER(nick), " + "'nm': LEFT(name, 6)}")).execute(); // Alternative to previous // Find in array values, sort by "inmate": col.find("'Murder' IN $.crimes").sort("inmate ASC").execute() .forEach(System.out::println); | {"_id":"...03","crimes":["Robbery","Murder"],"inmate":117,...} | {"_id":"...01","crimes":["Kidnapping","Murder"],"inmate":268,...} | {"_id":"...004","crimes":["Murder"],"inmate":594,...} … Fetching Documents
  • 64. Copyright © 2019 Oracle and/or its affiliates. col.add("{"name": "O. J. Simpson", "nick": "The Juice", " + ""inmate": 32, "crimes": ["Murder"]}").execute(); col.add("{"name": "O. J. Simpson", "nick": "The Juice", " + ""inmate": 32, "crimes": ["Robbery"]}").execute(); col.add("{"name": "O. J. Simpson", "nick": "The Juice", " + ""inmate": 32, "crimes": ["Kidnapping"]}").execute(); Result res = col.removeOne("00005d7381020000000000000004"); System.out.println(res.getAffectedItemsCount()); | 1 res = col.remove("nick LIKE '%Juice%'").execute(); System.out.println(res.getAffectedItemsCount()); | 2 Deleting Documents
  • 65. Advanced and Other X DevAPI Features Copyright © 2019 Oracle and/or its affiliates.
  • 66. Copyright © 2019 Oracle and/or its affiliates. Document/Row Locking Reads Session session1 = sf.getSession("mysqlx://demousr:demopwd@localhost/alcatraz"); Session session2 = sf.getSession("mysqlx://demousr:demopwd@localhost/alcatraz"); Collection col1 = session1.getDefaultSchema().getCollection("checkins"); Collection col2 = session2.getDefaultSchema().getCollection("checkins"); session1.startTransaction(); session2.startTransaction(); col1.find("_id = '00005d7381020000000000000001'").fields("name AS nm, nick AS nk, " + "inmate AS im").lockShared().execute().forEach(System.out::println); | {"im":268,"nk":"Doc","nm":"Arthur Barker"} col2.find().fields("name AS nm, nick AS nk, inmate AS im") .lockExclusive(LockContention.SKIP_LOCKED).execute().forEach(System.out::println); | {"im":85,"nk":"Scarface","nm":"Alphonse AL Capone"} | {"im":117,"nk":"Machine Gun Kelly","nm":"George Kelly Barnes"} | {"im":594,"nk":"Birdman","nm":"Robert Franklin Stroud"}
  • 67. Copyright © 2019 Oracle and/or its affiliates.  Also supports indexing JSON array fields: { fields: [ { field: "$.crimes", type: "TEXT(50)", array: true } ] } Indexes on Documents Session session = sf.getSession("mysqlx://demousr:demopwd@localhost/alcatraz"); Collection col = session.getDefaultSchema().getCollection("checkins"); col.createIndex("byInmate", "{"fields": [{"field": "$.inmate", "type": "INTEGER"}]}"); session.close();
  • 68. Copyright © 2019 Oracle and/or its affiliates.  Also supports indexing JSON array fields: { fields: [ { field: "$.crimes", type: "TEXT(50)", array: true } ] } Indexes on Documents Session session = sf.getSession("mysqlx://demousr:demopwd@localhost/alcatraz"); Collection col = session.getDefaultSchema().getCollection("checkins"); col.createIndex("byInmate", "{"fields": [{"field": "$.inmate", "type": "INTEGER"}]}"); session.close(); SQL> show create table checkins; +----------+---------------------------------------------------------------------------------+ | Table | Create Table | +----------+---------------------------------------------------------------------------------+ | checkins | CREATE TABLE `checkins` ( `doc` json DEFAULT NULL, `_id` varbinary(32) GENERATED ALWAYS AS (json_unquote(json_extract(`doc`,_utf8mb4'$._id'))) STORED NOT NULL, `$ix_i_92928B93F64A9D89EA3915487DC534EA3E9DBE62` int(11) GENERATED ALWAYS AS (json_extract(`doc`,_utf8mb4'$.inmate')) VIRTUAL, PRIMARY KEY (`_id`), KEY `byInmate` (`$ix_i_92928B93F64A9D89EA3915487DC534EA3E9DBE62`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci | +----------+---------------------------------------------------------------------------------+ 1 row in set (0.0028 sec) SQL>
  • 69. Copyright © 2019 Oracle and/or its affiliates. Working with Tables … SQL> create table alcatraz.teams (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, name varchar(100)); Query OK, 0 rows affected (0.0655 sec) SQL> Consumer<Row> rowPrinter = r -> System.out.println(r.getInt(0) + " -> " + r.getString(1)); Table table = schema.getTable("teams"); InsertResult res = table.insert("name") .values("Giants").values("Warriors").values("Glens").execute(); System.out.println(res.getAffectedItemsCount() + "/" + res.getAutoIncrementValue()); | 3/1 table.select().execute().forEach(rowPrinter); | 1 -> Giants | 2 -> Warriors | 3 -> Glens table.select().orderBy("name").execute().forEach(rowPrinter); | 1 -> Giants | 3 -> Glens | 2 -> Warriors
  • 70. Copyright © 2019 Oracle and/or its affiliates. … Working with Tables table.update().where("name = 'Glens'") .set("name", Expression.expr("CONCAT(name, ' SC')")).execute(); table.select().execute().forEach(rowPrinter); | 1 -> Giants | 2 -> Warriors | 3 -> Glens SC table.delete().where("name LIKE 'G%'").execute(); table.select().execute().forEach(rowPrinter); | 2 -> Warriors table.delete().execute(); table.select().execute().forEach(rowPrinter); |
  • 71. Copyright © 2019 Oracle and/or its affiliates. Raw SQL SqlResult res = session.sql("INSERT INTO teams (name) " + "VALUES ('Deltas'), ('Golden Gate')").execute(); System.out.println(res.getAffectedItemsCount() + "/" + res.getAutoIncrementValue()); | 2/4 res = session.sql("SELECT * FROM teams").execute(); res.forEach(rowPrinter); | 4 --> Deltas | 5 --> Golden Gate res.getColumns().stream().map(Column::getType).forEach(System.out::println); | INT | STRING session.sql("DELETE FROM teams").execute(); session.sql("SELECT * FROM teams").execute().forEach(rowPrinter); | session.sql("DROP TABLE teams").execute();
  • 72. Let’s wrap this up… Copyright © 2019 Oracle and/or its affiliates. Closing Comments, Q & A
  • 73.  Everyone demands quick, time-to-market, scalable, high available and extremely distributed software solutions  There are new standards in place – all flows around JSON  MySQL evolves to a combined SQL + NoSQL solution  Developers gain a new, CRUD based API for both relational and document store development available in most popular programming languages – The X DevAPI  Connector/J is the option for JDBC and the X DevAPI in Java Copyright © 2019 Oracle and/or its affiliates. Closing Comments
  • 74. Q & A https://dev.mysql.com Copyright © 2019 Oracle and/or its affiliates.
  • 75. The preceding is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, timing, and pricing of any features or functionality described for Oracle’s products may change and remains at the sole discretion of Oracle Corporation. Statements in this presentation relating to Oracle’s future plans, expectations, beliefs, intentions and prospects are “forward-looking statements” and are subject to material risks and uncertainties. A detailed discussion of these factors and other risks that affect our business is contained in Oracle’s Securities and Exchange Commission (SEC) filings, including our most recent reports on Form 10-K and Form 10-Q under the heading “Risk Factors.” These filings are available on the SEC’s website or on Oracle’s website at http://www.oracle.com/investor. All information in this presentation is current as of September 2019 and Oracle undertakes no duty to update any statement in light of new information or future events. Safe Harbor Copyright © 2019 Oracle and/or its affiliates.
  • 76. Session Survey Help us make the content even better. Please complete the session survey in the Mobile App. Copyright © 2019 Oracle and/or its affiliates.
  • 77. Thank You Copyright © 2019 Oracle and/or its affiliates. Connector/J Developer and Team Lead MySQL Middleware and Clients September 17, 2019 Filipe Silva