View source: R/write_df_to_delta.R
| write_df_to_delta | R Documentation |
This function writes a large R data frame or tibble (df) to a Delta Lake
table (target_table) on Databricks using Databricks Volumes and the
COPY INTO SQL command.
write_df_to_delta(
df,
target_table,
db_conn,
column_types_schema = NULL,
volume_dir,
copy_options = "'mergeSchema' = 'true'",
overwrite_table = FALSE,
chunk_size_bytes = 5 * 1024^3
)
df |
A |
target_table |
A character string specifying the name of the Delta
table.
Can be unqualified ( |
db_conn |
A valid DBI connection object to Databricks. This connection is used to interact with the Delta table. |
column_types_schema |
An optional Arrow Schema object (created via
Type mapping reference (Arrow schema field → Spark SQL type):
Important notes for schema use:
|
volume_dir |
A character string specifying the path to the target Databricks Volume where the Parquet file will be uploaded. |
copy_options |
A character string specifying options for the
|
overwrite_table |
Logical; if |
chunk_size_bytes |
An integer specifying the size of each data chunk in bytes. This is used to split the data frame into smaller chunks for uploading. Defaults to 5GB. |
The function performs the following steps:
Optionally overwrites the target table.
Uploads the data as Parquet file(s) to a specified Databricks Volume.
Executes a COPY INTO command to load the file(s) into Delta Lake.
Deletes the temporary file(s) from the Volume after loading is complete.
Data is chunked into segments during upload to accommodate the Databricks REST API limit of 5 GB per single file upload.
Invisibly returns the result of the COPY INTO execution.
To use this function, users must ensure that they have appropriate Databricks permissions:
Catalog/schema: USE CATALOG on the target catalog
and USE SCHEMA on the target schema.
Table creation: CREATE TABLE on the target schema
(if overwrite_table = TRUE) or MODIFY and SELECT on
the existing table.
Volume access: READ VOLUME and WRITE VOLUME
on the Databricks Volume used for staging (specified in volume_dir).
Moreover, this function requires valid .Renviron variables for
authentication, specifically DATABRICKS_TOKEN and DATABRICKS_HOST.
DATABRICKS_HOST may be supplied with or without a scheme. A bare host
has https:// prepended automatically, and http:// is upgraded to
https://. Both adb-1234.cloud.databricks.com and
https://adb-1234.cloud.databricks.com are accepted. A trailing slash
is stripped if present.
Other databricks:
check_databricks_odbc()
## Not run:
# Setup connection using environment variables
con <- DBI::dbConnect(odbc::databricks(),
httpPath = Sys.getenv("DATABRICKS_SQL_PATH")
)
write_df_to_delta(
df = my_data,
target_table = "catalog.schema.my_table",
db_conn = con,
volume_dir = "/Volumes/catalog/schema",
overwrite_table = TRUE
)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.