You are viewing documentation about an older version (1.3.0). View latest version

snowflake.snowpark.DataFrame.copy_into_table

DataFrame.copy_into_table(table_name: str | Iterable[str], *, files: Iterable[str] | None = None, pattern: str | None = None, validation_mode: str | None = None, target_columns: Iterable[str] | None = None, transformations: Iterable[ColumnOrName] | None = None, format_type_options: Dict[str, Any] | None = None, statement_params: Dict[str, str] | None = None, **copy_options: Any) List[Row][source]

Executes a COPY INTO <table> command to load data from files in a stage location into a specified table.

It returns the load result described in OUTPUT section of the COPY INTO <table> command. The returned result also depends on the value of validation_mode.

It’s slightly different from the COPY INTO command in that this method will automatically create a table if the table doesn’t exist and the input files are CSV files whereas the COPY INTO <table> doesn’t.

To call this method, this DataFrame must be created from a DataFrameReader.

Example:

>>> # Create a CSV file to demo load
>>> import tempfile
>>> with tempfile.NamedTemporaryFile(mode="w+t") as t:
...     t.writelines(["id1, Product A", "\n" "id2, Product B"])
...     t.flush()
...     create_stage_result = session.sql("create temp stage if not exists test_stage").collect()
...     put_result = session.file.put(t.name, "@test_stage/copy_into_table_dir", overwrite=True)
>>> # user_schema is used to read from CSV files. For other files it's not needed.
>>> from snowflake.snowpark.types import StringType, StructField, StringType
>>> from snowflake.snowpark.functions import length
>>> user_schema = StructType([StructField("product_id", StringType()), StructField("product_name", StringType())])
>>> # Use the DataFrameReader (session.read below) to read from CSV files.
>>> df = session.read.schema(user_schema).csv("@test_stage/copy_into_table_dir")
>>> # specify target column names.
>>> target_column_names = ["product_id", "product_name"]
>>> drop_result = session.sql("drop table if exists copied_into_table").collect()  # The copy will recreate the table.
>>> copied_into_result = df.copy_into_table("copied_into_table", target_columns=target_column_names, force=True)
>>> session.table("copied_into_table").show()
---------------------------------
|"PRODUCT_ID"  |"PRODUCT_NAME"  |
---------------------------------
|id1           | Product A      |
|id2           | Product B      |
---------------------------------
Copy

The arguments of this function match the optional parameters of the COPY INTO <table>.

Parameters:
  • table_name – A string or list of strings that specify the table name or fully-qualified object identifier (database name, schema name, and table name).

  • files – Specific files to load from the stage location.

  • pattern – The regular expression that is used to match file names of the stage location.

  • validation_mode – A str that instructs the COPY INTO <table> command to validate the data files instead of loading them into the specified table. Values can be “RETURN_n_ROWS”, “RETURN_ERRORS”, or “RETURN_ALL_ERRORS”. Refer to the above mentioned COPY INTO <table> command optional parameters for more details.

  • target_columns – Name of the columns in the table where the data should be saved.

  • transformations – A list of column transformations.

  • format_type_options – A dict that contains the formatTypeOptions of the COPY INTO <table> command.

  • statement_params – Dictionary of statement level parameters to be set while executing this action.

  • copy_options – The kwargs that is used to specify the copyOptions of the COPY INTO <table> command.