Skip to content

Commit 4061f73

Browse files
authored
chdb + urleng session persistence example
1 parent 5352706 commit 4061f73

File tree

1 file changed

+168
-0
lines changed

1 file changed

+168
-0
lines changed
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 0,
4+
"metadata": {
5+
"colab": {
6+
"provenance": []
7+
},
8+
"kernelspec": {
9+
"name": "python3",
10+
"display_name": "Python 3"
11+
},
12+
"language_info": {
13+
"name": "python"
14+
}
15+
},
16+
"cells": [
17+
{
18+
"cell_type": "markdown",
19+
"source": [
20+
"<img src=\"https://github.com/chdb-io/chdb/raw/main/docs/_static/snake-chdb.png\" width=150>\n",
21+
"\n",
22+
"# chdb + urleng\n",
23+
"This example shows the combined power of chdb and urleng to persist sessions on the cloud\n",
24+
"\n",
25+
"* [chdb](https://chdb.io) is an in-memory OLAP database powered by ClickHouse\n",
26+
"* [urleng.com](https://urleng.com) is a free pastie service for ClickHouse url tables"
27+
],
28+
"metadata": {
29+
"id": "olco61H9GcX2"
30+
}
31+
},
32+
{
33+
"cell_type": "code",
34+
"execution_count": null,
35+
"metadata": {
36+
"id": "W56ggkPiD9ZB"
37+
},
38+
"outputs": [],
39+
"source": [
40+
"!pip install chdb --upgrade --quiet"
41+
]
42+
},
43+
{
44+
"cell_type": "code",
45+
"source": [
46+
"import uuid\n",
47+
"from chdb import session as chs\n",
48+
"\n",
49+
"## Generate a secret, unique secret urleng.com session token & save for later\n",
50+
"## url_token = str(uuid.uuid1())\n",
51+
"url_token = 'dc81bb0a-4902-11ee-a84b-0242ac1c000c' # just for demo purposes\n",
52+
"%store url_token\n",
53+
"\n",
54+
"print(url_token)\n",
55+
"\n",
56+
"## Create DB, Table, View in temp session, auto cleanup when session is deleted.\n",
57+
"sess = chs.Session()\n",
58+
"\n",
59+
"sess.query(\"CREATE DATABASE IF NOT EXISTS db_xxx ENGINE = Atomic\")\n",
60+
"sess.query(\"CREATE TABLE IF NOT EXISTS db_xxx.log_table_xxx (key String, value UInt64) ENGINE = Log;\")\n",
61+
"sess.query(\"INSERT INTO db_xxx.log_table_xxx VALUES ('a', 1), ('b', 3), ('c', 2), ('d', 5);\")\n",
62+
"\n",
63+
"print(\"Select from session:\\n\")\n",
64+
"print(sess.query(\"SELECT * FROM db_xxx.log_table_xxx\", \"Pretty\"))\n",
65+
"\n",
66+
"## Copy to urlengine table, persist the session on the cloud.\n",
67+
"sess.query(\"INSERT INTO FUNCTION url('https://urleng.com/\"+url_token+\"', JSONEachRow, 'key String, value UInt64') SELECT * FROM db_xxx.log_table_xxx\")"
68+
],
69+
"metadata": {
70+
"colab": {
71+
"base_uri": "https://localhost:8080/"
72+
},
73+
"outputId": "d41c860e-503a-4203-a259-9a7b2864050f",
74+
"id": "9mDQLI3NFVQP"
75+
},
76+
"execution_count": 41,
77+
"outputs": [
78+
{
79+
"output_type": "stream",
80+
"name": "stdout",
81+
"text": [
82+
"Stored 'url_token' (str)\n",
83+
"dc81bb0a-4902-11ee-a84b-0242ac1c000c\n",
84+
"Select from session:\n",
85+
"\n",
86+
"┏━━━━━┳━━━━━━━┓\n",
87+
"\u001b[1mkey\u001b[0m ┃ \u001b[1mvalue\u001b[0m ┃\n",
88+
"┡━━━━━╇━━━━━━━┩\n",
89+
"│ a │ 1 │\n",
90+
"├─────┼───────┤\n",
91+
"│ b │ 3 │\n",
92+
"├─────┼───────┤\n",
93+
"│ c │ 2 │\n",
94+
"├─────┼───────┤\n",
95+
"│ d │ 5 │\n",
96+
"└─────┴───────┘\n",
97+
"\n"
98+
]
99+
},
100+
{
101+
"output_type": "execute_result",
102+
"data": {
103+
"text/plain": []
104+
},
105+
"metadata": {},
106+
"execution_count": 41
107+
}
108+
]
109+
},
110+
{
111+
"cell_type": "code",
112+
"source": [
113+
"from chdb import session as chs\n",
114+
"\n",
115+
"## Create a new DB, Table, View in temp session, auto cleanup when session is deleted.\n",
116+
"new_sess = chs.Session()\n",
117+
"\n",
118+
"## Retrieve the urlengine session token\n",
119+
"%store -r url_token\n",
120+
"print(url_token)\n",
121+
"\n",
122+
"new_sess.query(\"CREATE DATABASE IF NOT EXISTS db_xxx ENGINE = Atomic\")\n",
123+
"new_sess.query(\"CREATE TABLE IF NOT EXISTS db_xxx.log_table_xxx (key String, value UInt64) ENGINE = Log;\")\n",
124+
"\n",
125+
"## There's no data in our session yet. Zero results.\n",
126+
"print(new_sess.query(\"SELECT * FROM db_xxx.log_table_xxx\", \"Pretty\"))\n",
127+
"\n",
128+
"## Restore session from the cloud, insert data from urleng table\n",
129+
"new_sess.query(\"INSERT INTO db_xxx.log_table_xxx SELECT * FROM url('https://urleng.com//\"+url_token+\"', JSONEachRow)\")\n",
130+
"\n",
131+
"print(\"Select from restored session:\\n\")\n",
132+
"print(new_sess.query(\"SELECT * FROM db_xxx.log_table_xxx\", \"Pretty\"))\n"
133+
],
134+
"metadata": {
135+
"colab": {
136+
"base_uri": "https://localhost:8080/"
137+
},
138+
"outputId": "e9c254b1-6c71-4545-f67c-9c7d5a556031",
139+
"id": "xW9B4jryHNIr"
140+
},
141+
"execution_count": 42,
142+
"outputs": [
143+
{
144+
"output_type": "stream",
145+
"name": "stdout",
146+
"text": [
147+
"dc81bb0a-4902-11ee-a84b-0242ac1c000c\n",
148+
"\n",
149+
"Select from restored view:\n",
150+
"\n",
151+
"┏━━━━━┳━━━━━━━┓\n",
152+
"\u001b[1mkey\u001b[0m ┃ \u001b[1mvalue\u001b[0m ┃\n",
153+
"┡━━━━━╇━━━━━━━┩\n",
154+
"│ a │ 1 │\n",
155+
"├─────┼───────┤\n",
156+
"│ b │ 3 │\n",
157+
"├─────┼───────┤\n",
158+
"│ c │ 2 │\n",
159+
"├─────┼───────┤\n",
160+
"│ d │ 5 │\n",
161+
"└─────┴───────┘\n",
162+
"\n"
163+
]
164+
}
165+
]
166+
}
167+
]
168+
}

0 commit comments

Comments
 (0)