Skip to main content

Customize Table

DirectQuery allows to customize the table(s) retrieved from the remote database.

Typically, you would have discovered the table from the remote database, with a code similar to:

Table salesTable = session.discoverTable("TUTORIAL", "SALES");
Table productsTable = session.discoverTable("TUTORIAL", "PRODUCTS");

However, you might not be satisfied with the current data structure. Note that all the changes are done in the local data structure, and nothing is written in the remote database.

Customize a table

Each table can be customized.

Rename the table

For instance, you can rename the table:

salesTable = salesTable.renameLocally("Sales");
productsTable = productsTable.renameLocally("Products");

Select a subset of fields

The remote table might also contains some fields which are not relevant for the application. It's possible to select the fields you want:

productsTable =
productsTable.selectFields(List.of("PRODUCT_ID", "CATEGORY", "SIZE", "PURCHASE_PRICE", "BRAND"));

Set the keys

It is also possible to set the key(s) when it is not set properly.

salesTable = salesTable.setKeyFields(List.of("SALE_ID"));

Customize a field

Each field in a table can be customized as well. As a general note, do not forget to update the field in the table.

Rename the field

The field can be renamed:

Field productField = salesTable.getField("PRODUCT");
productField = productField.renameLocally("PRODUCT_ID");
salesTable = salesTable.updateField("PRODUCT", productField);

Change the type

The type can also be updated:

final Field priceField = productsTable.getField("PURCHASE_PRICE");
final Field updatedPriceField = priceField.withType(StandardTypes.FLOAT);
productsTable = productsTable.updateField("PURCHASE_PRICE", updatedPriceField);

Use a custom type

This is a bit more complex, and can be found in the dedicated guide.