CrateDB Blog | Development, integrations, IoT, & more

Building Query Lambdas with CrateDB's Views and the HTTP Endpoint

Written by Wierd van der Haar | 2024-08-15

This guide demonstrates how to create reusable, parameterized queries using CrateDB's views and execute them via the CrateDB HTTP Endpoint. This method streamlines query management, making it easier for former Rockset users to transition to CrateDB.

Step 1: Create a CrateDB Cloud environment

(if you already have one, you can skip this step)

  • Create a user as described here
  • Deploy a cluster as described here

Step 2: Create a table

After you have set up a CrateDB Cloud instance, you can proceed by creating a table. This could be done using the HTTP endpoint, but in this example, we use the CrateDB Cloud Console.

Use this code, paste it into the console, and execute.

Step 3: Insert sample data

Now that the table is created, it is time to insert some rows. In this example, we use a publicly available weather dataset. Copy the code into the Console and execute it to load the 70k rows.

Step 4: Query the data

Let's assume a query where NULL readings are interpolated based on the previous and the next value.

This could also be executed using the HTTP endpoint.

In a shell, set the following environment variables.

When the variables are set, you can execute the following.

Even though this works fine, you end up with a significant POST command. The best way to simplify things is by creating views.

Step 5: Create a View

Instead of including large SQL statements in the post of your command, you could simply create a view using the code.

Copy the code and execute it in the Console like this example:

With the view in place, the HTTP endpoint command looks like this.

Using views still gives you the power to use filters, for example. Let's assume you want to filter on location='Berlin' and interpolate_temperature > 10. You can still use the view.

Conclusion 

Using views combined with the CrateDB HTTP endpoint, you can efficiently manage and execute complex queries, providing an accessible pathway for Rockset users to transition to CrateDB.