Sub topic 4
File formats and FORMAT_OPTIONS
This chapter assumes you can write a basic COPY INTO statement, that you know what a Unity Catalog volume is, and that you know how CSV, JSON and Parquet differ. It covers how you tell COPY INTO what the source files look like.
FILEFORMAT tells the reader what to parse
FILEFORMAT is required. It selects the parser that reads each source file. The common values are:
| FILEFORMAT | Typical use | Header or schema in file |
|---|---|---|
| CSV | Exports from other systems | Optional header row |
| JSON | Event logs, API dumps | Field names in each record |
| PARQUET | Data already produced by Spark or similar tools | Schema stored in the file |
| AVRO, ORC, TEXT, BINARYFILE | Specialized sources | Varies |
Parquet and Avro carry their own schema, so they need few options. CSV and JSON are text, so you must describe how to read them.
FORMAT_OPTIONS controls parsing
FORMAT_OPTIONS is a list of key-value pairs, and both keys and values are written as strings. These options affect how the files are read. They do not affect how the table is written.
COPY INTO main.sales.orders
FROM '/Volumes/main/sales/landing/orders/'
FILEFORMAT = CSV
FORMAT_OPTIONS (
'header' = 'true',
'delimiter' = ',',
'inferSchema' = 'true'
);Common CSV options:
header:'true'treats the first row of each file as column names. The default is'false', so the header row would be loaded as data.delimiter: the separator character. Use'|'or'\t'for pipe- or tab-separated files.inferSchema:'true'samples the data to guess column types. Without it, every CSV column is read as a string.multiLine:'true'allows quoted values that contain line breaks.mergeSchema:'true'reconciles the schemas of all input files. Without it, the schema is taken from a sample of the files.
Common JSON options:
multiLine: set to'true'when a file holds one pretty-printed JSON document or array instead of one record per line.allowSingleQuotesandallowComments: accept relaxed JSON syntax.
COPY INTO main.sales.events
FROM '/Volumes/main/sales/landing/events/'
FILEFORMAT = JSON
FORMAT_OPTIONS ('multiLine' = 'true');FORMAT_OPTIONS versus COPY_OPTIONS
These two clauses are easy to confuse.
FORMAT_OPTIONS configures the reader. COPY_OPTIONS configures the load itself, for example 'force' and 'mergeSchema'. Note that mergeSchema exists in both clauses with different meanings. In FORMAT_OPTIONS it merges the schemas of the input files. In COPY_OPTIONS it lets the target table gain new columns. A later chapter covers the second use.
COPY INTO main.sales.orders
FROM '/Volumes/main/sales/landing/orders/'
FILEFORMAT = CSV
FORMAT_OPTIONS ('header' = 'true', 'mergeSchema' = 'true')
COPY_OPTIONS ('mergeSchema' = 'true');Common errors
- Header loaded as a row. You omitted
'header' = 'true'. With a typed target table, this often fails with a cast error on the first value, such as convertingorder_idto a number. - Everything is a string. CSV was read without
inferSchema. Either set it or cast columns in aSELECTsource. - One giant column. The
delimiterdoes not match the file. - Only one JSON record loaded, or parse errors. The file is pretty-printed and needs
'multiLine' = 'true'. - Option ignored. Option names are specific to each format. A CSV option given with
FILEFORMAT = JSONhas no effect, so check the spelling and the format.
Exercise
A pipe-separated file with a header row loads into your table, but every row has a single column containing the whole line. Which FORMAT_OPTIONS entry fixes this, and what value do you give it?
Show answer
Set 'delimiter' = '|'. The default delimiter is a comma, so a pipe-separated line is read as one field. Keep 'header' = 'true' so the first row is used for column names.
Exercise
What is the difference between 'mergeSchema' inside FORMATOPTIONS and 'mergeSchema' inside COPYOPTIONS?
Show answer
In FORMATOPTIONS it makes the reader merge the schemas of all input files. In COPYOPTIONS it allows the target Delta table to add columns that appear in the source but not in the table.