Programming Against Hybrid Databases with Java Handling SQL and NoSQL Brian Hughes IBM 1
Acknowledgements and Disclaimers Availability. References in this presentation to IBM products, programs, or services do not imply that they will be available in all countries in which IBM operates. The workshops, sessions and materials have been prepared by IBM or the session speakers and reflect their own views. They are provided for informational purposes only, and are neither intended to, nor shall have the effect of being, legal or other guidance or advice to any participant. While efforts were made to verify the completeness and accuracy of the information contained in this presentation, it is provided AS-IS without warranty of any kind, express or implied. IBM shall not be responsible for any damages arising out of the use of, or otherwise related to, this presentation or any other materials. Nothing contained in this presentation is intended to, nor shall have the effect of, creating any warranties or representations from IBM or its suppliers or licensors, or altering the terms and conditions of the applicable license agreement governing the use of IBM software. All customer examples described are presented as illustrations of how those customers have used IBM products and the results they may have achieved. Actual environmental costs and performance characteristics may vary by customer. Nothing contained in these materials is intended to, nor shall have the effect of, stating or implying that any activities undertaken by you will result in any specific sales, revenue growth or other results. Copyright IBM Corporation 2015. All rights reserved. U.S. Government Users Restricted Rights Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. IBM, the IBM logo, ibm.com are trademarks or registered trademarks of International Business Machines Corporation in the United States, other countries, or both. If these and other IBM trademarked terms are marked on their first occurrence in this information with a trademark symbol ( or TM), these symbols indicate U.S. registered or common law trademarks owned by IBM at the time this information was published. Such trademarks may also be registered or common law trademarks in other countries. A current list of IBM trademarks is available on the Web at Copyright and trademark information at www.ibm.com/legal/copytrade.shtml Other company, product, or service names may be trademarks or service marks of others. 2
New Types Tables with NoSQL Mongo BSON Classes JDBC Demo database SQL and Strings New data types (BSON, JSON???) Mongo-Java Wire Listener Collections 3
New Type JSON JSON (Javascript object notation) A lightweight data-interchange format Easy to read and write Informix server has JSON data type JDBC driver has no special support for JSON Considered a text string from the driver Need 3 rd party libraries to parse into usable objects 4
New Type BSON BSON - a binary form for representing simple data structures and associative arrays Short for Binary JSON BSON is efficient for data transfers, space and traversability Most new Informix functions/functionality is around supporting BSON JDBC driver has first class support for BSON as an extended type 5
Tables with NoSQL Easy to add JSON/BSON types to existing or new tables Create them as a normal columns CREATE TABLE names (id bigserial, info JSON) CREATE TABLE names (id bigserial, info BSON) 6
Mongo BSON Classes BasicBSONObject Base class for holding BSON data BSON/Java data can represented as Maps of objects Map<String, Object> specifically Extends LinkedHashMap BasicBSONList Represents arrays in BSON structures No real value Extends List<Object> so casting to a List works just fine BSONObject High level interface to represent a BSON object 7
Informix JDBC BSON Informix JDBC driver has bundled a number of BSON classes Subset of Mongo s org.bson package Missing DBObject Informix JDBC Driver Mongo Driver 8
Native Java Mapping Informix JDBC driver maps BSON objects to the IfxBSONObject class IfxBSONObject is a hybrid class Holds both byte [] representation of a BSON as well as the Mongo BasicBSONObject structure 9
Demo Database Basic Structure for JDBC A simple database using UTF-8 10
Demo Database (cont) A simple data with basic and NoSQL data types Includes varchar, json and bson columns 11
Handling NoSQL as Strings Using SQL casts we can treat our NoSQL objects as strings for inserting and querying 12
SQL Statement with NoSQL as Strings Final resulting string is difficult to read and long Could use a Prepared statement with setstring() to simplify 13
Querying Data as Strings Both JSON and BSON types can be viewed as Strings JSON maps to a native Java String BSON maps to a IfxBSONObject which has a sane tostring() method 14
Getting back JSON/BSON as a String 15
Getting back JSON/BSON as a String 16
Demo JSON/BSON with JDBC and Strings 17
Query Data with Native Types JSON is a String BSON comes back as an IfxBSONObject Informix JDBC UDT for the extended type BSON Cast getobject() to IfxBSONObject 18
IfxBSONObject A UDT, a BSONObject, and a SQLData object Allows for easy manipulation of BSON as well as insertion/retrieval the from JDBC driver 19
Processing an IfxBSONObject For most operations, you can use the object directly Sometimes you might want to convert to a BasicBSONObject which directly extends LinkedHashMap 20
Handling Complex BSON BSON objects in Java can hold objects, lists and other BSON objects (subdocuments) 21
Handling Complex BSON Resulting JSON document 22
Demo JSON/BSON with JDBC Native Types 23
Mongo Drivers If you work heavily in NoSQL you might prefer the use of Mongo Drivers Requires the setup of the Informix Wire Listener to understand Mongo protocols Allows you to create collections of documents easily and index 24
Index Creation on BSON Creating indexes on BSON documents in a collection is easy Basic Mongo create index Informix extension for indexing specific data types 25
Checking for Indices used Mongo API has an explain() method Ties directly into Informix optimizer plan 26
Getting Relational data from Mongo API s You can point to a relational table in a database and query with Mongo s API s 27
Execute Complex SQL via Mongo API s Using SQL pass through you can execute pure SQL via the Mongo API s secure.sql.passthrough=true Uses the virtual collection system.sql and the custom command $sql 28
Demo BSON with Mongo Java Drivers 29
Links http://www- 01.ibm.com/support/knowledgecenter/SSGU8 G_12.1.0/com.ibm.json.doc/json.htm http://mongodb.github.io/mongo-javadriver/2.13/getting-started/quick-tour/ 30
Questions? 31