Table Output transform Icon Table Output

Description

The Table Output transform inserts data into a relational database table.

To find which rows, columns and values the database would reject before writing, put Database value validation in front of this transform.

Table Output is equivalent to the DML operator INSERT.

This transform provides configuration options for target table and a lot of housekeeping and/or performance-related options such as Commit Size and Use batch update for inserts.

If you have a database table that has identity columns and you are inserting a record, as part of the insert, the JDBC driver will typically return the auto-generated key it used when performing the insert.

Options

Option Description

Transform name

Name of the transform, this name has to be unique in a single pipeline.

Connection

The database connection to which data is written

Target Schema

The name of the Schema for the table to write data to. This is important for data sources that allow for table names with periods in them.

Target table

The name of the table to which data is written.

Commit size

Use transactions to insert rows in the database table. Commit the connection every N rows if N is larger than zero (0); otherwise, don’t use transactions. (Slower)

Truncate table

Select if you want the table to be truncated before the first row is inserted into the table

Truncate on first row

Select if you want the table to be truncated when the transform receives it’s first row

Ignore insert errors

Makes Hop ignore all insert errors such as violated primary keys. A maximum of 20 warnings will be logged however. This option is not available for batch inserts.

Specify database fields

Enable this option to specify the fields in the Database fields tab. Otherwise all fields are taken into account by default.

Automatically update table structure

Automatically manages table structure based on the incoming data stream. When enabled, the table is created if it doesn’t exist, and additional options become available for column management. This option is incompatible with "Specify database fields".

Always drop and recreate table

Drops and recreates the table on every execution. Only available when "Automatically update table structure" is enabled.

Add columns

Adds columns to the table that exist in the incoming stream but not in the table. Only available when "Automatically update table structure" is enabled.

Drop non-existing columns

Drops columns from the table that don’t exist in the incoming stream. Warning: This will result in data loss for the dropped columns. Only available when "Automatically update table structure" is enabled.

Change column data types

Changes column data types to match the incoming stream. Warning: Columns are dropped and recreated, resulting in data loss. Only available when "Automatically update table structure" is enabled.

Partition data over tables

Use to split the data over multiple tables. For example instead of inserting all data into table SALES, put the data into tables SALES_200510, SALES_200511, SALES_200512, …​ Use this on systems that don’t have partitioned tables and/or don’t allow inserts into UNION ALL views or the master of inherited tables. The view SALES allows you to report on the complete sales:

sql
CREATE OR REPLACE VIEW SALES AS
SELECT * FROM SALES_200501
UNION ALL
SELECT * FROM SALES_200502
UNION ALL
SELECT * FROM SALES_200503
UNION ALL
SELECT * FROM SALES_200504
...

Use batch update for inserts

Enable if you want to use batch inserts. This feature groups inserts statements to limit round trips to the database. This is the fastest option and is enabled by default.

Rows per statement

With batch updates, the number of rows one INSERT statement carries, on a database type with a multi-row INSERT whose failed batches are recovered (see above). 100 for a new transform; a transform saved before this option existed inserts one row per statement. Variables are allowed. See Several rows per statement.

Is the name of the table defined in a field?

Use these options to split the data over one or more tables; the name of the target table is defined in the field you specify. For example if you store customer data in the field gender, the data might end up in tables M and F (Male and Female). There is an option to exclude the field containing the tablename from being inserted into the tables.

Field that contains name of table

When the option "Is the name of the table defined in a field?" is enabled, enter the field name to use here.

Store the table name field

When the option "Is the name of the table defined in a field?" is enabled, you can chose is this field should we written to the table or not.

Return auto-generated key

Enable if you want to get back the key that was generated by inserting a row into the table

Name of auto-generated key field

Specifies the name of the new field in the output rows that contains the auto-generated key

SQL

Generates the SQL to create the output table automatically

Several copies, partitions or bundles

When the transform runs in several copies on the local engine, or on the native Spark and Beam engines, Truncate table and Automatically update table structure happen once per run, before any row is written: on the local engine on the first copy, on the native Spark and Beam engines on the driver, before the job starts. The copies, Spark partitions or Beam bundles then only insert, so they don’t truncate each other’s rows or change the table at the same time. The table structure then follows the fields the previous transforms declare, as the SQL button does.

A single copy on the local engine still does both when the first row arrives, and the table structure follows the fields of that row.

Truncate on first row still truncates when the first row arrives, as only then is it known that rows arrive. In a transactional pipeline the truncate and table changes also wait for the first row, so they stay inside the transaction.

Batches and error handling

With Use batch update for inserts on, rows are sent to the database in batches of the commit size. When a row in a batch fails, what happened to the other rows of the batch depends on the database’s JDBC driver: some run the other rows, some stop at the failing row, and some lose the whole batch.

On a database whose driver’s behaviour has been measured, the transform uses it to work out which rows of a failed batch are in the database, and with an error hop attached it sends each row to exactly one stream: the main output for rows that were written, the error hop for rows that were refused. A row only appears on the main output after the database has committed it, and the rows keep their input order. Batches without errors run as before. On such a database the transform also keeps batching when an error hop is attached, where it otherwise writes row by row.

The databases measured so far, with their default connection options, are listed in the development manual under "Creating a dialect". On every other database, and when a connection option changes how the driver handles a failed batch, the transform handles a failed batch as it always did.

To go back to that behaviour on a measured database:

  • set When a batch fails, the driver on the Advanced tab of the connection to As before, or

  • set the variable HOP_DATABASE_BATCH_RECOVERY to LEGACY, for every connection.

The same option can name what the driver does, for a connection whose options change it.

When a failed batch is worked out this way, rows can be sent to the database more than once, so a failed batch can use up extra sequence values and fire non-transactional triggers again.

Several rows per statement

With batch updates and Rows per statement above 1, the rows of a batch are written as INSERT INTO t (a, b) VALUES (?, ?), (?, ?), …​, each table of Is the name of the table defined in a field? or of the partitioning with its own batch. This is done on the database types whose multi-row INSERT and failed batches have been checked: PostgreSQL, MySQL, Microsoft SQL Server (native), H2, DuckDB and Apache Derby. Oracle writes one row per statement: there an INSERT of several rows is no faster than the batch the driver already sends, and the more values it carries, the longer Oracle takes to parse it. A remainder shorter than the rows per statement is sent as a few statements of smaller sizes, and a database’s limits on rows or parameters per statement lower the rows per statement where needed. On MySQL a statement also has to fit in the server’s max_allowed_packet (or the connection’s lower maxAllowedPacket): a statement is cut short before its values would take it past that, so large rows go fewer to a statement. When a multi-row statement fails, its rows are run again one by one, so with an error hop exactly the refused rows go to the error hop (error code TOP0002) and the others are committed; without one the transform stops and the failed batch is rolled back. Work done earlier in the same transaction, such as Truncate table, is kept. A value that can’t be bound is found when the batch is sent, not when the row arrives; it then goes to the error hop with error code TOP001, or stops the transform, as with one row per statement.

Statement-level triggers fire once per statement and see all of its rows: every trigger on SQL Server, and FOR EACH STATEMENT triggers on PostgreSQL, H2 and Derby. A trigger that expects one row per INSERT needs Rows per statement 1. Row-level triggers (MySQL’s, and FOR EACH ROW triggers elsewhere) still fire once per row.

One row per statement is used, as before, without batch updates (also with Return auto-generated key, a commit size of 0 or a connection shared by a transactional pipeline), with Ignore insert errors, on a database without transactions, when two fields write the same column, on MySQL outside strict SQL mode or into a table without transactions (MyISAM, MEMORY, …​), or when the database type declares no multi-row INSERT or its limit on parameters leaves room for one row only. The detailed log says which, and how many rows per statement are used.

On PostgreSQL, the driver option reWriteBatchedInserts=true also applies to these statements: the driver folds a batch of multi-row INSERTs into larger multi-row INSERTs. The rows written are the same. When a row fails, though, the driver doesn’t say which statement of the batch caused it, so the rows of that batch are run again one by one, which takes more round trips than without the option.

To go back to one row per statement without changing any transform, tick Write one row per statement on the Advanced tab of the connection, or set the variable HOP_DATABASE_MULTI_ROW_DML to DISABLED for every connection. HOP_DATABASE_BATCH_RECOVERY=LEGACY does the same, because several rows per statement are only written where failed batches are recovered.

Samples

The samples project has transforms/tableoutput-basic.hpl, which empties the table tableoutput_basic of the hop-samples connection and writes 5 rows from a Data grid to it.