When dealing with an already loaded table where the goal is to append or update records, as opposed to the LOAD operator which is optimized for bulk loading into empty tables, one would opt for the UPDATE operator or the STREAM operator. Between these two, the STREAM operator offers a more flexible solution suited for high-speed loading while also allowing for inserting, updating, and deleting data in a single operation. It's a suitable choice for scenarios where the table is not empty and might require upsert operations (insert or update).
Here's an adjusted version of the TPT script using the STREAM operator to append more data to an already loaded target table:
DEFINE JOB append_data_stream_job
DESCRIPTION 'Append/Update data into Teradata target table using STREAM operator'
(
DEFINE SCHEMA source_schema
DESCRIPTION 'Source data schema'
(
Col1 INTEGER,
Col2 VARCHAR(100),
Col3 DATE FORMAT 'YYYY-MM-DD'
);
DEFINE OPERATOR data_connector_reader
TYPE DATACONNECTOR PRODUCER
SCHEMA source_schema
ATTRIBUTES
(
VARCHAR FileName = 'your_data_file.txt', -- Specify your data file name
VARCHAR DirectoryPath = '/path/to/your/file/', -- Specify the directory of your file
VARCHAR Format = 'DELIMITED',
VARCHAR TextDelimiter = '|', -- Assuming "|" to be your delimiter
VARCHAR OpenMode = 'Read',
INTEGER SkipRows = 1 -- Assuming a header row exists that you need to skip
);
DEFINE OPERATOR data_stream_operator
TYPE STREAM
SCHEMA source_schema
ATTRIBUTES
(
VARCHAR UserName = 'your_username', -- Your Teradata username
VARCHAR UserPassword = 'your_password', -- Your Teradata password
VARCHAR TargetTable = 'your_target_table', -- Your target table in Teradata
VARCHAR ErrorTable1 = 'err_table1', -- Specify your error table 1 for handling insert errors
VARCHAR ErrorTable2 = 'err_table2', -- Specify your error table 2 for handling update errors
INTEGER MaxSessions = 20, -- Maximum number of sessions
INTEGER MinSessions = 2, -- Minimum number of sessions
VARCHAR LogTable = 'your_stream_log', -- Table for logging stream operations
VARCHAR TdpId = 'your_TDPID' -- Your Teradata system name or IP
);
STEP load_append_step
(
APPLY
('INSERT INTO your_target_table (Col1, Col2, Col3) VALUES (:Col1, :Col2, :Col3);')
TO OPERATOR (data_stream_operator[1])
SELECT * FROM OPERATOR (data_connector_reader[1]);
);
);
Important Considerations:
- The
STREAM operator effectively handles both insert and update operations. By using an appropriate UPSERT operation (e.g., MERGE INTO) in the APPLY string, you can also update existing rows based on your business logic. Make sure that your target table and operations support such actions.
- Ensure to replace placeholders like 'your_data_file.txt', 'your_username', 'your_password', 'your_target_table', 'err_table1', 'err_table2', 'your_stream_log', and 'your_TDPID' with actual values applicable to your scenario.
- Adjust the
MaxSessions and MinSessions for optimal concurrency based on your system's capabilities and workload.
- This example assumes a basic insert operation in the
APPLY statement. Depending on your needs (e.g., updating existing records), change this statement to use Teradata's UPSERT logic or another appropriate SQL operation.
Using the STREAM operator for appending to or updating an already loaded table provides a balance between performance and flexibility, making it suitable for a wide range of data integration scenarios in Teradata environments.