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. Thes3cluster 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 theyoutube 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 newclickhouse-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: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: