JBoss.orgCommunity Documentation

Teiid - Scalable Information Integration

Teiid Quick Start Example

7.4

Legal Notice

Preface
1. What is Teiid?
1.1. What is a Virtual Database?
2. What is This Guide About?
1. Download
2. Portfolio Example Explained
2.1.
3. Setup Data Sources / Connection Factories
3.1. Install Data Sources / Connection Factories
3.2. Describe the CSV file and its contents
4. Building a VDB
4.1. Building Dynamic VDB
4.2. Dynamic VDB XML Structure
5. VDB Deployment
6. Connecting to a VDB through JDBC
6.1. Stand-alone Java Application Deployment
6.2. Testing Your Teiid Deployment

Note

Please read Federation Basics to understand different terminologies used, resources needed, and artifacts to be generated before developing a successful application. This example takes advantage of only a minimal set of features from Teiid for the sake of simplicity and time.

Commercial development support, production support, and training for Teiid is available through JBoss. Teiid is a Professional Open Source project and a critical component of the JBoss Enterprise Data Services Platform.

You need to download the binaries for Teiid . Note that there are three different artifacts are available for download.

  1. Teiid Source - contains all of the source code

  2. Teiid AdminShell - contains the admin client

  3. Teiid Runtime - contains the Teiid engine and required 3rd party dependencies

For this Quick Start, download and install JBoss AS 5.1.0. Then download the Teiid runtime and unzip the contents under any of the JBoss AS profiles, such as "default" or "all". The default profile is the typical installation location, for example "<jboss-install>/server/default". The Teiid runtime directory structure matches JBoss profiles directly - it is just an overlay.

In the "<jboss-install>/server/<profile>/lib" directory, you will find "teiid-7.4-client.jar", which is the main client binary jar file for Teiid. This jar file contains the Teiid's JDBC driver and data source driver jar's.

Note

JBoss AS 5.1 requires Java 6 to run.

Access to physical data sources such as Oracle, MS-SQL Server, DB2, and Sybase through Teiid relies upon the user supplying their own JDBC drivers in the deployment. Copy the JDBC driver files into "<jboss-install>/server/<profile>/lib" before you create any data sources.

The investor's portfolio example information is stored in a HSQL database and "current" stock prices are stored in a delimited text file. When the VDB is completed, a single query will cause Teiid to access the relational and non-relational sources, calculate the portfolio values, and return the results.


Note

HSQL database is used here since it is Open Source and comes with JBoss AS and is light-weight. You can substitute any other relational database, as long as you have a suitable JDBC driver. The schema file provided, and described below, is specific to HSQL, but can be easily converted for use with other databases.

A VDB can be built with either the Designer tool or through a simple XML file called a dynamic VDB. See the "dyanmicvdb-portolio" for an example dynamic VDB. For this example we will use the dynamic VDB. If you would like to use the Designer to build your VDB, check out the Designer examples. If you need to build any view layers using your source, you must use the Designer based approach to building the VDB. A sample Designer based VDB is available in the "teiid-examples/dynamicvdb-portfolio/PortfolioModel" directory.

This XML file defines a set of sources that can be accessed by the client application. A dynamic VDB does not yet allow for the creation of view layers. Below is the "dynamicvdb-portfolio" example vdb.

portfolio-vdb.xml (copy available in "teiid-examples/dynamicvdb-portfolio" directory)

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<vdb name="DynamicPortfolio" version="1">

    <description>A Dynamic VDB</description>
    
    <!-- 
      Setting to use connector supplied metadata. Can be "true" or "cached".  
      "true" will obtain metadata once for every launch of Teiid. 
      "cached" will save a file containing the metadata into 
      the deploy/<vdb name>/<vdb version/META-INF directory
    -->
    <property name="UseConnectorMetadata" value="true" />


    <!-- 
      Each model represents a access to one or more sources.
      The name of the model will be used as a top level schema name
      for all of the metadata imported from the connector.
    
      NOTE: Multiple model, with different import settings, can be bound to 
      the same connector binding and will be treated as the same source at
      runtime. 
    --> 
    <model name="MarketData">
        <!-- 
            Each source represents a translator and data source. There are 
            pre-defined translators, or you can create one. ConnectionFactories 
            or DataSources in JBoss AS they are typically defined using "xxx-ds.xml" files. 
        -->
        <source name="text-connector" translator-name="file" connection-jndi-name="java:marketdata-file"/>
    </model>

    <model name="Accounts">
        <!-- 
          JDBC Import settings 
          
          importer.useFullSchemaName directs the importer to drop the source 
          schema from the Teiid object name, so that the Teiid fully qualified name
          will be in the form of <model name>.<table name>
        -->
        <property name="importer.useFullSchemaName" value="false"/>
           
         <!--
            This connector is defined in the "portfoio-ds.xml" 
          -->
        <source name="hsql-connector" translator-name="hsql" connection-jndi-name="java:PortfolioDS"/>
    </model>

</vdb>        
    

At this point you have deployed Teiid and your VDB. Now it's time to connect the sample application to this VDB, issue SQL queries, and view the returned, integrated data. Note that this process is no different than connecting to any other JDBC source like Oracle.

Before you can make a JDBC connection to the Teiid VDB, add the Teiid's driver jar file to your application's classpath

${jboss-install}/server/${profile}/lib/teiid-7.4-client.jar

For a Java application to connect to a JDBC source, it needs a URL, user-id, and password. To connect to your VDB all you need is a URL and any additional optional properties that you would like to set. Teiid defaults to allowing the "user" as user with password as "user". Additional user accounts can be added. A JDBC connection can be obtained through the Teiid driver "org.teiid.jdbc.TeiidDriver" with the URL syntax of

jdbc:teiid:<VDB-Name>@mm(s)://<host name>:<port>

You can add optional properties at the end of the URL using a semi-colon(;) name=value format. For example

jdbc:teiid:<VDB-Name>@mm(s)://<host name>:<port>;autoCommitTxn=DETECT

Check out Client Developer's guide for all the optional connection properties in your URL. Here is sample code showing how to make JDBC connection.

public void execute() throws SQLException {
    String url = "jdbc:teiid:Portfolio@mm://localhost:31000";
    String sql = "select firstname, lastname from customer";
    
    Class.forName("org.teiid.jdbc.TeiidDriver");
    
    Connection connection;
    try{
        connection = DriverManager.getConnection(url, "user", "user");
        Statement statement = connection.createStatement();
        ResultSet results = statement.executeQuery(sql);
        while(results.next()) {
          System.out.println(results.getString(1));
          System.out.println(results.getString(2));
          ...
        }
        results.close();
        statement.close();
    } catch (SQLException e){
        e.printStackTrace();
        throw e;
    } finally {
        try{
          connection.close();
        }catch(SQLException e1){
          // ignore
        }              
    }
}

You can also use org.teiid.jdbc.TeiidDataSource to make connection in your Java application. For example, you can use following code fragment to make a connection to the VDB and issuing the query exactly same as in the above example

TeiidDataSource ds = new TeiidDataSource(); 
ds.setDatabaseName("Portfolio");
ds.setUser("user");
ds.setPassword("user");

Connection connection = ds.getConnection();
...

TeiidDataSource source also provides an option to set optional parameters using the "set" methods on the data source look. For all the allowable data source properties check out Client Developer's Guide.

The Teiid installation includes a simple Java class which demonstrates JDBC access of the deployed VDB. To execute this demonstration, follow these steps:

Depending on the VDB you deployed, see the relevant README file for example queries. If you are using a graphical client, such as SQuirreL, have a look at the metadata tree to see not only what is exposed by your VDB, but also the SYS schema tables.

If your application is Web based, you can create data source for your VDB using the above and treat it as any other JDBC source using org.teiid.jdbc.TeiidDataSource and assigning it a JNDI name. Refer to Client Developer's Guide deployment for more information on creating a DataSource.

Note

"embedded" mode is only available in versions of Teiid up to 6.2