Importing data from a CSV file
Before importing data, let’s create a table with a relevant structure:sometable table, we can pipe our file directly to the clickhouse-client:
FORMAT CSV clause so ClickHouse understands the file format. We can also load data directly from URLs using url() function or from S3 files using s3() function.
CSV files with headers
Suppose our CSV file has headers in it:CSV files with custom delimiters
In case the CSV file uses other than comma delimiter, we can use the format_csv_delimiter option to set the relevant symbol:; symbol is going to be used as a delimiter instead of a comma.
Skipping lines in a CSV file
Sometimes, we might skip a certain number of lines while importing data from a CSV file. This can be done using input_format_csv_skip_first_lines option:Treating NULL values in CSV files
Null values can be encoded differently depending on the application that generated the file. By default, ClickHouse uses\N as a Null value in CSV. But we can change that using the format_csv_null_representation option.
Suppose we have the following CSV file:
Nothing as a String (which is correct):
Nothing as NULL, we can define that using the following option:
NULL where we expect it to be:
TSV (tab-separated) files
Tab-separated data format is widely used as a data interchange format. To load data from a TSV file to ClickHouse, the TabSeparated format is used:Raw TSV
Sometimes, TSV files are saved without escaping tabs and line breaks. We should use TabSeparatedRaw to handle such files.Exporting to CSV
Any format in our previous examples can also be used to export data. To export data from a table (or a query) to a CSV format, we use the sameFORMAT clause:
Saving exported data to a CSV file
To save exported data to a file, we can use the INTO…OUTFILE clause:Exporting CSV with custom delimiters
If we want to have other than comma delimiters, we can use the format_csv_delimiter settings option for that:| as a delimiter for CSV format:
Exporting CSV for Windows
If we want a CSV file to work fine in a Windows environment, we should consider enabling output_format_csv_crlf_end_of_line option. This will use\r\n as a line breaks instead of \n:
Schema inference for CSV files
We might work with unknown CSV files in many cases, so we have to explore which types to use for columns. ClickHouse, by default, will try to guess data formats based on its analysis of a given CSV file. This is known as “Schema Inference”. Detected data types can be explored using theDESCRIBE statement in pair with the file() function:
String in this case.
Exporting and importing CSV with explicit column types
ClickHouse also allows explicitly setting column types when exporting data using CSVWithNamesAndTypes (and other *WithNames formats family):Custom delimiters, separators, and escaping rules
In sophisticated cases, text data can be formatted in a highly custom manner but still have a structure. ClickHouse has a special CustomSeparated format for such cases, which allows setting custom escaping rules, delimiters, line separators, and starting/ending symbols. Suppose we have the following data in the file:row(), lines are separated with , and individual values are delimited with ;. In this case, we can use the following settings to read data from this file:
Working with large CSV files
CSV files can be large, and ClickHouse works efficiently with files of any size. Large files usually come compressed, and ClickHouse covers this with no need for decompression before processing. We can use aCOMPRESSION clause during an insert:
COMPRESSION clause is omitted, ClickHouse will still try to guess file compression based on its extension. The same approach can be used to export files directly to compressed formats:
data_csv.csv.gz file.
Other formats
ClickHouse introduces support for many formats, both text, and binary, to cover various scenarios and platforms. Explore more formats and ways to work with them in the following articles:- CSV and TSV formats
- Parquet
- JSON formats
- Regex and templates
- Native and binary formats
- SQL formats