Overview
This tutorial follows the ClickHouse tutorial but runs all its queries via pg_clickhouse. Choose how you want to configurepg_clickhouse:
- From console if you are using ClickHouse Managed Postgres.
- From PSQL if you are installing and configuring the extension yourself.
Start ClickHouse
First, create a ClickHouse database if you don’t already have one. A quick way to start is with the Docker image:Create a table
Let’s borrow from the ClickHouse tutorial to create a simple database with The New York City taxi dataset:Add the data set
And then import the data:From console
If you are using ClickHouse Managed Postgres, you can configurepg_clickhouse and connect to a ClickHouse service from the ClickHouse Cloud
console. Open your Managed Postgres service, select Settings, and find the
ClickHouse integration section. Click Enable pg_clickhouse.
In the setup form, enter a name for the foreign server. Select the ClickHouse
service, database, and user to use for the connection. Enter the ClickHouse
password, then select the Postgres database and destination schema where the
ClickHouse tables should be imported. Click Connect to create the foreign
server and import the schema.
When setup is complete, the console displays a confirmation and the foreign
server appears in the server list.
You can now open the Postgres SQL console and query the imported tables:
taxi ClickHouse
database and import it into a taxi destination schema.
From PSQL
Install pg_clickhouse
Build and install pg_clickhouse from PGXN or GitHub. Or spin up a Docker container using the pg_clickhouse image, which simply adds pg_clickhouse to the Docker Postgres image:Connect pg_clickhouse
Now connect to Postgres:If you’re using the ClickHouse Cloud console to run SQL, grant the console admin user access to the foreign server:
password option.
Now, add the taxi table, just import it all of the tables from the remote
ClickHouse database into a Postgres schema:
\det+ to see it:
\d to show all the columns:
COUNT() aggregate, so it runs on ClickHouse and only
returns the single row to Postgres. Use EXPLAIN to see it:
Analyze the data
Run some queries to analyze the data. Explore the following examples or try your own SQL query.-
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:
-
Set display time zone for New York and retrieve rides to LaGuardia or JFK
airports:
Create a dictionary
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. TheLocationID column in the file maps to the pickup_nyct2010_gid and
dropoff_nyct2010_gid columns in your trips table:
-
Still in Postgres, use the
clickhouse_raw_queryfunction to create a ClickHouse dictionary namedtaxi_zone_dictionaryand populate the dictionary from the CSV file in S3:SettingLIFETIMEto 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.- Now import it:
- Confirm we can query it:
- Excellent. Now use the
dictGetfunction unction to retrieve a borough’s name in a query. For this query sums up the number of taxi rides per borough that end at either the LaGuardia or JFK airport:
This query sums up the number of taxi rides per borough that end at either the LaGuardia or JFK airport. Notice there are quite a few trips where the pickup neighborhood is unknown.
Perform a join
Write some queries that join thetaxi_zone_dictionary with your trips
table.
-
Start with a simple
JOINthat acts similarly to the previous airport query above:Notice the output of the aboveJOINquery is the same as thedictGetquery above, (except that theUnknownvalues aren’t included). Behind the scenes, ClickHouse is actually calling thedictGetfunction for thetaxi_zone_dictionarydictionary, but theJOINsyntax is more familiar for SQL developers. -
This query returns rows for the 1000 trips with the highest tip
amount, then performs an inner join of each row with the dictionary:
Generally, we avoid using
SELECT * in PostgreSQL and ClickHouse. You
should only retrieve the columns you actually need.