In this tutorial you’ll explore how ClickHouse can be used to run analytical queries over large amounts of data.
You’ll also see how you can use a dictionary to enrich the data and write join queries.
Prerequisites
For this tutorial, you’ll need:- A ClickHouse Cloud account ($300 in free credits when signing up)
- A ClickHouse Cloud service
Create the table
The dataset used in this tutorial is the New York City taxi dataset, which contains details about millions of taxi rides, with columns including tip amount, tolls, payment type, and more.
- Select SQL console from the the left hand menu
- Click the + tab next to the home icon to create a new query
- In the SQL editor type the following query, then click Run:
Expandable
Insert the data
Now that you’ve created a table, add the New York City taxi data from CSV files in S3.The following command inserts ~2,000,000 rows into your trips table from two different files in S3: Wait for the data to finish inserting. Around 150MB of data will be downloaded.
When the data is finished inserting, check the number of rows in the You should get a result of 1,999,657 rows
trips_1.tsv.gz and trips_2.tsv.gz:Expandable
trips table:Analyze the data
With the data loaded, you can run some queries to analyze the data.
-
Calculate the average tip amount:
-
Calculate the average cost based on the number of passengers:
-
Calculate the daily number of pickups per neighborhood:
-
Calculate the length of each trip in minutes, then group the results by trip length:
-
Show the number of pickups in each neighborhood broken down by hour of the day:
Create a dictionary
Next you’ll create a dictionary (a mapping of key-value pairs stored in memory) called Verify it worked. The following query should return 265 rows, or one row for each neighborhood:
taxi_zone_dictionary to provide a mapping between location IDs and NYC borough names, sourced from a CSV file containing every New York City neighborhood.
These correspond to the pickup_nyct2010_gid and dropoff_nyct2010_gid columns in the trips table.Here’s an excerpt from the CSV file you’re using in table format. The LocationID column in the file maps to the pickup_nyct2010_gid and dropoff_nyct2010_gid columns in your trips table:Run the following SQL command, which creates a dictionary named
taxi_zone_dictionary and populates the dictionary from the CSV file in S3. The URL for the file is https://datasets-documentation.s3.eu-west-3.amazonaws.com/nyc-taxi/taxi_zone_lookup.csv.Setting
LIFETIME to 0 disables automatic updates to avoid unnecessary traffic to our S3 bucket. In other cases, you might configure it differently. For details, see Refreshing dictionary data using LIFETIME.Run queries using the dictionary
You can use the JFK is in Queens. Notice the time to retrieve the value is essentially 0:Use the The following query returns 0 because 4567 isn’t a value of Use the This query sums up the number of taxi rides per borough that end at either the LaGuardia or JFK airport. The result looks like the following, and notice there are quite a few trips where the pickup neighborhood is unknown:
dictGet function (or its variations) to retrieve a value from a dictionary.
You pass in the name of the dictionary, the value you want, and the key (which in our example is the LocationID column of taxi_zone_dictionary), and get back the matching value.
For example, the following query returns the Borough whose LocationID is 132 (corresponding to JFK airport):dictHas function to see if a key is present in the dictionary. For example, the following query returns 1 (which is “true” in ClickHouse):LocationID in the dictionary:dictGet function to retrieve a borough’s name in a query. For example:Perform a join
Finally, write some queries that join the The response looks identical to the This query returns rows for the 1000 trips with the highest tip amount, then performs an inner join of each row with the dictionary:
taxi_zone_dictionary with your trips table.Start with a simple JOIN that acts similarly to the previous airport query above:dictGet query:Notice the output of the above
JOIN query is the same as the query before it that used dictGetOrDefault (except that the Unknown values aren’t included).
Behind the scenes, ClickHouse is actually calling the dictGet function for the taxi_zone_dictionary dictionary, but the JOIN syntax is more familiar for SQL developers.Next steps
Learn more about ClickHouse with the following documentation:- Introduction to primary indexes in ClickHouse: Learn how ClickHouse uses sparse primary indexes to efficiently locate relevant data during queries.
- Integrate an external data source: Review data source integration options, including files, Kafka, PostgreSQL, data pipelines, and many others.
- Visualize data in ClickHouse: Connect your favorite UI/BI tool to ClickHouse.
- SQL Reference: Browse the SQL functions available in ClickHouse for transforming, processing and analyzing data.