SlideShare uma empresa Scribd logo
1 de 66
Baixar para ler offline
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Connector/J Beyond JDBC:
Filipe Silva
Senior Software Developer
Oracle, MySQL
October 23, 2018
the X DevAPI for Java and MySQL as a Document Store
Presented with
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Safe Harbor Statement
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.
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
• Filipe Silva
– Connector/J Developer and Team Lead,
MySQL Middleware and Clients
– filipe.silva@oracle.com
Who am I?
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Program Agenda
Introduction
MySQL as a Document Store
X DevAPI Overview
The X DevAPI in Connector/J
Q & A
1
2
3
4
5
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Introduction
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
From a DBA perspective From a developer perspective
How Data is Perceived
{
"_id": "5bc1195b6f36a842a4f65b24",
"age": 21,
"name": {
"first": "Vicky",
"last": "Leonard“
},
"company": "MUSAPHICS",
"email": "vicky.leonard@musaphics.io",
"phone": "+1 (864) 428-3897",
"address": "506 Broadway , Jeff, Ohio, 2249",
"tags": ["java", "development", "database"]
}
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
• Developers and companies feel
compelled to explore NoSQL
– RDBMS “do not scale”
– “Schema-less is agile”
– Developers don’t like SQL
• Later finding out about limitations
and drawbacks
– Lack of referential integrity.
– Lack of true ACID support.
• Different solutions flood the market
– Key-Value Data Store
Oracle NoSQL Database, Redis, Memcached
– Document Store
Apache CouchDB, MongoDB, Couchbase
– Wide Column Store
Bigtable, Apache Cassandra, Apache HBase
– Graph Store
Neo4j, JanusGraph
How NoSQL Comes Into Play
NoSQL also stands for “Not Only SQL”
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Developers
Schema-less and/or
schema based
developement
Rapid prototyping
Document based model
Transactions
Operations
Comprehensive tooling
ecosystem
Performance
management
Robust replication,
backup and restore
Simpler application
schema upgrades
Business Owners
Don’t lose my data
 ACID compliant
Capture all my data
 Extensible, Scalable
Products on schedule
 Affordable time to market
 Fast development
What If…
Schema and schema-less co-existed together in the same technology stack?
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
NoSQL + SQL
=
MySQL
SQL
NoSQL
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
MySQL as a Document Store
“An easy, straightforward way of working with JSON documents in MySQL”
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
MySQL Document Store
MySQL Server
X Plugin
Client Application
X DevAPI
(Connector)
MySQL Router
MySQL Protocol X Protocol
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
X DevAPI
• Provides a high-level database API to develop CRUD-based applications
– Off-the-shelf NoSQL document operations
– Available in many popular languages and the brand new MySQL Shell
– Expert-level advanced features, including raw SQL, transactions, locking and more
– Powerful way to query documents and relational tables
• Empowered (optionally) by clustered MySQL setups
– Seamless integration with InnoDB clusters via the MySQL Router
https://dev.mysql.com/doc/x-devapi-userguide/en/
MySQL
Server
X Plugin
Client
Application
X DevAPI
(Connector)
MySQL
Router
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
MySQL Router
• MySQL Router is an integral part of the InnoDB cluster
– Lightweight middleware for transparent routing between client applications and back-
end MySQL Servers
– Standalone component; accepts and routes requests to the appropriate servers
– Available for Linux, MacOS and Windows.
• Provides high availability and scalability to client applicaitions
https://dev.mysql.com/doc/mysql-router/8.0/en/
MySQL
Server
X Plugin
Client
Application
X DevAPI
(Connector)
MySQL
Router
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
X Plugin
• MySQL server plugin that supports the MySQL Document Store
– Enabled by default on MySQL 8.0
– Listens on TCP port 33060 by default.
• Provides full document storage layer on existing MySQL installations
– Accepts X DevAPI client requests and processes them using the core MySQL engine
• Implements a new client-server protocol – the X Protocol
https://dev.mysql.com/doc/refman/8.0/en/x-plugin.html
MySQL
Server
X Plugin
Client
Application
X DevAPI
(Connector)
MySQL
Router
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
X Protocol
• A new protocol based on Google Protocol Buffers
– Implemented in the X Plugin
– Improved data streaming: Vectored I/O; Pipelining
• Simple packets with structured content
– CRUD operation trees
– Pipelined messages can be wrapped by expectations based on conditions
– Embedded parameter binding
• Security baked in
– SSL/TLS by default
– No information leaks out to non-authenticated users
https://dev.mysql.com/doc/internals/en/x-protocol.html
MySQL
Server
X Plugin
Client
Application
X DevAPI
(Connector)
MySQL
Router
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Protocol Buffer Message Sample
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" }
}
}
}
}
}
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
X DevAPI Overview
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
• Enables MySQL as a Document
Store Database
• Offers Schema-less data storage
development
• Fluent API
– Intuitive query building methods
– Raw SQL interface also available
• Domain specific expression
language
• Synchronous and Asynchronous
operation executions
• Programming language agnostic
• Secure by default (SSL/TLS and
SHA256 authentication)
• Data consistency with transactions,
save points and row-locking
• Integrated in the MySQL Shell
– JavaScript and Python scripting
capabilities
Key Features
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Documents
• Documents are represented as JSON structures
– Open standard, language-independent file format
– Human-readable text
– Documents are uniquely identified by one common attribute – “_id”
• JSON: “JavaScript Object Notation”
– Name/value pairs, array data and other serializable types
– Cross-platform serialization format (common for web services)
– Standardized as ECMA-404 (http://json.org) and RFC 8259
(https://tools.ietf.org/html/rfc8259)
– Natively supported by MySQL
• Specific data type and multiple functions to operate on JSON values and columns
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Collections
• Collections store multiple 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 look into the JSON structures for indexing and enforcing
constraints
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Schema-less Data
• Modern and versatile CRUD API
– Semantic Document Store methods
• Manage data without writing a single line of SQL
> users.add({ name: ‘Filipe' }).add({ name: ‘Alex' })
Query OK, 2 items affected (0.0373 sec)
> users.find()
[
{
"_id": "00005b50ced40000000000000001",
"name": “Filipe"
},
{
"_id": "00005b50ced40000000000000002",
"name": “Alex"
}
]
2 documents in set (0.0009 sec)
> users.modify('true').set('team', ‘connector-java')
Query OK, 2 items affected (0.0751 sec)
> users.find('name = “Filipe"')
[
{
"_id": "00005b50ced40000000000000001",
"name": “Filipe",
"team": “connector-java"
}
]
1 document in set (0.0026 sec)
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Fluent API
• Code flows from a single point of entry – getSession()
– First-class support for text-editor (or 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 most popular programming languages
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
CollectionAdd
collection.add({ name: ‘Ann', age: 42 })
.add({ name: ‘Peter', age: 23 })
.execute()
collection.add([
{ name: ‘John', age: 50 },
{ name: ‘Mark', age: 25 }
]).execute()
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
CollectionFind
collection.find('name = :name')
.bind('name', ‘Ann')
.fields('COUNT(age) AS age')
.groupBy('age')
.having('age > 42')
.sort('age DESC')
.limit(10)
.offset(5)
.execute()
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
CollectionModify
collection.modify('name = :name')
.bind('name', ‘Ann')
.set('age', 42)
.sort('name ASC')
.limit(1)
.execute()
collection.modify('name = :name')
.bind('name', ‘Peter')
.patch({ age: 42, active: false })
.sort('name DESC')
.limit(1)
.execute()
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
CollectionRemove
collection.remove('name = :name')
.bind('name', ‘Ann')
.sort('age ASC')
.limit(1)
.execute()
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Method Chaining and Flexible and Parameters
• Repeated method calls with individual arguments
• A single list/array of a variable number of arguments
mysqlx.getSession('root@localhost')
mysqlx.getSession({ user: 'root' })
collection.add({ name: ‘Ann' }).add({ name: ‘Peter' })
collection.add([{ name: ‘John' }, { name: ‘Mark' }])
collection.find('name = :name').bind('name', ‘Ann')
collection.find('name = :name').bind({ name: ‘Ann' })
collection.find().fields(‘name', ‘age')
collection.find().fields([‘name', ‘age'])
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Expression Language
• Subset of SQL expressions
• Meaningful and human-readable
• Common between all official connector implementations
// JavaScript
collection
.find("name = ‘Ann' AND age > 42")
.fields("name", "age")
.groupBy("name", "age")
.sort("name ASC", "age DESC")
.limit(4)
.offset(2)
.execute()
// Java
collection
.find("name = ‘Ann' AND age > 42")
.fields("name", "age")
.groupBy("name", "age")
.sort("name ASC", "age DESC")
.limit(4)
.offset(2)
.execute()
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Secure by Default – SSL Modes
• SSL/TLS enabled by default in TCP based sessions
• Additional server certificate validation options
• Default options can be overridden on-demand
mysqlx.getSession({ ssl: false })
mysqlx.getSession('mysqlx://root@localhost?ssl-mode=DISABLED')
mysqlx.getSession({ ssl: true, sslOptions: { ca: '/path/to/ca.pem' } })
mysqlx.getSession('mysqlx://root@localhost?ssl-ca=(/path/to/ca.pem)')
mysqlx.getSession({ ssl: true, sslOptions: { ca: '/path/to/ca.pem', crl: '/path/to/crl.pem' } })
mysqlx.getSession('mysqlx://root@localhost?ssl-ca=(/path/to/ca.pem)&ssl-crl=(/path/to/crl.pem)')
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Authentication Mechanisms
• SHA-1 and SHA-2 password hashing
• Supports the main server authentication plugins:
– mysql_native_password
– sha256_password
– caching_sha2_password
mysqlx.getSession({ user: ‘user1', auth: 'MYSQL41' })
mysqlx.getSession('mysqlx://root@localhost?auth=MYSQL41')
mysqlx.getSession({ user: ‘user2', auth: 'PLAIN' })
mysqlx.getSession('mysqlx://root@localhost?auth=PLAIN')
mysqlx.getSession({ user: ‘user3', auth: 'SHA256_MEMORY' })
mysqlx.getSession('mysqlx://root@localhost?auth=SHA256_MEMORY')
MySQL41
SHA256_MEMORY
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Transactions and Savepoints
• 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('foo')
// run more operations (2)
session.releaseSavepoint('foo')
session.commit()
} catch (err) {
try {
session.rollbackTo('foo') // go to (2)
} catch (err) {
session.rollback() // revert the entire thing
}
}
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Row Locking
• Reads determine isolation level in the presence of concurrent transactions
• Exclusive and Shared locks with two operation modes besides DEFAULT:
– NOWAIT
– SKIP_LOCKED
collection.find('name = :name').bind('name', ‘Ann').lockExclusive()
collection.find('name = :name').bind('name', ‘Ann').lockExclusive(mysqlx.LockContention.DEFAULT)
collection.find('name = :name').bind('name', ‘Ann').lockExclusive(mysqlx.LockContention.NOWAIT)
collection.find('name = :name').bind('name', ‘Ann').lockExclusive(mysqlx.LockContention.SKIP_LOCKED)
collection.find('name = :name').bind('name', ‘Ann').lockShared()
collection.find('name = :name').bind('name', ‘Ann').lockShared(mysqlx.LockContention.DEFAULT)
collection.find('name = :name').bind('name', ‘Ann').lockShared(mysqlx.LockContention.NOWAIT)
collection.find('name = :name').bind('name', ‘Ann').lockShared(mysqlx.LockContention.SKIP_LOCKED)
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Raw SQL
• 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
– Using JOINs
// create a table
session.sql('CREATE TABLE foo (bar VARCHAR(3))').execute()
// add a unique constraint
session.sql('ALTER TABLE foo ADD COLUMN bar VARCHAR(3) GENERATED ALWAYS AS doc->>"$.bar" VIRTUAL
UNIQUE KEY NOT NULL').execute()
// execute a JOIN query
session.sql('SELECT DISTINCT t1.bar FROM foo t1 JOIN baz t2 ON t1.bar = t2.qux WHERE
t1.qux = t2.quux').execute()
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
The X DevAPI in Connector/J
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Overview, Setup and Sneak Peek
1
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Overview
• MySQL develops and maintains two Connector/J versions (5.1 and 8.0)
• 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 © 2018, Oracle and/or its affiliates. All rights reserved. |
Connector/J 5.1
• Type 4 JDBC driver
• Compliant with JDBC 4.2 (Java 8.0)
• Requires Java 5 or above
• Supports MySQL Protocol
• Compatible with MySQL 5.1 and above
• Available since Apr. 2007
Connector/J 8.0
• 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
• Available since Aug. 2015
Overview
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Connector/J 5.1
Compatible with
[Java 5]
Java 6
Java 7
[Java 8]
Java 9+
Supports
MySQL
5.1
MySQL
5.5
MySQL
5.6
MySQL
5.7
MySQL
8.0
Connector/J 8.0
Compatible with
[Java 8]
Java 9+
Supports
MySQL
5.5
MySQL
5.6
MySQL
5.7 incl.
X Plugin
MySQL
8.0 incl.
X Plugin
Overview
[Java n] – Required for compiling
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Obtaining the MySQL Connector/J Library
• Download from the official MySQL Downloads page:
– https://dev.mysql.com/downloads/connector/j/8.0.html
• Portable library bundle
• Linux packages and Windows installer
• Configure as a maven dependency
– https://search.maven.org/search?q=g:mysql%20AND%20a:mysql-connector-java
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.12</version>
</dependency>
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Project Dependencies
• MySQL Connector/J depends on Google’s protobuf java library for
compiling and running X DevAPI code
• Java applications using the X DevAPI via Connector/J 8.0.13 must have the
following libraries in their Classpath:
– mysql-connector-java-8.0.13.jar
– protobuf-java-3.6.1.jar
• Can be found in the directory lib, inside Connector/J portable bundles
• Installations via Linux packages or Windows installer require manual download:
– https://repo1.maven.org/maven2/com/google/protobuf/protobuf-java/3.6.1/
• Maven enabled projects manage and download all dependencies
automatically
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Side by Side: JDBC
Sneak Peek
X
DevAPI
CRUD
X
DevAPI
Raw
JDBC
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/hr", "demouser", "demopass");
Statement stmt = conn.createStatement();
stmt.execute("CREATE TABLE offices (id INT AUTO_INCREMENT PRIMARY KEY, country VARCHAR(100), "
+ "capacity INT)");
ResultSet rs = conn.getMetaData().getTables(“hr", "%", "%", null);
while (rs.next()) {
System.out.println(rs.getString(1) + " - " + rs.getString(3));
}
// continue...
hr - offices
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Side by Side: JDBC
Sneak Peek
X
DevAPI
CRUD
X
DevAPI
Raw
JDBC
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO offices VALUES (NULL, ?, ?)");
pstmt.setString(1, "USA"); pstmt.setInt(2, 200); pstmt.addBatch();
pstmt.setString(1, "UK"); pstmt.setInt(2, 170); pstmt.addBatch();
pstmt.executeBatch();
pstmt.close();
rs = stmt.executeQuery("SELECT * FROM offices");
while (rs.next()) {
System.out.println(rs.getInt(1) + ": " + rs.getString(2) + " (" + rs.getInt(3) + ")");
}
stmt.execute("DROP TABLE offices");
stmt.close();
conn.close();
1: USA (200)
2: UK (170)
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Side by Side: X DevAPI Raw
Sneak Peek
X
DevAPI
CRUD
X
DevAPI
Raw
JDBC
SessionFactory sf = new SessionFactory();
Session sess = sf.getSession("mysqlx://demouser:demopass@localhost/hr");
sess.sql("CREATE TABLE offices (id INT AUTO_INCREMENT PRIMARY KEY, country VARCHAR(100), "
+ "capacity INT)").execute();
sess.getDefaultSchema().getTables().forEach(System.out::println);
sess.sql("INSERT INTO offices VALUES (NULL, 'USA', 200), (NULL, 'UK', 170)").execute();
sess.sql("SELECT * FROM offices").execute().forEach(r -> System.out.println(r.getInt(0) + ": "
+ r.getString(1) + " (" + r.getInt(2) + ")"));
sess.sql("DROP TABLE offices").execute(); sess.close();
Table(hr.offices)
1: USA (200)
2: UK (170)
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Side by Side: X DevAPI CRUD
Sneak Peek
X
DevAPI
CRUD
X
DevAPI
Raw
JDBC
SessionFactory sf = new SessionFactory();
Session sess = sf.getSession("mysqlx://demouser:demopass@localhost/hr");
sess.sql("CREATE TABLE offices (id INT AUTO_INCREMENT PRIMARY KEY, country VARCHAR(100), "
+ "capacity INT)").execute();
Schema schm = sess.getDefaultSchema(); schm.getTables().forEach(System.out::println);
Table tbl = schm.getTable("offices");
tbl.insert("country", "capacity").values("USA", 200).values("UK", 170).execute();
tbl.select().execute().forEach(r -> System.out.println(r.getInt(0) + ": " + r.getString(1) +
" (" + r.getInt(2) + ")"));
sess.sql("DROP TABLE offices").execute(); sess.close();
Table(hr.offices)
1: USA (200)
2: UK (170)
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
X DevAPI Core Features
2
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Main X DevAPI Objects
Session Entry point for all operations; Establishes server sessions/connections and provides means to
manage schemas; Enables raw SQL operations
Schema Equivalent to a MySQL database; Manages collections and tables
Collection Document storage and management; Equivalent to a MySQL table with a specific structure
Table Plain 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; Represents a JSON collection of name/value pairs
RowResult Result of selecting data from tables (via table.select())
Row Single data row from a RowResult
SqlResult Result of selecting data from raw SQL (via session.sql())
Package: com.mysql.cj.xdevapi
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
JSON Parsing and Processing
JsonParser Transforms JSON documents in string form into DbDoc or JsonArray objects
DbDoc Represents a JSON 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
Package: com.mysql.cj.xdevapi
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Data for Following Code Samples
hr.candidates ::=
[
{
"firstName": "Ann", "age": 21
},
{
"firstName": "Peter", "age": 25
},
{
"firstName": "John", "lastName": "Jones", "age": 52, "languages": ["EN", "SP"]
},
{
"firstName": "Mark", "lastName": "Miller", "age": 47,
"languages": ["EN", "SP", "FR", "PT"]
}
]
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
SessionFactory sf = new SessionFactory();
// schema HR must exist
Session session1 = sf.getSession("mysqlx://demouser:demopass@localhost/hr");
Session session2 = sf.getSession("mysqlx://demouser:demopass@localhost/hr?xdevapi.ssl-mode=DISABLED");
session1.close();
session2.close();
Creating a Session
• Created via SessionFactory.getSession(uri), that takes a URI
– mysqlx://<user>:<pass>@<host>:<port>/<default-schema>?<property=value>&…
– The URI identifies the user, host, default schema and a set of session attributes
– All URI components are optional in Connector/J
• It is a good practice to always close sessions after using them
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
session.createSchema("hr", true);
session.createSchema(“foo");
List<Schema> schemas = session.getSchemas();
schemas.forEach(System.out::println);
// Schema(hr)
// Schema(foo)
session.dropSchema("foo");
session.getSchemas().forEach(System.out::println);
// Schema(hr)
Managing Schemas
• The Session class allows to create, list and drop schemas
• A schema is the same as a MySQL database
• The schema becomes the entry point for working with collections
– Tied to an open session from where transactions can be managed
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| hr |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Managing Collections
Session session = sf.getSession("mysqlx://demouser:demopass@localhost/hr");
Schema schema = session.getDefaultSchema();
Collection colC = schema.createCollection("candidates", true);
Collection colJ = schema.createCollection("jobs", true);
Collection colF = schema.createCollection("foo");
schema.getCollections().forEach(System.out::println);
// Collection(hr.candidates)
// Collection(hr.jobs)
schema.getCollections("jo%").forEach(System.out::println);
// Collection(hr.jobs)
System.out.println(schema.getCollectionAsTable("foo"));
// Table(hr.foo)
schema.dropCollection("foo");
session.close();
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Managing Collections
Session session = sf.getSession("mysqlx://demouser:demopass@localhost/hr");
Schema schema = session.getDefaultSchema();
Collection colC = schema.createCollection("candidates", true);
Collection colJ = schema.createCollection("jobs", true);
Collection colD = schema.createCollection("foo");
schema.getCollections().forEach(System.out::println);
// Collection(hr.candidates)
// Collection(hr.jobs)
schema.getCollections("jo%").forEach(System.out::println);
// Collection(hr.jobs)
System.out.println(schema.getCollectionAsTable("foo"));
// Table(hr.foo)
schema.dropCollection("foo");
session.close();
mysql> show tables from hr;
+--------------+
| Tables_in_hr |
+--------------+
| candidates |
| jobs |
+--------------+
2 rows in set (0.00 sec)
mysql> desc candidates;
+-------+---------------+------+-----+---------+------------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------+------+-----+---------+------------------+
| doc | json | YES | | NULL | |
| _id | varbinary(32) | NO | PRI | NULL | STORED GENERATED |
+-------+---------------+------+-----+---------+------------------+
2 rows in set (0,00 sec)
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Adding Documents
• Several add() methods
with single argument and
varargs
• Multiple add() can be
chained until execute()
or executeAsync()
String candidate1 = "{"firstName": "Ann", "age": 21}";
String candidate2 = "{"firstName": "Peter", "age": 25}";
DbDoc doc1 = JsonParser.parseDoc(candidate1);
DbDoc doc2 = JsonParser.parseDoc(candidate2);
AddResult res = colC.add(doc1).add(doc2).execute();
res.getGeneratedIds().forEach(System.out::println);
// 00005b8c23190000000000000001
// 00005b8c23190000000000000002
CompletableFuture<AddResult> cfRes = colC.add(
"{"firstName": "John", "age": 52}",
"{"firstName": "Mark", "age": 47}")
.executeAsync();
res = cfRes.get();
res.getGeneratedIds().forEach(System.out::println);
// 00005b8c23190000000000000003
// 00005b8c23190000000000000004
mysql> select _id from hr.candidates;
+------------------------------+
| _id |
+------------------------------+
| 00005b8c163e0000000000000001 |
| 00005b8c163e0000000000000002 |
| 00005b8c163e0000000000000003 |
| 00005b8c163e0000000000000004 |
+------------------------------+
4 rows in set (0.00 sec)
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Modifying Documents
Result res = colC.modify("_id = '00005b8c23190000000000000003'")
.set("lastName", "Jones").execute();
System.out.println(res.getAffectedItemsCount());
// 1 --> {"_id":"...","age":52,"firstName":"John","lastName":"Jones"}
ModifyStatement modSt = colC.modify("_id = :id").bind("id", "00005b8c23190000000000000004");
res = modSt.set("lastName", "Muller").execute();
System.out.println(res.getAffectedItemsCount());
// 1 --> {"_id":"...","age":47,"firstName":"Mark","lastName":"Muller"}
res = modSt.patch("{"lastName": "Miller",
"languages": ["EN", "SP",
"FR", "PT"]}").execute();
System.out.println(res.getAffectedItemsCount());
// 1 --> {"_id":"...","firstName":"Mark",
// "languages":["EN","SP","FR","PT"],
// "lastName":"Miller","age":47}
mysql> select _id, doc->'$.firstName' fn, doc-
>'$.lastName' ln from hr.candidates where _id =
'00005b8c23190000000000000004';
+------------------------------+--------+----------+
| _id | fn | ln |
+------------------------------+--------+----------+
| 00005b8c23190000000000000004 | "Mark" | "Miller" |
+------------------------------+--------+----------+
1 row in set (0.00 sec)
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Fetching Documents
• Method chaining allows to construct extensive search operations
• A powerful expression syntax allows to filter results easily
• Most of SQL’s SELECT clauses mapped to methods in this fluent pattern
colC.find().execute().fetchAll().forEach(System.out::println);
// {"_id":"...","age":21,"firstName":"Ann"}
// {"_id":"...","age":25,"firstName":"Peter"}
// {"_id":"...","age":52,"firstName":"John","lastName":"Jones"}
// {"_id":"...","age":47,"firstName":"Mark","languages":["EN","SP","FR","PT"],"lastName":"Miller"}
DocResult docRes = colC.find("LENGTH($.firstName) = :lenfn").bind("lenfn", 4).execute();
System.out.println(docRes.count());
docRes.forEach(System.out::println);
// 2
// {"_id":"...","age":52,"firstName":"John","lastName":"Jones"}
// {"_id":"...","age":47,"firstName":"Mark","languages":["EN","SP","FR","PT"],"lastName":"Miller"}
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Fetching Documents(cont.)
• Customizable projections with access to MySQL functions and operators
• Capability to search deep in the documents structure and arrays
colC.find("age >= 25").fields("_id AS id, UPPER(firstName) AS fn, age AS age")
.sort("age DESC").execute().forEach(System.out::println);
// {"age":52,"fn":"JOHN","id":"00005b8c23190000000000000003"}
// {"age":47,"fn":"MARK","id":"00005b8c23190000000000000004"}
// {"age":25,"fn":"PETER","id":"00005b8c23190000000000000002"}
colC.find("age >= 25").fields(Expression.expr("{'id': _id, 'fn': UPPER(firstName), 'age': age}"))
.sort("age DESC").execute().forEach(System.out::println);
// {"age":52,"fn":"JOHN","id":"00005b8c23190000000000000003"}
// {"age":47,"fn":"MARK","id":"00005b8c23190000000000000004"}
// {"age":25,"fn":"PETER","id":"00005b8c23190000000000000002"}
colC.find("'PT' IN languages").fields("_id AS id", "firstName AS fn").execute()
.forEach(System.out::println);
// {"fn":"Mark","id":"00005b8c23190000000000000004"}
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Deleting Documents
• Supports the same filtering expressions and bindings as the remaining
CRUD operations
• removeOne() is a single-shot operation, doesn’t require execute()
colC.add("{"firstName": "Kevin", "lastName": "Kline", "age": 71}").execute();
colC.add("{"firstName": "Kevin", "lastName": "Kline", "age": 71}").execute();
colC.add("{"firstName": "Kevin", "lastName": "Kline", "age": 71}").execute();
long c = colC.removeOne("00005b8c23190000000000000005").getAffectedItemsCount();
System.out.println(c);
// 1 -> {"_id":"...","age":71,"firstName":"Kevin","lastName":"Kline"}
c = colC.remove("lastName = :ln").bind("ln", "Kline").execute().getAffectedItemsCount();
System.out.println(c);
// 2 -> {"_id":"...","age":71,"firstName":"Kevin","lastName":"Kline"}
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
X DevAPI Advanced Features
3
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Locking Reads
• Some modes require criteria based on indexes, otherwise the full collection
may get locked
Session session1 = sf.getSession("mysqlx://demouser:demopass@localhost/hr");
Session session2 = sf.getSession("mysqlx://demouser:demopass@localhost/hr");
session1.startTransaction();
Collection colC1 = session1.getDefaultSchema().getCollection("candidates");
colC1.find("_id = '00005b8c23190000000000000001'").fields("firstName AS fn", "age AS age")
.lockShared().execute().forEach(System.out::println);
// {"age":21,"fn":"Ann"}
session2.startTransaction();
Collection colC2 = session2.getDefaultSchema().getCollection("candidates");
colC2.find().fields("firstName AS fn", "age AS age").lockExclusive(LockContention.SKIP_LOCKED)
.execute().forEach(System.out::println);
// {"age":25,"fn":"Peter"}
// {"age":52,"fn":"John"}
// {"age":47,"fn":"Mark"}
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Creating Indexes
Session session = sf.getSession("mysqlx://demouser:demopass@localhost/hr");
Collection colC = session.getDefaultSchema().getCollection("candidates");
colC.createIndex("byAge", "{"fields": [{"field": "$.age", "type": "INTEGER"}]}");
mysql> show create table hr.candidates;
+------------+------------------------------------------------------------------------------------+
| Table | Create Table |
+------------+------------------------------------------------------------------------------------+
| candidates | CREATE TABLE `candidates` (
`doc` json DEFAULT NULL,
`_id` varbinary(32) GENERATED ALWAYS AS
(json_unquote(json_extract(`doc`,_utf8mb4'$._id'))) STORED NOT NULL,
`$ix_i_F177B50B40803DD7D3962E25071AC5CAA3D1139C` int(11) GENERATED ALWAYS AS
(json_extract(`doc`,_utf8mb4'$.age')) VIRTUAL,
PRIMARY KEY (`_id`),
KEY `byAge` (`$ix_i_F177B50B40803DD7D3962E25071AC5CAA3D1139C`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+------------+------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Working with Tables
Table table = schema.getTable("colors");
InsertResult res = table.insert("color")
.values("White").values("Blue").values("Red").execute();
System.out.println(
res.getAffectedItemsCount() + "/" + res.getAutoIncrementValue());
// 3/1
Consumer<Row> rowPrinter =
r -> System.out.println(r.getInt(0) + " -> " + r.getString(1));
table.select().execute().forEach(rowPrinter);
// 1 -> White
// 2 -> Blue
// 3 -> Red
table.select().orderBy("color").execute().forEach(rowPrinter);
// 2 -> Blue
// 3 -> Red
// 1 -> White
mysql> create table hr.colors (id int not null
auto_increment primary key, color varchar(100));
Query OK, 0 rows affected (0.06 sec)
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Working with Tables(cont.)
table.update().where("color = 'Blue'").set("color",
Expression.expr("CONCAT(color, 'ish')"))
.execute();
table.select().execute().forEach(rowPrinter);
// 1 -> White
// 2 -> Blueish
// 3 -> Red
table.delete().where("color LIKE 'R%'").execute();
table.select().execute().forEach(rowPrinter);
// 1 -> White
// 2 -> Blueish
table.delete().execute();
table.select().execute().forEach(rowPrinter);
// nothing
mysql> show tables from hr;
+--------------+
| Tables_in_hr |
+--------------+
| candidates |
| colors |
| jobs |
+--------------+
3 rows in set (0.00 sec)
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Raw SQL
SqlResult res = session.sql(
"INSERT INTO colors (color) VALUES ('Black'), ('White')").execute();
System.out.println(res.getAffectedItemsCount() + "/" + res.getAutoIncrementValue());
// 2/1
res = session.sql("SELECT * FROM colors").execute();
res.forEach(rowPrinter);
// 1 --> Black
// 2 --> White
res.getColumns().stream().map(Column::getType).forEach(System.out::println);
// INT
// STRING
session.sql("DELETE FROM colors").execute();
session.sql("SELECT * FROM colors").execute().forEach(rowPrinter);
// nothing
session.sql("DROP TABLE colors").execute();
mysql> show tables from hr;
+--------------+
| Tables_in_hr |
+--------------+
| candidates |
| jobs |
+--------------+
2 rows in set (0.00 sec)
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Q & A
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Connector/J and X DevAPI
• https://dev.mysql.com/do
c/connector-j/8.0/en/
• https://dev.mysql.com/do
c/dev/connector-j/8.0/
• https://dev.mysql.com/do
c/x-devapi-userguide/en/
Document store
• https://dev.mysql.com/do
c/refman/8.0/en/docume
nt-store.html
• https://dev.mysql.com/do
c/refman/8.0/en/x-
plugin.html
• https://dev.mysql.com/do
c/internals/en/x-
protocol.html
Support & resources
• https://forums.mysql.com
/list.php?39
• https://github.com/mysql
/mysql-connector-j
• https://mysqlcommunity.
slack.com/messages/conn
ectors
• https://bugs.mysql.com/
Getting Help
Connector/J Beyond JDBC: the X DevAPI for Java and MySQL as a Document Store

Mais conteúdo relacionado

Mais procurados

Elephants vs. Dolphins: Comparing PostgreSQL and MySQL for use in the DoD
Elephants vs. Dolphins:  Comparing PostgreSQL and MySQL for use in the DoDElephants vs. Dolphins:  Comparing PostgreSQL and MySQL for use in the DoD
Elephants vs. Dolphins: Comparing PostgreSQL and MySQL for use in the DoDJamey Hanson
 
Sql vs NO-SQL database differences explained
Sql vs NO-SQL database differences explainedSql vs NO-SQL database differences explained
Sql vs NO-SQL database differences explainedSatya Pal
 
Postgres_9.0 vs MySQL_5.5
Postgres_9.0 vs MySQL_5.5Postgres_9.0 vs MySQL_5.5
Postgres_9.0 vs MySQL_5.5Trieu Dao Minh
 
NOSQL Meets Relational - The MySQL Ecosystem Gains More Flexibility
NOSQL Meets Relational - The MySQL Ecosystem Gains More FlexibilityNOSQL Meets Relational - The MySQL Ecosystem Gains More Flexibility
NOSQL Meets Relational - The MySQL Ecosystem Gains More FlexibilityIvan Zoratti
 
Things Every Oracle DBA Needs to Know About the Hadoop Ecosystem 20170527
Things Every Oracle DBA Needs to Know About the Hadoop Ecosystem 20170527Things Every Oracle DBA Needs to Know About the Hadoop Ecosystem 20170527
Things Every Oracle DBA Needs to Know About the Hadoop Ecosystem 20170527Zohar Elkayam
 
What is NoSQL and CAP Theorem
What is NoSQL and CAP TheoremWhat is NoSQL and CAP Theorem
What is NoSQL and CAP TheoremRahul Jain
 
Scalable relational database with SQL Azure
Scalable relational database with SQL AzureScalable relational database with SQL Azure
Scalable relational database with SQL AzureShy Engelberg
 
The Complete MariaDB Server tutorial
The Complete MariaDB Server tutorialThe Complete MariaDB Server tutorial
The Complete MariaDB Server tutorialColin Charles
 
The Great Debate: PostgreSQL vs MySQL
The Great Debate: PostgreSQL vs MySQLThe Great Debate: PostgreSQL vs MySQL
The Great Debate: PostgreSQL vs MySQLEDB
 
MySQL Document Store - A Document Store with all the benefts of a Transactona...
MySQL Document Store - A Document Store with all the benefts of a Transactona...MySQL Document Store - A Document Store with all the benefts of a Transactona...
MySQL Document Store - A Document Store with all the benefts of a Transactona...Olivier DASINI
 
MySQL Storage Engines
MySQL Storage EnginesMySQL Storage Engines
MySQL Storage EnginesKarthik .P.R
 
NoSQL Data Architecture Patterns
NoSQL Data ArchitecturePatternsNoSQL Data ArchitecturePatterns
NoSQL Data Architecture PatternsMaynooth University
 
Chapter1: NoSQL: It’s about making intelligent choices
Chapter1: NoSQL: It’s about making intelligent choicesChapter1: NoSQL: It’s about making intelligent choices
Chapter1: NoSQL: It’s about making intelligent choicesMaynooth University
 
Application Development with Apache Cassandra as a Service
Application Development with Apache Cassandra as a ServiceApplication Development with Apache Cassandra as a Service
Application Development with Apache Cassandra as a ServiceWSO2
 
Maria db the new mysql (Colin Charles)
Maria db the new mysql (Colin Charles)Maria db the new mysql (Colin Charles)
Maria db the new mysql (Colin Charles)Ontico
 
MariaDB 10: The Complete Tutorial
MariaDB 10: The Complete TutorialMariaDB 10: The Complete Tutorial
MariaDB 10: The Complete TutorialColin Charles
 
Converting from MySQL to PostgreSQL
Converting from MySQL to PostgreSQLConverting from MySQL to PostgreSQL
Converting from MySQL to PostgreSQLJohn Ashmead
 
Maria db vs mysql
Maria db vs mysqlMaria db vs mysql
Maria db vs mysqlNitin KR
 

Mais procurados (20)

Elephants vs. Dolphins: Comparing PostgreSQL and MySQL for use in the DoD
Elephants vs. Dolphins:  Comparing PostgreSQL and MySQL for use in the DoDElephants vs. Dolphins:  Comparing PostgreSQL and MySQL for use in the DoD
Elephants vs. Dolphins: Comparing PostgreSQL and MySQL for use in the DoD
 
Sql vs NO-SQL database differences explained
Sql vs NO-SQL database differences explainedSql vs NO-SQL database differences explained
Sql vs NO-SQL database differences explained
 
Postgres_9.0 vs MySQL_5.5
Postgres_9.0 vs MySQL_5.5Postgres_9.0 vs MySQL_5.5
Postgres_9.0 vs MySQL_5.5
 
NOSQL Meets Relational - The MySQL Ecosystem Gains More Flexibility
NOSQL Meets Relational - The MySQL Ecosystem Gains More FlexibilityNOSQL Meets Relational - The MySQL Ecosystem Gains More Flexibility
NOSQL Meets Relational - The MySQL Ecosystem Gains More Flexibility
 
Things Every Oracle DBA Needs to Know About the Hadoop Ecosystem 20170527
Things Every Oracle DBA Needs to Know About the Hadoop Ecosystem 20170527Things Every Oracle DBA Needs to Know About the Hadoop Ecosystem 20170527
Things Every Oracle DBA Needs to Know About the Hadoop Ecosystem 20170527
 
What is NoSQL and CAP Theorem
What is NoSQL and CAP TheoremWhat is NoSQL and CAP Theorem
What is NoSQL and CAP Theorem
 
Scalable relational database with SQL Azure
Scalable relational database with SQL AzureScalable relational database with SQL Azure
Scalable relational database with SQL Azure
 
The Complete MariaDB Server tutorial
The Complete MariaDB Server tutorialThe Complete MariaDB Server tutorial
The Complete MariaDB Server tutorial
 
NoSQL Consepts
NoSQL ConseptsNoSQL Consepts
NoSQL Consepts
 
The Great Debate: PostgreSQL vs MySQL
The Great Debate: PostgreSQL vs MySQLThe Great Debate: PostgreSQL vs MySQL
The Great Debate: PostgreSQL vs MySQL
 
MySQL Document Store - A Document Store with all the benefts of a Transactona...
MySQL Document Store - A Document Store with all the benefts of a Transactona...MySQL Document Store - A Document Store with all the benefts of a Transactona...
MySQL Document Store - A Document Store with all the benefts of a Transactona...
 
MySQL Storage Engines
MySQL Storage EnginesMySQL Storage Engines
MySQL Storage Engines
 
NoSQL Data Architecture Patterns
NoSQL Data ArchitecturePatternsNoSQL Data ArchitecturePatterns
NoSQL Data Architecture Patterns
 
Chapter1: NoSQL: It’s about making intelligent choices
Chapter1: NoSQL: It’s about making intelligent choicesChapter1: NoSQL: It’s about making intelligent choices
Chapter1: NoSQL: It’s about making intelligent choices
 
Application Development with Apache Cassandra as a Service
Application Development with Apache Cassandra as a ServiceApplication Development with Apache Cassandra as a Service
Application Development with Apache Cassandra as a Service
 
Maria db the new mysql (Colin Charles)
Maria db the new mysql (Colin Charles)Maria db the new mysql (Colin Charles)
Maria db the new mysql (Colin Charles)
 
MariaDB 10: The Complete Tutorial
MariaDB 10: The Complete TutorialMariaDB 10: The Complete Tutorial
MariaDB 10: The Complete Tutorial
 
MySQL 开发
MySQL 开发MySQL 开发
MySQL 开发
 
Converting from MySQL to PostgreSQL
Converting from MySQL to PostgreSQLConverting from MySQL to PostgreSQL
Converting from MySQL to PostgreSQL
 
Maria db vs mysql
Maria db vs mysqlMaria db vs mysql
Maria db vs mysql
 

Semelhante a Connector/J Beyond JDBC: the X DevAPI for Java and MySQL as a Document Store

MySQL 8.0 - What's New ?
MySQL 8.0 - What's New ?MySQL 8.0 - What's New ?
MySQL 8.0 - What's New ?Olivier DASINI
 
MySQL Day Paris 2018 - What’s New in MySQL 8.0 ?
MySQL Day Paris 2018 - What’s New in MySQL 8.0 ?MySQL Day Paris 2018 - What’s New in MySQL 8.0 ?
MySQL Day Paris 2018 - What’s New in MySQL 8.0 ?Olivier DASINI
 
MySQL Shell: the daily tool for devs and admins. By Vittorio Cioe.
MySQL Shell: the daily tool for devs and admins. By Vittorio Cioe.MySQL Shell: the daily tool for devs and admins. By Vittorio Cioe.
MySQL Shell: the daily tool for devs and admins. By Vittorio Cioe.Cloud Native Day Tel Aviv
 
MySQL Connector/Node.js and the X DevAPI
MySQL Connector/Node.js and the X DevAPIMySQL Connector/Node.js and the X DevAPI
MySQL Connector/Node.js and the X DevAPIRui Quelhas
 
Node.js and the MySQL Document Store
Node.js and the MySQL Document StoreNode.js and the MySQL Document Store
Node.js and the MySQL Document StoreRui Quelhas
 
MySQL Shell: The DevOps Tool for MySQL
MySQL Shell: The DevOps Tool for MySQLMySQL Shell: The DevOps Tool for MySQL
MySQL Shell: The DevOps Tool for MySQLMiguel Araújo
 
MySQL Document Store and Node.JS
MySQL Document Store and Node.JSMySQL Document Store and Node.JS
MySQL Document Store and Node.JSReggie Burnett
 
MySQL 8.0, what's new ? - Forum PHP 2018
MySQL 8.0, what's new ? - Forum PHP 2018MySQL 8.0, what's new ? - Forum PHP 2018
MySQL 8.0, what's new ? - Forum PHP 2018Olivier DASINI
 
MySQL Day Paris 2018 - MySQL JSON Document Store
MySQL Day Paris 2018 - MySQL JSON Document StoreMySQL Day Paris 2018 - MySQL JSON Document Store
MySQL Day Paris 2018 - MySQL JSON Document StoreOlivier DASINI
 
MySQL Document Store (Oracle Code Warsaw 2018)
MySQL Document Store (Oracle Code Warsaw 2018)MySQL Document Store (Oracle Code Warsaw 2018)
MySQL Document Store (Oracle Code Warsaw 2018)Vittorio Cioe
 
2018: State of the Dolphin, MySQL Keynote at Percona Live Europe 2018, Frankf...
2018: State of the Dolphin, MySQL Keynote at Percona Live Europe 2018, Frankf...2018: State of the Dolphin, MySQL Keynote at Percona Live Europe 2018, Frankf...
2018: State of the Dolphin, MySQL Keynote at Percona Live Europe 2018, Frankf...Geir Høydalsvik
 
20171104 hk-py con-mysql-documentstore_v1
20171104 hk-py con-mysql-documentstore_v120171104 hk-py con-mysql-documentstore_v1
20171104 hk-py con-mysql-documentstore_v1Ivan Ma
 
MySQL Connector/J in the Making of Modern Applications
MySQL Connector/J in the Making of Modern ApplicationsMySQL Connector/J in the Making of Modern Applications
MySQL Connector/J in the Making of Modern ApplicationsFilipe Silva
 
MySQL 8 loves JavaScript
MySQL 8 loves JavaScript MySQL 8 loves JavaScript
MySQL 8 loves JavaScript Sanjay Manwani
 
MySQL Document Store
MySQL Document StoreMySQL Document Store
MySQL Document StoreMario Beck
 
MySQL Day Paris 2016 - MySQL as a Document Store
MySQL Day Paris 2016 - MySQL as a Document StoreMySQL Day Paris 2016 - MySQL as a Document Store
MySQL Day Paris 2016 - MySQL as a Document StoreOlivier DASINI
 
MySQL JSON Document Store - A Document Store with all the benefits of a Trans...
MySQL JSON Document Store - A Document Store with all the benefits of a Trans...MySQL JSON Document Store - A Document Store with all the benefits of a Trans...
MySQL JSON Document Store - A Document Store with all the benefits of a Trans...Olivier DASINI
 
MySQL 5.7 como Document Store
MySQL 5.7 como Document StoreMySQL 5.7 como Document Store
MySQL 5.7 como Document StoreMySQL Brasil
 
Oracle NoSQL Database -- Big Data Bellevue Meetup - 02-18-15
Oracle NoSQL Database -- Big Data Bellevue Meetup - 02-18-15Oracle NoSQL Database -- Big Data Bellevue Meetup - 02-18-15
Oracle NoSQL Database -- Big Data Bellevue Meetup - 02-18-15Dave Segleau
 

Semelhante a Connector/J Beyond JDBC: the X DevAPI for Java and MySQL as a Document Store (20)

MySQL 8.0 - What's New ?
MySQL 8.0 - What's New ?MySQL 8.0 - What's New ?
MySQL 8.0 - What's New ?
 
MySQL Day Paris 2018 - What’s New in MySQL 8.0 ?
MySQL Day Paris 2018 - What’s New in MySQL 8.0 ?MySQL Day Paris 2018 - What’s New in MySQL 8.0 ?
MySQL Day Paris 2018 - What’s New in MySQL 8.0 ?
 
MySQL Shell: the daily tool for devs and admins. By Vittorio Cioe.
MySQL Shell: the daily tool for devs and admins. By Vittorio Cioe.MySQL Shell: the daily tool for devs and admins. By Vittorio Cioe.
MySQL Shell: the daily tool for devs and admins. By Vittorio Cioe.
 
MySQL Connector/Node.js and the X DevAPI
MySQL Connector/Node.js and the X DevAPIMySQL Connector/Node.js and the X DevAPI
MySQL Connector/Node.js and the X DevAPI
 
Node.js and the MySQL Document Store
Node.js and the MySQL Document StoreNode.js and the MySQL Document Store
Node.js and the MySQL Document Store
 
MySQL Shell: The DevOps Tool for MySQL
MySQL Shell: The DevOps Tool for MySQLMySQL Shell: The DevOps Tool for MySQL
MySQL Shell: The DevOps Tool for MySQL
 
MySQL Document Store and Node.JS
MySQL Document Store and Node.JSMySQL Document Store and Node.JS
MySQL Document Store and Node.JS
 
MySQL 8.0, what's new ? - Forum PHP 2018
MySQL 8.0, what's new ? - Forum PHP 2018MySQL 8.0, what's new ? - Forum PHP 2018
MySQL 8.0, what's new ? - Forum PHP 2018
 
MySQL Day Paris 2018 - MySQL JSON Document Store
MySQL Day Paris 2018 - MySQL JSON Document StoreMySQL Day Paris 2018 - MySQL JSON Document Store
MySQL Day Paris 2018 - MySQL JSON Document Store
 
MySQL Document Store (Oracle Code Warsaw 2018)
MySQL Document Store (Oracle Code Warsaw 2018)MySQL Document Store (Oracle Code Warsaw 2018)
MySQL Document Store (Oracle Code Warsaw 2018)
 
2018: State of the Dolphin, MySQL Keynote at Percona Live Europe 2018, Frankf...
2018: State of the Dolphin, MySQL Keynote at Percona Live Europe 2018, Frankf...2018: State of the Dolphin, MySQL Keynote at Percona Live Europe 2018, Frankf...
2018: State of the Dolphin, MySQL Keynote at Percona Live Europe 2018, Frankf...
 
20171104 hk-py con-mysql-documentstore_v1
20171104 hk-py con-mysql-documentstore_v120171104 hk-py con-mysql-documentstore_v1
20171104 hk-py con-mysql-documentstore_v1
 
MySQL Connector/J in the Making of Modern Applications
MySQL Connector/J in the Making of Modern ApplicationsMySQL Connector/J in the Making of Modern Applications
MySQL Connector/J in the Making of Modern Applications
 
MySQL 8 loves JavaScript
MySQL 8 loves JavaScript MySQL 8 loves JavaScript
MySQL 8 loves JavaScript
 
Novinky v Oracle Database 18c
Novinky v Oracle Database 18cNovinky v Oracle Database 18c
Novinky v Oracle Database 18c
 
MySQL Document Store
MySQL Document StoreMySQL Document Store
MySQL Document Store
 
MySQL Day Paris 2016 - MySQL as a Document Store
MySQL Day Paris 2016 - MySQL as a Document StoreMySQL Day Paris 2016 - MySQL as a Document Store
MySQL Day Paris 2016 - MySQL as a Document Store
 
MySQL JSON Document Store - A Document Store with all the benefits of a Trans...
MySQL JSON Document Store - A Document Store with all the benefits of a Trans...MySQL JSON Document Store - A Document Store with all the benefits of a Trans...
MySQL JSON Document Store - A Document Store with all the benefits of a Trans...
 
MySQL 5.7 como Document Store
MySQL 5.7 como Document StoreMySQL 5.7 como Document Store
MySQL 5.7 como Document Store
 
Oracle NoSQL Database -- Big Data Bellevue Meetup - 02-18-15
Oracle NoSQL Database -- Big Data Bellevue Meetup - 02-18-15Oracle NoSQL Database -- Big Data Bellevue Meetup - 02-18-15
Oracle NoSQL Database -- Big Data Bellevue Meetup - 02-18-15
 

Último

Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 

Último (20)

Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 

Connector/J Beyond JDBC: the X DevAPI for Java and MySQL as a Document Store

  • 1. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Connector/J Beyond JDBC: Filipe Silva Senior Software Developer Oracle, MySQL October 23, 2018 the X DevAPI for Java and MySQL as a Document Store Presented with
  • 2. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Safe Harbor Statement 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.
  • 3. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | • Filipe Silva – Connector/J Developer and Team Lead, MySQL Middleware and Clients – filipe.silva@oracle.com Who am I?
  • 4. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Program Agenda Introduction MySQL as a Document Store X DevAPI Overview The X DevAPI in Connector/J Q & A 1 2 3 4 5
  • 5. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Introduction
  • 6. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | From a DBA perspective From a developer perspective How Data is Perceived { "_id": "5bc1195b6f36a842a4f65b24", "age": 21, "name": { "first": "Vicky", "last": "Leonard“ }, "company": "MUSAPHICS", "email": "vicky.leonard@musaphics.io", "phone": "+1 (864) 428-3897", "address": "506 Broadway , Jeff, Ohio, 2249", "tags": ["java", "development", "database"] }
  • 7. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | • Developers and companies feel compelled to explore NoSQL – RDBMS “do not scale” – “Schema-less is agile” – Developers don’t like SQL • Later finding out about limitations and drawbacks – Lack of referential integrity. – Lack of true ACID support. • Different solutions flood the market – Key-Value Data Store Oracle NoSQL Database, Redis, Memcached – Document Store Apache CouchDB, MongoDB, Couchbase – Wide Column Store Bigtable, Apache Cassandra, Apache HBase – Graph Store Neo4j, JanusGraph How NoSQL Comes Into Play NoSQL also stands for “Not Only SQL”
  • 8. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Developers Schema-less and/or schema based developement Rapid prototyping Document based model Transactions Operations Comprehensive tooling ecosystem Performance management Robust replication, backup and restore Simpler application schema upgrades Business Owners Don’t lose my data  ACID compliant Capture all my data  Extensible, Scalable Products on schedule  Affordable time to market  Fast development What If… Schema and schema-less co-existed together in the same technology stack?
  • 9. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | NoSQL + SQL = MySQL SQL NoSQL
  • 10. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | MySQL as a Document Store “An easy, straightforward way of working with JSON documents in MySQL”
  • 11. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | MySQL Document Store MySQL Server X Plugin Client Application X DevAPI (Connector) MySQL Router MySQL Protocol X Protocol
  • 12. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | X DevAPI • Provides a high-level database API to develop CRUD-based applications – Off-the-shelf NoSQL document operations – Available in many popular languages and the brand new MySQL Shell – Expert-level advanced features, including raw SQL, transactions, locking and more – Powerful way to query documents and relational tables • Empowered (optionally) by clustered MySQL setups – Seamless integration with InnoDB clusters via the MySQL Router https://dev.mysql.com/doc/x-devapi-userguide/en/ MySQL Server X Plugin Client Application X DevAPI (Connector) MySQL Router
  • 13. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | MySQL Router • MySQL Router is an integral part of the InnoDB cluster – Lightweight middleware for transparent routing between client applications and back- end MySQL Servers – Standalone component; accepts and routes requests to the appropriate servers – Available for Linux, MacOS and Windows. • Provides high availability and scalability to client applicaitions https://dev.mysql.com/doc/mysql-router/8.0/en/ MySQL Server X Plugin Client Application X DevAPI (Connector) MySQL Router
  • 14. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | X Plugin • MySQL server plugin that supports the MySQL Document Store – Enabled by default on MySQL 8.0 – Listens on TCP port 33060 by default. • Provides full document storage layer on existing MySQL installations – Accepts X DevAPI client requests and processes them using the core MySQL engine • Implements a new client-server protocol – the X Protocol https://dev.mysql.com/doc/refman/8.0/en/x-plugin.html MySQL Server X Plugin Client Application X DevAPI (Connector) MySQL Router
  • 15. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | X Protocol • A new protocol based on Google Protocol Buffers – Implemented in the X Plugin – Improved data streaming: Vectored I/O; Pipelining • Simple packets with structured content – CRUD operation trees – Pipelined messages can be wrapped by expectations based on conditions – Embedded parameter binding • Security baked in – SSL/TLS by default – No information leaks out to non-authenticated users https://dev.mysql.com/doc/internals/en/x-protocol.html MySQL Server X Plugin Client Application X DevAPI (Connector) MySQL Router
  • 16. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Protocol Buffer Message Sample 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" } } } } } }
  • 17. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | X DevAPI Overview
  • 18. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | • Enables MySQL as a Document Store Database • Offers Schema-less data storage development • Fluent API – Intuitive query building methods – Raw SQL interface also available • Domain specific expression language • Synchronous and Asynchronous operation executions • Programming language agnostic • Secure by default (SSL/TLS and SHA256 authentication) • Data consistency with transactions, save points and row-locking • Integrated in the MySQL Shell – JavaScript and Python scripting capabilities Key Features
  • 19. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Documents • Documents are represented as JSON structures – Open standard, language-independent file format – Human-readable text – Documents are uniquely identified by one common attribute – “_id” • JSON: “JavaScript Object Notation” – Name/value pairs, array data and other serializable types – Cross-platform serialization format (common for web services) – Standardized as ECMA-404 (http://json.org) and RFC 8259 (https://tools.ietf.org/html/rfc8259) – Natively supported by MySQL • Specific data type and multiple functions to operate on JSON values and columns
  • 20. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Collections • Collections store multiple 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 look into the JSON structures for indexing and enforcing constraints
  • 21. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Schema-less Data • Modern and versatile CRUD API – Semantic Document Store methods • Manage data without writing a single line of SQL > users.add({ name: ‘Filipe' }).add({ name: ‘Alex' }) Query OK, 2 items affected (0.0373 sec) > users.find() [ { "_id": "00005b50ced40000000000000001", "name": “Filipe" }, { "_id": "00005b50ced40000000000000002", "name": “Alex" } ] 2 documents in set (0.0009 sec) > users.modify('true').set('team', ‘connector-java') Query OK, 2 items affected (0.0751 sec) > users.find('name = “Filipe"') [ { "_id": "00005b50ced40000000000000001", "name": “Filipe", "team": “connector-java" } ] 1 document in set (0.0026 sec)
  • 22. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Fluent API • Code flows from a single point of entry – getSession() – First-class support for text-editor (or 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 most popular programming languages
  • 23. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | CollectionAdd collection.add({ name: ‘Ann', age: 42 }) .add({ name: ‘Peter', age: 23 }) .execute() collection.add([ { name: ‘John', age: 50 }, { name: ‘Mark', age: 25 } ]).execute()
  • 24. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | CollectionFind collection.find('name = :name') .bind('name', ‘Ann') .fields('COUNT(age) AS age') .groupBy('age') .having('age > 42') .sort('age DESC') .limit(10) .offset(5) .execute()
  • 25. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | CollectionModify collection.modify('name = :name') .bind('name', ‘Ann') .set('age', 42) .sort('name ASC') .limit(1) .execute() collection.modify('name = :name') .bind('name', ‘Peter') .patch({ age: 42, active: false }) .sort('name DESC') .limit(1) .execute()
  • 26. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | CollectionRemove collection.remove('name = :name') .bind('name', ‘Ann') .sort('age ASC') .limit(1) .execute()
  • 27. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Method Chaining and Flexible and Parameters • Repeated method calls with individual arguments • A single list/array of a variable number of arguments mysqlx.getSession('root@localhost') mysqlx.getSession({ user: 'root' }) collection.add({ name: ‘Ann' }).add({ name: ‘Peter' }) collection.add([{ name: ‘John' }, { name: ‘Mark' }]) collection.find('name = :name').bind('name', ‘Ann') collection.find('name = :name').bind({ name: ‘Ann' }) collection.find().fields(‘name', ‘age') collection.find().fields([‘name', ‘age'])
  • 28. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Expression Language • Subset of SQL expressions • Meaningful and human-readable • Common between all official connector implementations // JavaScript collection .find("name = ‘Ann' AND age > 42") .fields("name", "age") .groupBy("name", "age") .sort("name ASC", "age DESC") .limit(4) .offset(2) .execute() // Java collection .find("name = ‘Ann' AND age > 42") .fields("name", "age") .groupBy("name", "age") .sort("name ASC", "age DESC") .limit(4) .offset(2) .execute()
  • 29. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Secure by Default – SSL Modes • SSL/TLS enabled by default in TCP based sessions • Additional server certificate validation options • Default options can be overridden on-demand mysqlx.getSession({ ssl: false }) mysqlx.getSession('mysqlx://root@localhost?ssl-mode=DISABLED') mysqlx.getSession({ ssl: true, sslOptions: { ca: '/path/to/ca.pem' } }) mysqlx.getSession('mysqlx://root@localhost?ssl-ca=(/path/to/ca.pem)') mysqlx.getSession({ ssl: true, sslOptions: { ca: '/path/to/ca.pem', crl: '/path/to/crl.pem' } }) mysqlx.getSession('mysqlx://root@localhost?ssl-ca=(/path/to/ca.pem)&ssl-crl=(/path/to/crl.pem)')
  • 30. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Authentication Mechanisms • SHA-1 and SHA-2 password hashing • Supports the main server authentication plugins: – mysql_native_password – sha256_password – caching_sha2_password mysqlx.getSession({ user: ‘user1', auth: 'MYSQL41' }) mysqlx.getSession('mysqlx://root@localhost?auth=MYSQL41') mysqlx.getSession({ user: ‘user2', auth: 'PLAIN' }) mysqlx.getSession('mysqlx://root@localhost?auth=PLAIN') mysqlx.getSession({ user: ‘user3', auth: 'SHA256_MEMORY' }) mysqlx.getSession('mysqlx://root@localhost?auth=SHA256_MEMORY') MySQL41 SHA256_MEMORY
  • 31. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Transactions and Savepoints • 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('foo') // run more operations (2) session.releaseSavepoint('foo') session.commit() } catch (err) { try { session.rollbackTo('foo') // go to (2) } catch (err) { session.rollback() // revert the entire thing } }
  • 32. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Row Locking • Reads determine isolation level in the presence of concurrent transactions • Exclusive and Shared locks with two operation modes besides DEFAULT: – NOWAIT – SKIP_LOCKED collection.find('name = :name').bind('name', ‘Ann').lockExclusive() collection.find('name = :name').bind('name', ‘Ann').lockExclusive(mysqlx.LockContention.DEFAULT) collection.find('name = :name').bind('name', ‘Ann').lockExclusive(mysqlx.LockContention.NOWAIT) collection.find('name = :name').bind('name', ‘Ann').lockExclusive(mysqlx.LockContention.SKIP_LOCKED) collection.find('name = :name').bind('name', ‘Ann').lockShared() collection.find('name = :name').bind('name', ‘Ann').lockShared(mysqlx.LockContention.DEFAULT) collection.find('name = :name').bind('name', ‘Ann').lockShared(mysqlx.LockContention.NOWAIT) collection.find('name = :name').bind('name', ‘Ann').lockShared(mysqlx.LockContention.SKIP_LOCKED)
  • 33. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Raw SQL • 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 – Using JOINs // create a table session.sql('CREATE TABLE foo (bar VARCHAR(3))').execute() // add a unique constraint session.sql('ALTER TABLE foo ADD COLUMN bar VARCHAR(3) GENERATED ALWAYS AS doc->>"$.bar" VIRTUAL UNIQUE KEY NOT NULL').execute() // execute a JOIN query session.sql('SELECT DISTINCT t1.bar FROM foo t1 JOIN baz t2 ON t1.bar = t2.qux WHERE t1.qux = t2.quux').execute()
  • 34. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | The X DevAPI in Connector/J
  • 35. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Overview, Setup and Sneak Peek 1
  • 36. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Overview • MySQL develops and maintains two Connector/J versions (5.1 and 8.0) • 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
  • 37. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Connector/J 5.1 • Type 4 JDBC driver • Compliant with JDBC 4.2 (Java 8.0) • Requires Java 5 or above • Supports MySQL Protocol • Compatible with MySQL 5.1 and above • Available since Apr. 2007 Connector/J 8.0 • 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 • Available since Aug. 2015 Overview
  • 38. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Connector/J 5.1 Compatible with [Java 5] Java 6 Java 7 [Java 8] Java 9+ Supports MySQL 5.1 MySQL 5.5 MySQL 5.6 MySQL 5.7 MySQL 8.0 Connector/J 8.0 Compatible with [Java 8] Java 9+ Supports MySQL 5.5 MySQL 5.6 MySQL 5.7 incl. X Plugin MySQL 8.0 incl. X Plugin Overview [Java n] – Required for compiling
  • 39. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Obtaining the MySQL Connector/J Library • Download from the official MySQL Downloads page: – https://dev.mysql.com/downloads/connector/j/8.0.html • Portable library bundle • Linux packages and Windows installer • Configure as a maven dependency – https://search.maven.org/search?q=g:mysql%20AND%20a:mysql-connector-java <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.12</version> </dependency>
  • 40. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Project Dependencies • MySQL Connector/J depends on Google’s protobuf java library for compiling and running X DevAPI code • Java applications using the X DevAPI via Connector/J 8.0.13 must have the following libraries in their Classpath: – mysql-connector-java-8.0.13.jar – protobuf-java-3.6.1.jar • Can be found in the directory lib, inside Connector/J portable bundles • Installations via Linux packages or Windows installer require manual download: – https://repo1.maven.org/maven2/com/google/protobuf/protobuf-java/3.6.1/ • Maven enabled projects manage and download all dependencies automatically
  • 41. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Side by Side: JDBC Sneak Peek X DevAPI CRUD X DevAPI Raw JDBC Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/hr", "demouser", "demopass"); Statement stmt = conn.createStatement(); stmt.execute("CREATE TABLE offices (id INT AUTO_INCREMENT PRIMARY KEY, country VARCHAR(100), " + "capacity INT)"); ResultSet rs = conn.getMetaData().getTables(“hr", "%", "%", null); while (rs.next()) { System.out.println(rs.getString(1) + " - " + rs.getString(3)); } // continue... hr - offices
  • 42. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Side by Side: JDBC Sneak Peek X DevAPI CRUD X DevAPI Raw JDBC PreparedStatement pstmt = conn.prepareStatement("INSERT INTO offices VALUES (NULL, ?, ?)"); pstmt.setString(1, "USA"); pstmt.setInt(2, 200); pstmt.addBatch(); pstmt.setString(1, "UK"); pstmt.setInt(2, 170); pstmt.addBatch(); pstmt.executeBatch(); pstmt.close(); rs = stmt.executeQuery("SELECT * FROM offices"); while (rs.next()) { System.out.println(rs.getInt(1) + ": " + rs.getString(2) + " (" + rs.getInt(3) + ")"); } stmt.execute("DROP TABLE offices"); stmt.close(); conn.close(); 1: USA (200) 2: UK (170)
  • 43. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Side by Side: X DevAPI Raw Sneak Peek X DevAPI CRUD X DevAPI Raw JDBC SessionFactory sf = new SessionFactory(); Session sess = sf.getSession("mysqlx://demouser:demopass@localhost/hr"); sess.sql("CREATE TABLE offices (id INT AUTO_INCREMENT PRIMARY KEY, country VARCHAR(100), " + "capacity INT)").execute(); sess.getDefaultSchema().getTables().forEach(System.out::println); sess.sql("INSERT INTO offices VALUES (NULL, 'USA', 200), (NULL, 'UK', 170)").execute(); sess.sql("SELECT * FROM offices").execute().forEach(r -> System.out.println(r.getInt(0) + ": " + r.getString(1) + " (" + r.getInt(2) + ")")); sess.sql("DROP TABLE offices").execute(); sess.close(); Table(hr.offices) 1: USA (200) 2: UK (170)
  • 44. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Side by Side: X DevAPI CRUD Sneak Peek X DevAPI CRUD X DevAPI Raw JDBC SessionFactory sf = new SessionFactory(); Session sess = sf.getSession("mysqlx://demouser:demopass@localhost/hr"); sess.sql("CREATE TABLE offices (id INT AUTO_INCREMENT PRIMARY KEY, country VARCHAR(100), " + "capacity INT)").execute(); Schema schm = sess.getDefaultSchema(); schm.getTables().forEach(System.out::println); Table tbl = schm.getTable("offices"); tbl.insert("country", "capacity").values("USA", 200).values("UK", 170).execute(); tbl.select().execute().forEach(r -> System.out.println(r.getInt(0) + ": " + r.getString(1) + " (" + r.getInt(2) + ")")); sess.sql("DROP TABLE offices").execute(); sess.close(); Table(hr.offices) 1: USA (200) 2: UK (170)
  • 45. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | X DevAPI Core Features 2
  • 46. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Main X DevAPI Objects Session Entry point for all operations; Establishes server sessions/connections and provides means to manage schemas; Enables raw SQL operations Schema Equivalent to a MySQL database; Manages collections and tables Collection Document storage and management; Equivalent to a MySQL table with a specific structure Table Plain 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; Represents a JSON collection of name/value pairs RowResult Result of selecting data from tables (via table.select()) Row Single data row from a RowResult SqlResult Result of selecting data from raw SQL (via session.sql()) Package: com.mysql.cj.xdevapi
  • 47. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | JSON Parsing and Processing JsonParser Transforms JSON documents in string form into DbDoc or JsonArray objects DbDoc Represents a JSON 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 Package: com.mysql.cj.xdevapi
  • 48. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Data for Following Code Samples hr.candidates ::= [ { "firstName": "Ann", "age": 21 }, { "firstName": "Peter", "age": 25 }, { "firstName": "John", "lastName": "Jones", "age": 52, "languages": ["EN", "SP"] }, { "firstName": "Mark", "lastName": "Miller", "age": 47, "languages": ["EN", "SP", "FR", "PT"] } ]
  • 49. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | SessionFactory sf = new SessionFactory(); // schema HR must exist Session session1 = sf.getSession("mysqlx://demouser:demopass@localhost/hr"); Session session2 = sf.getSession("mysqlx://demouser:demopass@localhost/hr?xdevapi.ssl-mode=DISABLED"); session1.close(); session2.close(); Creating a Session • Created via SessionFactory.getSession(uri), that takes a URI – mysqlx://<user>:<pass>@<host>:<port>/<default-schema>?<property=value>&… – The URI identifies the user, host, default schema and a set of session attributes – All URI components are optional in Connector/J • It is a good practice to always close sessions after using them
  • 50. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | session.createSchema("hr", true); session.createSchema(“foo"); List<Schema> schemas = session.getSchemas(); schemas.forEach(System.out::println); // Schema(hr) // Schema(foo) session.dropSchema("foo"); session.getSchemas().forEach(System.out::println); // Schema(hr) Managing Schemas • The Session class allows to create, list and drop schemas • A schema is the same as a MySQL database • The schema becomes the entry point for working with collections – Tied to an open session from where transactions can be managed mysql> show databases; +--------------------+ | Database | +--------------------+ | hr | | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec)
  • 51. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Managing Collections Session session = sf.getSession("mysqlx://demouser:demopass@localhost/hr"); Schema schema = session.getDefaultSchema(); Collection colC = schema.createCollection("candidates", true); Collection colJ = schema.createCollection("jobs", true); Collection colF = schema.createCollection("foo"); schema.getCollections().forEach(System.out::println); // Collection(hr.candidates) // Collection(hr.jobs) schema.getCollections("jo%").forEach(System.out::println); // Collection(hr.jobs) System.out.println(schema.getCollectionAsTable("foo")); // Table(hr.foo) schema.dropCollection("foo"); session.close();
  • 52. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Managing Collections Session session = sf.getSession("mysqlx://demouser:demopass@localhost/hr"); Schema schema = session.getDefaultSchema(); Collection colC = schema.createCollection("candidates", true); Collection colJ = schema.createCollection("jobs", true); Collection colD = schema.createCollection("foo"); schema.getCollections().forEach(System.out::println); // Collection(hr.candidates) // Collection(hr.jobs) schema.getCollections("jo%").forEach(System.out::println); // Collection(hr.jobs) System.out.println(schema.getCollectionAsTable("foo")); // Table(hr.foo) schema.dropCollection("foo"); session.close(); mysql> show tables from hr; +--------------+ | Tables_in_hr | +--------------+ | candidates | | jobs | +--------------+ 2 rows in set (0.00 sec) mysql> desc candidates; +-------+---------------+------+-----+---------+------------------+ | Field | Type | Null | Key | Default | Extra | +-------+---------------+------+-----+---------+------------------+ | doc | json | YES | | NULL | | | _id | varbinary(32) | NO | PRI | NULL | STORED GENERATED | +-------+---------------+------+-----+---------+------------------+ 2 rows in set (0,00 sec)
  • 53. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Adding Documents • Several add() methods with single argument and varargs • Multiple add() can be chained until execute() or executeAsync() String candidate1 = "{"firstName": "Ann", "age": 21}"; String candidate2 = "{"firstName": "Peter", "age": 25}"; DbDoc doc1 = JsonParser.parseDoc(candidate1); DbDoc doc2 = JsonParser.parseDoc(candidate2); AddResult res = colC.add(doc1).add(doc2).execute(); res.getGeneratedIds().forEach(System.out::println); // 00005b8c23190000000000000001 // 00005b8c23190000000000000002 CompletableFuture<AddResult> cfRes = colC.add( "{"firstName": "John", "age": 52}", "{"firstName": "Mark", "age": 47}") .executeAsync(); res = cfRes.get(); res.getGeneratedIds().forEach(System.out::println); // 00005b8c23190000000000000003 // 00005b8c23190000000000000004 mysql> select _id from hr.candidates; +------------------------------+ | _id | +------------------------------+ | 00005b8c163e0000000000000001 | | 00005b8c163e0000000000000002 | | 00005b8c163e0000000000000003 | | 00005b8c163e0000000000000004 | +------------------------------+ 4 rows in set (0.00 sec)
  • 54. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Modifying Documents Result res = colC.modify("_id = '00005b8c23190000000000000003'") .set("lastName", "Jones").execute(); System.out.println(res.getAffectedItemsCount()); // 1 --> {"_id":"...","age":52,"firstName":"John","lastName":"Jones"} ModifyStatement modSt = colC.modify("_id = :id").bind("id", "00005b8c23190000000000000004"); res = modSt.set("lastName", "Muller").execute(); System.out.println(res.getAffectedItemsCount()); // 1 --> {"_id":"...","age":47,"firstName":"Mark","lastName":"Muller"} res = modSt.patch("{"lastName": "Miller", "languages": ["EN", "SP", "FR", "PT"]}").execute(); System.out.println(res.getAffectedItemsCount()); // 1 --> {"_id":"...","firstName":"Mark", // "languages":["EN","SP","FR","PT"], // "lastName":"Miller","age":47} mysql> select _id, doc->'$.firstName' fn, doc- >'$.lastName' ln from hr.candidates where _id = '00005b8c23190000000000000004'; +------------------------------+--------+----------+ | _id | fn | ln | +------------------------------+--------+----------+ | 00005b8c23190000000000000004 | "Mark" | "Miller" | +------------------------------+--------+----------+ 1 row in set (0.00 sec)
  • 55. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Fetching Documents • Method chaining allows to construct extensive search operations • A powerful expression syntax allows to filter results easily • Most of SQL’s SELECT clauses mapped to methods in this fluent pattern colC.find().execute().fetchAll().forEach(System.out::println); // {"_id":"...","age":21,"firstName":"Ann"} // {"_id":"...","age":25,"firstName":"Peter"} // {"_id":"...","age":52,"firstName":"John","lastName":"Jones"} // {"_id":"...","age":47,"firstName":"Mark","languages":["EN","SP","FR","PT"],"lastName":"Miller"} DocResult docRes = colC.find("LENGTH($.firstName) = :lenfn").bind("lenfn", 4).execute(); System.out.println(docRes.count()); docRes.forEach(System.out::println); // 2 // {"_id":"...","age":52,"firstName":"John","lastName":"Jones"} // {"_id":"...","age":47,"firstName":"Mark","languages":["EN","SP","FR","PT"],"lastName":"Miller"}
  • 56. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Fetching Documents(cont.) • Customizable projections with access to MySQL functions and operators • Capability to search deep in the documents structure and arrays colC.find("age >= 25").fields("_id AS id, UPPER(firstName) AS fn, age AS age") .sort("age DESC").execute().forEach(System.out::println); // {"age":52,"fn":"JOHN","id":"00005b8c23190000000000000003"} // {"age":47,"fn":"MARK","id":"00005b8c23190000000000000004"} // {"age":25,"fn":"PETER","id":"00005b8c23190000000000000002"} colC.find("age >= 25").fields(Expression.expr("{'id': _id, 'fn': UPPER(firstName), 'age': age}")) .sort("age DESC").execute().forEach(System.out::println); // {"age":52,"fn":"JOHN","id":"00005b8c23190000000000000003"} // {"age":47,"fn":"MARK","id":"00005b8c23190000000000000004"} // {"age":25,"fn":"PETER","id":"00005b8c23190000000000000002"} colC.find("'PT' IN languages").fields("_id AS id", "firstName AS fn").execute() .forEach(System.out::println); // {"fn":"Mark","id":"00005b8c23190000000000000004"}
  • 57. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Deleting Documents • Supports the same filtering expressions and bindings as the remaining CRUD operations • removeOne() is a single-shot operation, doesn’t require execute() colC.add("{"firstName": "Kevin", "lastName": "Kline", "age": 71}").execute(); colC.add("{"firstName": "Kevin", "lastName": "Kline", "age": 71}").execute(); colC.add("{"firstName": "Kevin", "lastName": "Kline", "age": 71}").execute(); long c = colC.removeOne("00005b8c23190000000000000005").getAffectedItemsCount(); System.out.println(c); // 1 -> {"_id":"...","age":71,"firstName":"Kevin","lastName":"Kline"} c = colC.remove("lastName = :ln").bind("ln", "Kline").execute().getAffectedItemsCount(); System.out.println(c); // 2 -> {"_id":"...","age":71,"firstName":"Kevin","lastName":"Kline"}
  • 58. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | X DevAPI Advanced Features 3
  • 59. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Locking Reads • Some modes require criteria based on indexes, otherwise the full collection may get locked Session session1 = sf.getSession("mysqlx://demouser:demopass@localhost/hr"); Session session2 = sf.getSession("mysqlx://demouser:demopass@localhost/hr"); session1.startTransaction(); Collection colC1 = session1.getDefaultSchema().getCollection("candidates"); colC1.find("_id = '00005b8c23190000000000000001'").fields("firstName AS fn", "age AS age") .lockShared().execute().forEach(System.out::println); // {"age":21,"fn":"Ann"} session2.startTransaction(); Collection colC2 = session2.getDefaultSchema().getCollection("candidates"); colC2.find().fields("firstName AS fn", "age AS age").lockExclusive(LockContention.SKIP_LOCKED) .execute().forEach(System.out::println); // {"age":25,"fn":"Peter"} // {"age":52,"fn":"John"} // {"age":47,"fn":"Mark"}
  • 60. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Creating Indexes Session session = sf.getSession("mysqlx://demouser:demopass@localhost/hr"); Collection colC = session.getDefaultSchema().getCollection("candidates"); colC.createIndex("byAge", "{"fields": [{"field": "$.age", "type": "INTEGER"}]}"); mysql> show create table hr.candidates; +------------+------------------------------------------------------------------------------------+ | Table | Create Table | +------------+------------------------------------------------------------------------------------+ | candidates | CREATE TABLE `candidates` ( `doc` json DEFAULT NULL, `_id` varbinary(32) GENERATED ALWAYS AS (json_unquote(json_extract(`doc`,_utf8mb4'$._id'))) STORED NOT NULL, `$ix_i_F177B50B40803DD7D3962E25071AC5CAA3D1139C` int(11) GENERATED ALWAYS AS (json_extract(`doc`,_utf8mb4'$.age')) VIRTUAL, PRIMARY KEY (`_id`), KEY `byAge` (`$ix_i_F177B50B40803DD7D3962E25071AC5CAA3D1139C`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci | +------------+------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
  • 61. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Working with Tables Table table = schema.getTable("colors"); InsertResult res = table.insert("color") .values("White").values("Blue").values("Red").execute(); System.out.println( res.getAffectedItemsCount() + "/" + res.getAutoIncrementValue()); // 3/1 Consumer<Row> rowPrinter = r -> System.out.println(r.getInt(0) + " -> " + r.getString(1)); table.select().execute().forEach(rowPrinter); // 1 -> White // 2 -> Blue // 3 -> Red table.select().orderBy("color").execute().forEach(rowPrinter); // 2 -> Blue // 3 -> Red // 1 -> White mysql> create table hr.colors (id int not null auto_increment primary key, color varchar(100)); Query OK, 0 rows affected (0.06 sec)
  • 62. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Working with Tables(cont.) table.update().where("color = 'Blue'").set("color", Expression.expr("CONCAT(color, 'ish')")) .execute(); table.select().execute().forEach(rowPrinter); // 1 -> White // 2 -> Blueish // 3 -> Red table.delete().where("color LIKE 'R%'").execute(); table.select().execute().forEach(rowPrinter); // 1 -> White // 2 -> Blueish table.delete().execute(); table.select().execute().forEach(rowPrinter); // nothing mysql> show tables from hr; +--------------+ | Tables_in_hr | +--------------+ | candidates | | colors | | jobs | +--------------+ 3 rows in set (0.00 sec)
  • 63. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Raw SQL SqlResult res = session.sql( "INSERT INTO colors (color) VALUES ('Black'), ('White')").execute(); System.out.println(res.getAffectedItemsCount() + "/" + res.getAutoIncrementValue()); // 2/1 res = session.sql("SELECT * FROM colors").execute(); res.forEach(rowPrinter); // 1 --> Black // 2 --> White res.getColumns().stream().map(Column::getType).forEach(System.out::println); // INT // STRING session.sql("DELETE FROM colors").execute(); session.sql("SELECT * FROM colors").execute().forEach(rowPrinter); // nothing session.sql("DROP TABLE colors").execute(); mysql> show tables from hr; +--------------+ | Tables_in_hr | +--------------+ | candidates | | jobs | +--------------+ 2 rows in set (0.00 sec)
  • 64. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Q & A
  • 65. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Connector/J and X DevAPI • https://dev.mysql.com/do c/connector-j/8.0/en/ • https://dev.mysql.com/do c/dev/connector-j/8.0/ • https://dev.mysql.com/do c/x-devapi-userguide/en/ Document store • https://dev.mysql.com/do c/refman/8.0/en/docume nt-store.html • https://dev.mysql.com/do c/refman/8.0/en/x- plugin.html • https://dev.mysql.com/do c/internals/en/x- protocol.html Support & resources • https://forums.mysql.com /list.php?39 • https://github.com/mysql /mysql-connector-j • https://mysqlcommunity. slack.com/messages/conn ectors • https://bugs.mysql.com/ Getting Help