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 8.11 Beta1 and 8.10.1 Released

Teiid 8.11 Beta1 is now available from the  downloads  and maven.  Feature highlights since Alpha2 include: TEIID-3434 More caching control over ttls in the result set cache down to the schema/table level. TEIID-3412 MS Access support via the UCanAccess driver. The UCanAccess support is necessary for those running on Java 1.8 as the JDBC ODBC bridge has been removed from the JRE. The waiting continues on EAP 6.4 Alpha1 - it still should be available shortly and should be the platform target for Teiid 8.11 Beta2. Of course, let us know if you find any issues with these early releases.  There's still plenty of time to get fixes into the final release. Teiid 8.10.1 is also available.  It addresses 5 important issues discovered since 8.10 was released: [ TEIID-3409 ] - PostgreSQLExecutionFactory TranslatorProperty annotation in wrong place [ TEIID-3437 ] - Inconsistencies with row count handling [ TEIID-3438 ] - Null value returned from BlobImpl

Tech Tip: Teiid SQL Language MAKEDEP Hint Explained

In this article I will explain what a MAKEDEP hint is, how and when, why it should be used in Teiid. What: MAKEDEP is query hint.  When a query hint is defined in the SQL query it influences the Teiid query planner to optimize the query in a way that is driven by the user. MAKEDEP means "make this as a dependent join". What is a Dependent Join? For example if we have query like: SELECT * FROM X INNER JOIN Y ON X.PK = Y.FK Where the data for X, and Y are coming from two different sources like Oracle and WebService in Teiid, so in relational algebra you can represent above query as Here the result tuples from node X and node Y are being simultaneously fetched by Teiid query engine, then it joins the both the results inside Teiid engine based on the specified X.PK = Y.PK condition and returns the filtered resulted to the user. simple.. Now, what if, if X table has 5 rows and Y table has 100K rows? In order to do the JOIN naively Teiid need sto read all the 5

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