fastload() | Teradata Package for Python - fastload() - Teradata Package for Python

Teradata® Package for Python User Guide

Deployment
VantageCloud
VantageCore
Edition
Enterprise
IntelliFlex
VMware
Product
Teradata Package for Python
Release Number
20.00
Published
March 2024
Language
English (United States)
Last Update
2024-04-09
dita:mapPath
nvi1706202040305.ditamap
dita:ditavalPath
plt1683835213376.ditaval
dita:id
rkb1531260709148
Product Category
Teradata Vantage

The fastload() function writes records from a Pandas DataFrame to Vantage using Fastload, and can be used to quickly load large amounts of data in an empty table on Vantage.

FastLoad opens multiple data transfer connections to the database. The number of data transfer sessions can be set using argument open_sessions. If this argument is not set, by default, data transfer sessions opened by teradataml is the smaller of 8 and the number of available AMPs in Vantage.

Teradata recommends using fastload() function when number of rows in the Pandas DataFrame is greater than 100,000 for better performance. To insert lesser rows, you can use the copy_to_sql() function for optimized performance. The data is loaded in batches.

  • FastLoad API cannot load duplicate rows in the DataFrame if the table is a MULTISET table with Primary Index.
  • FastLoad API does not support all Analytics Database data types.

    For example, target table having BLOB and CLOB data type columns cannot be loaded.

  • If there are any incorrect rows due to constraint violations, data type conversion errors, etc., FastLoad protocol ignores those rows and inserts all valid rows.
  • Rows in the DataFrame that failed to get inserted are categorized into errors and warnings by FastLoad protocol and these errors and warnings are stored into respective error and warning tables by FastLoad API.
  • If 'save_errors' argument is set to True, the names of error and warning tables are shown once the fastload operation is complete. These tables will be persisted using copy_to_sql.

See the FastLoad section of https://pypi.org/project/teradatasql/ for more information about FastLoad protocol through teradatasql driver.

Required Arguments:
  • df: Specifies the pandas or teradataml DataFrame object to be saved in Vantage.
  • table_name: Specifies the name of the table to be created in Vantage.
Optional Arguments:
  • schema_name: Specifies the name of the SQL schema in Vantage to write to.

    The default value is None, which means use the default Vantage schema.

  • if_exists: Specifies the action to take when table already exists in the database.
    Possible values are:
    • 'fail': If table exists, do nothing;
    • 'replace': If table exists, drop it, recreate it, and insert data;
    • 'append': If table exists, insert data. Create if does not exist.

    The default value is 'replace'.

  • index: Specifies whether to save pandas DataFrame index as a column or not. Possible values are True or False.

    The default value is False.

  • index_label: Specifies the column labels for pandas DataFrame index columns.

    The default value is None.

  • primary_index: Specifies the column(s) to use as primary index while creating tables in Vantage. When None (default value), No Primary Index (NOPI) tables are created.
    For example:
    • primary_index = 'my_primary_index'
    • primary_index = ['my_primary_index1', 'my_primary_index2', 'my_primary_index3']
  • types: Specifies required data types for requested columns to be saved in Vantage.

    The default value is None.

    This argument accepts a dictionary of column names and their required teradatasqlalchemy types as key-value pairs (({column_name1: type_value1, ... column_nameN: type_valueN})), allowing to specify a subset of the columns of a specific type.

    • When only a subset of all columns are provided, the rest are defaulted to appropriate types.
    • When types argument is not provided, all column types are appropriately assigned and defaulted. The column types are assigned as listed in the following table.
      pandas/NumPy Type teradatasqlalchemy Type
      int32 INTEGER
      int64 BIGINT
      bool BYTEINT
      float32/float64 FLOAT
      datetime64/datatime64[ns] TIMESTAMP
      datatime64[ns,time_zone] TIMESTAMP(timezone=True)
      Any other data type VARCHAR(configure.default_varchar_size)
    • This argument does not have any effect when the table specified using table_name and schema_name exists and if_exists = 'append'.
  • batch_size: Specifies the number of rows to be loaded in a batch. The value of batch_size must be a positive integer.

    For better performance, Teradata recommends batch size to be at least 100,000.

    If this argument is None, which is the default value, there are two cases based on the number of rows, say N, in the dataframe 'df':
    • If N is greater than 100,000, the rows are divided into batches of equal size with each batch having at least 100,000 rows (except the last batch which might have more rows).
    • If N is less than 100,000, the rows are inserted in one batch after notifying the user that insertion happens with degradation of performance.

    If this argument is not None, the rows are inserted in batches of size given in the argument, irrespective of the recommended batch size.

    The last batch will have rows less than the batch size specified, if the number of rows is not an integral multiples of the argument batch_size.

  • save_errors: Specifies whether to persist the error and warning information in Vantage or not.
    • If set to False, which is the default value, error and warnings are not persisted as tables.
    • If set to True, the error and warnings information are persisted and names of error and warning tables are returned. Otherwise, the function returns None for the names of the tables.
  • open_sessions: Specifies the number of Teradata data transfer sessions to be opened for fastload operation.

    If this argument is not provided, the default value is the smaller of 8 or the number of AMPs available.

    See the FastLoad section of https://pypi.org/project/teradatasql/ for additional information about number of Teradata data transfer sessions opened during fastload.

Returns

FastLoad returns a dict containing the following attributes:
  • errors_dataframe: It is a Pandas DataFrame containing error messages thrown by fastload.

    DataFrame is empty if there are no errors.

  • warnings_dataframe: It is a Pandas DataFrame containing warning messages thrown by fastload.

    DataFrame is empty if there are no warnings.

  • errors_table: Name of the table containing errors.

    It is None, if argument save_errors is set to 'False'.

  • warnings_table: Name of the table containing warnings.

    It is None, if argument save_errors is set to 'False'.

Minimum version requirements for fastload()

teradatasql version 16.20.00.48 or later is required for fastload() function to work properly. If you have a lower version installed, then teradatasql raises OperationalError and fastload() call ends with the following error:
[Teradata Database] [Error 3706] Syntax error: expected something between the beginning of the request and the word 'teradata_require_fastloadINSERT'.
pandas version 0.24 or later is required for fastload() API to work properly. If you have a lower version, fastload() API will fail with following error:
AttributeError: 'Index' object has no attribute 'to_list'
Install pandas >= 0.24 to solve this issue.

Example Setup

>>> from teradataml.dataframe.fastload import fastload
>>> from teradatasqlalchemy.types import *
>>> import pandas as pd
>>> df = {'emp_name': ['A1', 'A2', 'A3', 'A4'],
         'emp_sage': [100, 200, 300, 400],
         'emp_id': [133, 144, 155, 177],
         'marks': [99.99, 97.32, 94.67, 91.00]
          }
>>> pandas_df = pd.DataFrame(df)

Example 1: Save a Pandas DataFrame with default signature

>>> fastload(df = pandas_df, table_name = 'my_table')

Example 2: Save a Pandas DataFrame with primary_index

>>> pandas_df = pandas_df.set_index(['emp_id'])
>>> fastload(df = pandas_df, table_name = 'my_table_1', primary_index='emp_id')

Example 3: Save a Pandas DataFrame with index and primary_index

>>> fastload(df = pandas_df, table_name = 'my_table_2', index=True, primary_index='index_label')

Example 4: Save a Pandas DataFrame with types, appending to an existing table

>>> fastload(df = pandas_df, table_name = 'my_table_3', schema_name = 'alice', index = True, index_label = 'my_index_label', primary_index = ['emp_id'], if_exists = 'append', types = {'emp_name': VARCHAR, 'emp_sage':INTEGER, 'emp_id': BIGINT, 'marks': DECIMAL})

Example 5: Save a Pandas DataFrame using levels in index of type MultiIndex, replacing an existing table

>>> pandas_df = pandas_df.set_index(['emp_id', 'emp_name'])
>>> fastload(df = pandas_df, table_name = 'my_table_4', schema_name = 'alice', index = True, index_label = ['index1', 'index2'], primary_index = ['index1'], if_exists = 'replace'

Example 6: Save a Pandas DataFrame by opening given number of Teradata data transfer sessions

This example saves a Pandas DataFrame by opening two Teradata data transfer sessions.

>>> fastload(df = pandas_df, table_name = 'my_table_5', open_sessions = 2)