Learn how to ingest and query data in ClickHouse using the New York City taxi example dataset.
PrerequisitesYou need access to a running ClickHouse service to complete this tutorial. For instructions, see the Quick Start guide.
Create a new tableThe New York City taxi dataset contains details about millions of taxi rides, with columns including tip amount, tolls, payment type, and more. Create a table to store this data.
Connect to the SQL console:
https://_hostname_:8443/play
. Check with your ClickHouse administrator for the details.Create the following trips
table in the default
database:
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: trips_1.tsv.gz
and trips_2.tsv.gz
:
Wait for the INSERT
to finish. It might take a moment for the 150 MB of data to be downloaded.
When the insert is finished, verify it worked:
This query should return 1,999,657 rows.
Run some queries to analyze the data. Explore the following examples or try your own SQL query.
Calculate the average tip amount:
Expected outputCalculate the average cost based on the number of passengers:
Expected outputThe passenger_count
ranges from 0 to 9:
Calculate the daily number of pickups per neighborhood:
Expected outputCalculate the length of each trip in minutes, then group the results by trip length:
Expected outputShow the number of pickups in each neighborhood broken down by hour of the day:
Expected outputRetrieve rides to LaGuardia or JFK airports:
Expected outputA dictionary is a mapping of key-value pairs stored in memory. For details, see Dictionaries
Create a dictionary associated with a table in your ClickHouse service. The table and dictionary are based on a CSV file that contains a row for each neighborhood in New York City.
The neighborhoods are mapped to the names of the five New York City boroughs (Bronx, Brooklyn, Manhattan, Queens and Staten Island), as well as Newark Airport (EWR).
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:
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
.Note
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.
Verify it worked. The following should return 265 rows, or one row for each neighborhood:
Use the 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
).
For example, the following query returns the Borough
whose LocationID
is 132, which corresponds to JFK airport):
JFK is in Queens. Notice the time to retrieve the value is essentially 0:
Use the dictHas
function to see if a key is present in the dictionary. For example, the following query returns 1
(which is "true" in ClickHouse):
The following query returns 0 because 4567 is not a value of LocationID
in the dictionary:
Use the dictGet
function to retrieve a borough's name in a query. For example:
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:
Write some queries that join the taxi_zone_dictionary
with your trips
table.
Start with a simple JOIN
that acts similarly to the previous airport query above:
The response looks is identical to the dictGet
query:
Note
Notice the output of the above JOIN
query is the same as the query before it that used dictGetOrDefault
(except that the Unknown
values are not 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.
This query returns rows for the the 1000 trips with the highest tip amount, then performs an inner join of each row with the dictionary:
Note
Generally, we avoid using SELECT *
often in ClickHouse. You should only retrieve the columns you actually need. However, this query is slower for the purposes of the example.
Learn more about ClickHouse with the following documentation:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4