The steps below will easily work on a local install of ClickHouse too. The only change would be to use the
s3 function instead of s3cluster (unless you have a cluster configured - in which case change default to the name of your cluster).Step-by-step instructions
1
Data exploration
Let’s see what the data looks like. The ClickHouse infers the following schema from the JSON file:
s3cluster table function returns a table, so we can DESCRIBE the result:2
Create the table
Based on the inferred schema, we cleaned up the data types and added a primary key.
Define the following table:
3
Insert data
The following command streams the records from the S3 files into the Some comments about our
youtube table.INSERT command:- The
parseDateTimeBestEffortUSOrZerofunction is handy when the incoming date fields may not be in the proper format. Iffetch_datedoesn’t get parsed properly, it will be set to0 - The
upload_datecolumn contains valid dates, but it also contains strings like “4 hours ago” - which is certainly not a valid date. We decided to store the original value inupload_date_strand attempt to parse it withtoDate(parseDateTimeBestEffortUSOrZero(upload_date::String)). If the parsing fails we just get0 - We used
ifNullto avoid gettingNULLvalues in our table. If an incoming value isNULL, theifNullfunction is setting the value to an empty string
4
Count the number of rows
Open a new tab in the SQL Console of ClickHouse Cloud (or a new
clickhouse-client window) and watch the count increase.
It will take a while to insert 4.56B rows, depending on your server resources. (Without any tweaking of settings, it takes about 4.5 hours.)5
Explore the data
Once the data is inserted, go ahead and count the number of dislikes of your favorite videos or channels. Let’s see how many videos were uploaded by ClickHouse:Let’s look and likes and dislikes of ClickHouse videos:The response looks like:Here is a search for videos with ClickHouse in the This query has to process every row, and also parse through two columns of strings. Even then, we get decent performance at 4.15M rows/second:The results look like:
The query above runs so quickly because we chose
uploader as the first column of the primary key - so it only had to process 237k rows.title or description fields: