You can override and implement the method getMetadataProcessor(), in order to expose the metadata about the source for use in Dynamic VDBs. This defines the tables, column names, procedures, parameters, etc. for use in the query engine. This method is used by Designer tooling when the Teiid Connection importer is used. A sample MetadataProcessor may look like this:
public class MyMetadataProcessor implements MetadataProcessor<Connection> {
public void process(MetadataFactory mf, Connection conn) {
Object somedata = connection.getSomeMetadata();
Table table = mf.addTable(tableName);
Column col1 = mf.addColumn("col1", TypeFacility.RUNTIME_NAMES.STRING, table);
column col2 = mf.addColumn("col2", TypeFacility.RUNTIME_NAMES.STRING, table);
//add a pushdown function that can also be evaluated in the engine
Method method = ...
Function f = mf.addFunction("func", method);
//add a pushdown aggregate function that can also be evaluated in the engine
Method aggMethod = ...
Function af = mf.addFunction("agg", aggMethod);
af.setAggregateAttributes(new AggregateAttributes());
...
}
}
public class MyMetadataProcessor implements MetadataProcessor<Connection> {
public void process(MetadataFactory mf, Connection conn) {
Object somedata = connection.getSomeMetadata();
Table table = mf.addTable(tableName);
Column col1 = mf.addColumn("col1", TypeFacility.RUNTIME_NAMES.STRING, table);
column col2 = mf.addColumn("col2", TypeFacility.RUNTIME_NAMES.STRING, table);
//add a pushdown function that can also be evaluated in the engine
Method method = ...
Function f = mf.addFunction("func", method);
//add a pushdown aggregate function that can also be evaluated in the engine
Method aggMethod = ...
Function af = mf.addFunction("agg", aggMethod);
af.setAggregateAttributes(new AggregateAttributes());
...
}
}
Copy to ClipboardCopied!Toggle word wrapToggle overflow
If your MetadataProcessor needs external properties that are needed during the import process, you can define them on MetadataProcessor. For example, to define a import property called "Column Name Pattern", which can be used to filter which columns are defined on the table, you can add it like this:
@TranslatorProperty(display="Column Name Pattern", category=PropertyType.IMPORT, description="Pattern to derive column names")
public String getColumnNamePattern() {
return columnNamePattern;
}
public void setColumnNamePattern(String columnNamePattern) {
this.columnNamePattern = columnNamePattern;
}
@TranslatorProperty(display="Column Name Pattern", category=PropertyType.IMPORT, description="Pattern to derive column names")
public String getColumnNamePattern() {
return columnNamePattern;
}
public void setColumnNamePattern(String columnNamePattern) {
this.columnNamePattern = columnNamePattern;
}
Copy to ClipboardCopied!Toggle word wrapToggle overflow
Note the category type. The configuration property defined in the previous section is different from this one. Configuration properties define the runtime behavior of translator, where as "IMPORT" properties define the metadata import behavior, and aid in controlling what metadata is exposed by your translator.
These properties can be automatically injected through "import" properties set through Designer when using the "Teiid Connection" importer or the properties can be defined under the model construct in the vdb.xml file, like
Copy to ClipboardCopied!Toggle word wrapToggle overflow
There may be times when implementing a custom translator, the built in metadata about your schema is not enough to process the incoming query due to variance of semantics with your source query. To aid this issue, Teiid provides a mechanism called "Extension Metadata", which is a mechanism to define custom properties and then add those properties on metadata object (table, procedure, function, column, index etc.). For example, in my custom translator a table represents a file on disk. I could define a extension metadata property like this:
public class MyMetadataProcessor implements MetadataProcessor<Connection> {
public static final String NAMESPACE = "{http://my.company.corp}";
@ExtensionMetadataProperty(applicable={Table.class}, datatype=String.class, display="File name", description="File Name", required=true)
public static final String FILE_PROP = NAMESAPCE+"FILE";
public void process(MetadataFactory mf, Connection conn) {
Object somedata = connection.getSomeMetadata();
Table table = mf.addTable(tableName);
table.setProperty(FILE_PROP, somedata.getFileName());
Column col1 = mf.addColumn("col1", TypeFacility.RUNTIME_NAMES.STRING, table);
column col2 = mf.addColumn("col2", TypeFacility.RUNTIME_NAMES.STRING, table);
}
}
public class MyMetadataProcessor implements MetadataProcessor<Connection> {
public static final String NAMESPACE = "{http://my.company.corp}";
@ExtensionMetadataProperty(applicable={Table.class}, datatype=String.class, display="File name", description="File Name", required=true)
public static final String FILE_PROP = NAMESAPCE+"FILE";
public void process(MetadataFactory mf, Connection conn) {
Object somedata = connection.getSomeMetadata();
Table table = mf.addTable(tableName);
table.setProperty(FILE_PROP, somedata.getFileName());
Column col1 = mf.addColumn("col1", TypeFacility.RUNTIME_NAMES.STRING, table);
column col2 = mf.addColumn("col2", TypeFacility.RUNTIME_NAMES.STRING, table);
}
}
Copy to ClipboardCopied!Toggle word wrapToggle overflow
The @ExtensionMetadataProperty defines the following metadata that you can define about your property
applicable: Metadata object this is applicable on. This is array of metadata classes like Table.class, Column.class.
datatype: The java class indicating the data type
display: Display name of the property
description: Description about the property
required: Indicates if the property is a required property
When you define an extension metadata property like above, during the runtime you can obtain the value of that property. If you get the query object which contains 'SELECT * FROM MyTable', MyTable will be represented by an object called "NamedTable".
for (TableReference tr:query.getFrom()) {
NamedTable t = (NameTable) tr;
Table table = t.getMetadataObject();
String file = table.getProperty(FILE_PROP);
..
}
for (TableReference tr:query.getFrom()) {
NamedTable t = (NameTable) tr;
Table table = t.getMetadataObject();
String file = table.getProperty(FILE_PROP);
..
}
Copy to ClipboardCopied!Toggle word wrapToggle overflow
Now you have accessed the file name you set during the construction of the Table schema object, and you can use this value however you seem feasible to execute your query. With the combination of built in metadata properties and extension metadata properties you can design and execute queries for a variety of sources.
We help Red Hat users innovate and achieve their goals with our products and services with content they can trust. Explore our recent updates.
Making open source more inclusive
Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.
About Red Hat
We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.