Skip to main content

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 http://teiid.jboss.org/basics/ and go through a quick start example here https://docs.jboss.org/author/display/teiidexamples/Teiid+Quick+Starts

So, let's get started. 

  <resource-adapters>   
   <resource-adapter id="mongodb">   
    <module slot="main" id="org.jboss.teiid.resource-adapter.mongodb"/>   
    <transaction-support>NoTransaction</transaction-support>   
    <connection-definitions>   
     <connection-definition class-name="org.teiid.resource.adapter.mongodb.MongoDBManagedConnectionFactory"   
       jndi-name="java:/mongoDS"   
       enabled="true"   
       use-java-context="true"   
       pool-name="teiid-mongodb-ds">   
      <!-- 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>   
       -->    
     </connection-definition>   
    </connection-definitions>   
   </resource-adapter>   
  </resource-adapters>  

  • 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)  
         ) OPTIONS(UPDATABLE 'TRUE');  
       ]]> </metadata>  
   </model>  
 <vdb>  

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.

Thanks

Ramesh..

Comments

  1. Part 2 - http://teiid.blogspot.com/2015/06/sql-on-mongodb-using-teiid-part-2.html

    ReplyDelete

Post a Comment

Popular posts from this blog

Teiid Spring Boot 1.7.0 Released

Teiid Spring Boot version 1.7.0 to support Teiid 16.0 has been released. This release is mainly to support the Teiid's latest version.  In this release, the support for OpenAPI code generation based on VDB has been removed as there is no community interest and moreover it was at OpenAPI 2.0, and the industry has moved to 3.0 and beyond. There are no plans to further pursue this feature. VDB maven plugin was also removed, which was intended to be a replacement for the VDB importing feature was to be used when working on OpenShift, however, since it requires the Maven repository and does not completely represent the feature as defined on the WildFly based deployments this is also removed. You can still use the VDB import feature with Teiid Spring Boot, simply define the VDB with your "IMPORT DATABASE" statements and provide the additional files along with the main VDB file. During the start of the application, Teiid Spring Boot will load all the necessary DDL files for the

Teiid 8.13.3 Released

Teiid 8.13.3 is now  available .  In total 8.13.3 addresses 10 issues since 8.13.2: [ TEIID-4028 ] - adding salesforce-34 resource adapter does not work through the cli [ TEIID-4066 ] - Odata translator ClassNotFoundException: com.sun.ws.rs.ext.RuntimeDelegateImpl [ TEIID-4070 ] - Issues with resource adapters with api modules in wildfly [ TEIID-4089 ] - Teiid JDBC driver does not reset the update count when calling getMoreResults(int) [ TEIID-4093 ] - OData authentication fails with NPE when gss-pattern related properties are included in VDB [ TEIID-4096 ] - AssertionError with independent side of a dependent join that has an ordered limit [ TEIID-3050 ] - allow for more incremental insert with iterator [ TEIID-4075 ] - Netezza translator to support common table expressions [ TEIID-4098 ] - Always preserve columns order in google spreadsheets models [ TEIID-4046 ] - OData - $skip is beeing ignor

Access Teiid from node.js

Are you writing a "node.js" application and would like to access Teiid VDB from it? If "yes", this is currently possible using NPM package "pg" .  Since, Teiid supports the PG transport, you can use this PostgreSQL client for "node.js" for accessing the Teiid. For example if you have VDB called "northwind" deployed on your Teiid server, and it has table called "customers" and you are using default configuration such as user = 'user' password = 'user' host = 127.0.0.1 port = 35432 then you can use following to write simple access program to Teiid var pg = require('pg'); var connectionString = "pg://user:user@localhost:35432/northwind" pg.connect(connectionString, function(err, client) { client.query('SELECT CustomerID, ContactName, ContactTitle FROM Customers', function(err, result) { console.log(result.rows) }); }); If you want