Skip to content

Latest commit

 

History

History
157 lines (116 loc) · 5.11 KB

Readme.adoc

File metadata and controls

157 lines (116 loc) · 5.11 KB

Existing VDB Based Example

This guide walks you through the process of building an application that uses Teiid Spring Boot Starter and existing VDB you may have defined previously to build application and would like query based on it.

What you’ll build

You’ll build an application that loads the VDB supplied and provides a JDBC interface on it using Teiid.

What you’ll need

  • About 10 minutes

  • A favorite text editor or IDE

  • JDK 1.11 or later

  • Maven 3.0+

Build With Maven

First you set up a basic build script. You can use any build system you like when building apps with Spring, but the code you need to work with Maven is included here. If you’re not familiar with Maven, refer to Building Java Projects with Maven.

Go to Spring Initializer and type in "JPA" in dependencies and generate a project. Then open the generated code in your favorite IDE, and edit the pom.xml to add the below dependencies.

Otherwise, in a project directory of your choosing, create the following sub-directory structure; for example, with

mkdir -p src/main/java/example on *nix systems:

and create pom.xml file of your choosing and add following maven dependencies

spring-boot-starter-data-jpa
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
teiid-spring-boot-starter
<dependency>
   <groupId>org.teiid</groupId>
   <artifactId>teiid-spring-boot-starter</artifactId>
</dependency>

Since we are going to connect H2 database, add the JDBC driver dependency. You can replace this with database driver of your choosing.

h2
<dependency>
	<groupId>com.h2database</groupId>
	<artifactId>h2</artifactId>
	<scope>runtime</scope>
</dependency>

Define the Data Sources

In this example, first we need to define all the data sources that in play. To capture data source information, create the following Java class.

src/main/java/org/example/DataSources.java
package org.example;

@Configuration
public class DataSources {

    @ConfigurationProperties(prefix = "spring.datasource.mydb")
    @Bean
    public DataSource mydb() {
        return DataSourceBuilder.create().build();
    }
}
Note
Keep the data source property name and method name exactly SAME. From above example "mydb" in property and mydb() method, keep the names same, as additional properties will not be discovered otherwise.

We need to provide the corresponding configuration for this data source. In "application.properties" file, define your configuration similar to

src/main/resources/application.properties
spring.datasource.mydb.jdbc-url=jdbc:h2:mem:mydb;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS ACCOUNTS
spring.datasource.mydb.username=sa
spring.datasource.mydb.password=sa
spring.datasource.mydb.driver-class-name=org.h2.Driver
spring.datasource.mydb.platform=mydb

repeat above step for all the data sources you are going to use in the vdb. Note the bean name, as you need to use that name as the SERVER name in the VDB source definitions. For example, see the line


CREATE SERVER mydb FOREIGN DATA WRAPPER h2; ---

from the example customer-vdb.ddl file defined.

Copy the VDB file

Copy your -vdb.ddl file into "src/main/resources" directory, and add below property in application.properties this tells the application which VDB file to load.

teiid.vdb-file=customer-vdb.ddl
Note
you can omit the above teiid.vdb-file property, however the vdb file needs have a explit name as teiid.ddl and must be available in the classpath. If you need reference to the DDL based VDB syntax see link:http://teiid.github.io/teiid-documents/master/content/reference/ddl_deployment_mode.html
Note
If you have a .vdb or -vdb.xml based VDB files, you can use vdb migration utility to convert into a .DDL file to use. But currently we support .vdb, -vdb.xml or .ddl formats. But .vdb and -vdb.xml formats will be deprecated.

Let’s wire this up and see what it looks like!

Create an Application class

Here you create an Application class with all the components.

src/main/java/org/example/Application.java
@SpringBootApplication
public class Application implements CommandLineRunner {

    @Autowired
    private JdbcTemplate jdbcTemplate;

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args).close();
	}

    @Override
    public void run(String... args) throws Exception {
        jdbcTemplate.query(<your-query>);
    }
}

Now when you are ready to build the application

mvn clean install

and execute your application

java -jar target/spring-vdb-example-{version}.jar

If you want to expose this VDB through OData, add following dependency to your pom.xml file.

<dependency>
    <groupId>org.teiid</groupId>
    <artifactId>spring-odata</artifactId>
</dependency>

and re-run the build, and then run, after that on port 8080 the OData REST API can be queried.