Sunday, June 7, 2015

SQL on MongoDB using Teiid - Part 1

In this article series, I will showcase how you can use SQL based queries with MongoDB. In general there seems to be resurgence of SQL based access to all NoSQL based stores in the market space, take for example Hive, Impala, Spark, Apache Drill etc. The main reason for this shift is there are abundant amount of talent pool out there for SQL based developers, and even today (and years to come) our strong dependence on the relational stores for the enterprise data. I do not see either of them fading away any time soon. So utilizing the known skills on new types of data stores will save you lot of time and provide better integration with rest of your enterprise applications.

I know there are plenty of folks offering SQL based access to MongoDB, why you should choose Teiid?
  • Teiid provides full ANSI compatible SQL based access to MongoDB. This includes full SQL-92, and most SQL-99 and SQL-2003 support.
  • Provides JDBC/ODBC access to execute SQL queries.
  • Provides ODATA based access to MongoDB.
  • Provides framework to easily create custom REST based services over MongoDB or any other data source
  • Teiid is a data virtualization system, that means you can integrate data from MongoDB with other enterprise sources like Orcale, DB2, SQL Server etc.
  • Complex nested document support up to three+ levels of embedded documents. I have not seen any other project support more than one level of nested documents.
  • Supports full range of SELECT, INSERT, UPDATE and DELETE calls.
  • Array support, Blob Support, GEO functions support.  
  • A JBoss community Open Source project.
Depending upon your architecture and needs there are different ways you use Teiid with MongoDB
  • Standalone Teiid server
  • Embedded Teiid
  • Teiid Docker 
However, in all the above are only different deployment scenarios, as per defining your interaction with MongoDB in-terms of SQL is exactly same. In this article I will focus on Standalone Teiid Server. I will not go in any details about installation of Teiid or MongoDB. Also, I assume user already has familiarity with Teiid concepts, if not please read and go through a quick start example here

So, let's get started. 

   <resource-adapter id="mongodb">   
    <module slot="main" id="org.jboss.teiid.resource-adapter.mongodb"/>   
     <connection-definition class-name="org.teiid.resource.adapter.mongodb.MongoDBManagedConnectionFactory"   
      <!-- MongoDB server list (host:port[;host:port...]) -->   
      <config-property name="RemoteServerList">localhost:27017</config-property>   
      <!-- Database Name in the MongoDB -->   
      <config-property name="Database">test</config-property>   
        Uncomment these properties to supply user name and password   
       <config-property name="Username">user</config-property>   
       <config-property name="Password">user</config-property>   

  • The above will create a JCA connection pool to the MongoDB. Save it and start the JBoss EAP.
  • Now it is time to create VDB. The following shows a very simple VDB

 <vdb name="nothwind" version="1">  
   <model name="northwind">  
     <source name="local" translator-name="mongodb" connection-jndi-name="java:/mongoDS"/>  
       <metadata type="DDL"><![CDATA[  
         CREATE FOREIGN TABLE Customer (  
           customer_id integer,  
           FirstName varchar(25),  
           LastName varchar(25)  
       ]]> </metadata>  

Now deploy this VDB using either web-console or CLI into Teiid. Make sure it is deployed in ACTIVE state. Now using any JDBC client like SquirreL, and issue SQL queries like

 INSERT INTO Customer(customer_id, FirstName, LastName) VALUES (1, 'John', 'Doe');  

This will produce a document like below in the MongoDB database.

  _id: ObjectID("509a8fb2f3f4948bd2f983a0"),  
  customer_id: 1,  
  FirstName: "John",  
  LastName: "Doe"  

If you issue a query like

 SELECT * FROM Customer  

you will see the result like

 customer_id  FirstName  LastName  
 1           John       Doe  

This is a illustration of what it takes to use SQL on MongoDB using Teiid. But we only scratched SQL capabilities. In the next article I will show case, how to create complex nested documents and resulting SQL/DDL for it.



1 comment:

  1. Part 2 -