# Wrapping a Serverless ClickHouse pt.2

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1665570893115/z576IZ16g.png align="left")

<mark>LATEST UPDATE: </mark> [<mark>https://blog.qryn.dev/chdb-clickhouse-as-a-library</mark>](https://blog.qryn.dev/chdb-clickhouse-as-a-library)

# ClickHouse into Space

In our previous episode we had fun assembling a [portable ClickHouse binary with a thin API layer ready to be used in a serverless function](https://blog.qryn.dev/wrapping-a-serverless-clickhouse-pt1) with remote cloud storage.

Today, we're launching [ClickHouse](https://clickhouse.com/docs/en/home/) into Space with [Deta](https://alpha.deta.space/) 🚀

  

> **Warnings**  
> *This is a free experimental ride for demo purposes. Not a final product!*  
> *The goal is to showcase the flexibility of Deta and power of ClickHouse*  

  

## Sign up for [Deta Space Alpha](https://alpha.deta.space/)

Deta is your own personal computer in the cloud: *private, secure & always online*

🟣 All apps on Deta Space run in your own sandboxed 'personal cloud'  
🟣 App developers cannot see or touch your personal infrastructure  
🟣 App users run clones and remain in the drivers seat with all the data  

Deta Space Micros, Base and Drive are perfect for modern web applications.

*But can they run something as complex as ClickHouse?*   **TLDR; HELL YEAH!**

  

## Launch your ClickHouse Space

Ready? Browse to the [ClickHouse Space](https://alpha.deta.space/discovery/clickhouse-22.10.1.1.-3) page using Discovery and Install

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1665735048581/O4-yP0oP-.png align="left")

![](https://d24bnpykhxwj9p.cloudfront.net/s3file/s3fs-public/users1/2017-02/Thu/stanley1.gif align="left")

Space will generate a unique secure URL for your **ClickHouse Space** endpoint

```bash
https://CLICKHOUSE-SPACE-URL.deta.app
```

Fire up your Browser - you should be welcomed by a familiar interface 🌞🌑

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1665570509669/cgIhvNTJp.png align="left")

Awesome! Your serverless, stateless and *soul-less* ClickHouse is ready! 🔥

## Deta ClickHouse + AWS S3

Our ClickHouse has no storage attached. It only works with remote datasets.  
Let run our first Space query and fetch some Taxi rides from AWS S3

```bash
SELECT * FROM s3('https://s3.us-east-1.amazonaws.com/altinity-clickhouse-data/nyc_taxi_rides/data/tripdata/data-20*.csv.gz', 'CSVWithNames', 'pickup_date Date, id UInt64, vendor_id String, tpep_pickup_datetime DateTime, tpep_dropoff_datetime DateTime, passenger_count UInt8, trip_distance Float32, pickup_longitude Float32, pickup_latitude Float32, rate_code_id String, store_and_fwd_flag String, dropoff_longitude Float32, dropoff_latitude Float32, payment_type LowCardinality(String), fare_amount Float32, extra String, mta_tax Float32, tip_amount Float32, tolls_amount Float32, improvement_surcharge Float32, total_amount Float32, pickup_location_id UInt16, dropoff_location_id UInt16, junk1 String, junk2 String', 'gzip') LIMIT 20 FORMAT JSONCompact
```

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1665567297496/ayf7Rzi_k.png align="left")

*Congratulations! You're now a ClickHouse Space Cadet* 🏆🚀

## Deta ClickHouse + Deta S3

Your ClickHouse Space ship offers a virtual S3 endpoint on top of *Deta Drive*

To use it, just deploy a personal [**s3rver-deta**](https://alpha.deta.space/discovery/s3rver-1.0.6) instance using Discovery

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1665735048581/O4-yP0oP-.png align="left")

Space will generate a unique secure URL for your personal **S3 Drive** endpoint

```bash
https://S3RVER-SPACE-URL.deta.app
```

Let's `INSERT` some data into our Space bucket

```bash
INSERT INTO FUNCTION s3('https://S3RVER-SPACE-URL.deta.app/test/test.csv.gz', 'CSV', 'name String, value UInt32', 'gzip')
VALUES ('one', 1), ('two', 2),('three', 3), ('four', 4)
SETTINGS s3_truncate_on_insert=1
```

```bash
Query id: 523a1130-a1fa-4212-aaac-f24486d2e71e

Ok.

4 rows in set. Elapsed: 0.121 sec.
```

Let's `SELECT` our data back - *note authentication is handled by the edge*

```bash
SELECT * FROM s3('https://S3RVER-SPACE-URL.deta.app/test/test.csv.gz', 'CSV', 'name String, value UInt32') FORMAT JSONCompact
```

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1666015766484/ReIaT5xWx.png align="left")

*Whoo-Hoo!* 🚀

All of your database data is exclusively stored in your *own Deta Space Drive*  

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1665747181388/-r3DWRasr.png align="left")

## Deta ClickHouse + URLENG

Our S3 Space bucket is secure and only accessible to our Space instance.

To share data between instances or with remote ClickHouse servers, we can switch to the public [Serverless URL Engine](https://urleng.com) *(also running on Deta Space)* using a **Secret URL**

> Replace **SUPERSECRETURL** with your **own random string**

```bash
INSERT INTO FUNCTION url('https://urleng.com/SUPERSECRETURL', JSONEachRow, 'key String, value UInt64') VALUES ('hello', 1)
```

```bash
SELECT * FROM url('https://urleng.com/SUPERSECRETURL', JSONEachRow)
```

> That's it. Now your ClickHouse Space instances can exchange persistent data.

  

### Next: *Beyond ClickHouse*

The experiment continues and there's so much more coming!

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1665826931661/-BnSa6Jly.png align="left")

In our next article we'll showcase **InfluDB Flux** running on Deta Space at ludicrous speeds, with APIs compatible with **Grafana** and ready to run in seconds ... *for free!*

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1665776818649/vJwxImmJd.png align="left")

#### Cross Connect

The **Flux** and **ClickHouse** Space containers can communicate and exchange data!

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1665789401630/H1Z90PyQ3.png align="left")

*Just imagine the possibilities for this little big serverless chain ...*

  

### Live Demo

This is not an image. It's a real Clickhouse serverless shell.

<iframe src="https://clickhouse.glitch.me/?user=default#U0VMRUNUIHZlcnNpb24oKQ==" width="100%" height="500"></iframe>

## Conclusion

This ClickHouse *does not exist*. Unless you're *talking to it*. *From Space!*  

Lots of work is up next in order to make this POC *faster, lighter and stronger!*

The question is: *What will you build with it?* 🚀 🚀 🚀

[![](https://res.cloudinary.com/crunchbase-production/image/upload/c_lpad,h_256,w_256,f_auto,q_auto:eco,dpr_1/hyxpdvyfvlvsz9dh3a70 align="left")](https://alpha.deta.space)

Thanks to the **Deta Team** for their dedication and for their support!

#### Want More?

*Did this get you excited? Join our team,* [*we're hiring*](mailto:info@qxip.net)*!*  
*Working with ClickHouse? Try our* [*polyglot observability stack*](https://qryn.dev)

[![](https://cdn.hashnode.com/res/hashnode/image/upload/v1665785535618/6MMcraiG8.png align="left")](https://metrico.in)

> Enjoy and Share your Comments! 👋
