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.
You’ll build an application that loads the VDB supplied and provides a JDBC interface on it using Teiid.
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
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<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.
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
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.
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
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 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!
Here you create an Application class with all the components.
@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.