Azmi-84 commited on
Commit
dd84d5b
·
1 Parent(s): 3cd7fd0

Add DuckDB getting started guide with interactive examples. This commit introduces a new Python script that serves as a comprehensive guide to getting started with DuckDB. It includes interactive examples for database connections, table creation, data insertion, basic queries, and integration with Polars. The guide aims to facilitate learning and experimentation with DuckDB's features in a user-friendly manner.

Browse files
Files changed (1) hide show
  1. duckdb/01_getting_started.py +242 -0
duckdb/01_getting_started.py ADDED
@@ -0,0 +1,242 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import marimo
2
+
3
+ __generated_with = "0.11.30"
4
+ app = marimo.App(width="medium")
5
+
6
+
7
+ @app.cell(hide_code=True)
8
+ def _introduction(mo):
9
+ mo.md(
10
+ """
11
+ # DuckDB: An Embeddable Analytical Database System
12
+
13
+ ### What is DuckDB?
14
+
15
+ [DuckDB](https://duckdb.org/) is a high-performance, in-process analytical database management system (DBMS) designed for speed and simplicity. It's particularly well-suited for analytical query workloads, offering a robust SQL interface and efficient data processing capabilities. This document highlights key features and aspects of DuckDB relevant for a course on database systems or data analysis.
16
+
17
+ ### [Key Features](https://duckdb.org/why_duckdb):
18
+
19
+ - In-Process: Easy integration, zero external dependencies.
20
+ - Portable: Works on various OS and architectures.
21
+ - Columnar Storage: Efficient for analytical queries.
22
+ - Vectorized Execution: Speeds up data processing.
23
+ - ACID Transactions: Ensures data integrity.
24
+ - Multi-Language APIs: Python, R, Java, etc.
25
+
26
+ ### [Use Cases](https://github.com/davidgasquez/awesome-duckdb?tab=readme-ov-file):
27
+
28
+ - Data analysis and exploration
29
+ - Embedded analytics in applications
30
+ - ETL (Extract, Transform, Load) processes
31
+ - Data science and machine learning workflows
32
+ - Rapid prototyping of data analysis pipelines.
33
+
34
+ ### [Installation](https://duckdb.org/docs/installation/?version=stable&environment=python):
35
+
36
+ - The DuckDB Python API can be installed using pip:
37
+ ```
38
+ pip install duckdb
39
+ ```
40
+ - It is also possible to install DuckDB using conda:
41
+ ```
42
+ conda install python-duckdb -c conda-forge.
43
+ ```
44
+
45
+ **Python version:** DuckDB requires Python 3.7 or newer.
46
+ """
47
+ )
48
+ return
49
+
50
+
51
+ @app.cell(hide_code=True)
52
+ def _(mo):
53
+ mo.md(
54
+ r"""
55
+ # [1. DuckDB Basic Connection](https://duckdb.org/docs/stable/connect/overview.html)
56
+
57
+ DuckDB can run entirely in your computer's RAM, known as in-memory mode, which you can enable by using `:memory:` as the database name or by not providing a database file. It's crucial to understand that this means all data is temporary and will be completely erased when the program closes, as it isn't saved to disk.
58
+ """
59
+ )
60
+ return
61
+
62
+
63
+ @app.cell
64
+ def _database_connection(duckdb):
65
+ # Create a connection to an in-memory database
66
+ database_connection = duckdb.connect(database=":memory:")
67
+ print(
68
+ f"DuckDB version: {database_connection.execute('SELECT version()').fetchone()[0]}"
69
+ )
70
+ return (database_connection,)
71
+
72
+
73
+ @app.cell(hide_code=True)
74
+ def _(mo):
75
+ mo.md(
76
+ r"""# [2. Creating Tables](https://duckdb.org/docs/stable/sql/statements/create_table.html)"""
77
+ )
78
+ return
79
+
80
+
81
+ @app.cell
82
+ def _create_users_table(database_connection):
83
+ database_connection.execute(
84
+ """
85
+ CREATE TABLE users (
86
+ id INTEGER,
87
+ name VARCHAR,
88
+ age INTEGER,
89
+ registration_date DATE
90
+ )
91
+ """
92
+ )
93
+ return
94
+
95
+
96
+ @app.cell(hide_code=True)
97
+ def _(mo):
98
+ mo.md(
99
+ r"""# [3. Instering data into table](https://duckdb.org/docs/stable/sql/statements/insert)"""
100
+ )
101
+ return
102
+
103
+
104
+ @app.cell
105
+ def _insert_user_data(database_connection):
106
+ database_connection.execute(
107
+ """
108
+ INSERT INTO users VALUES
109
+ (1, 'Alice', 25, '2021-01-01'),
110
+ (2, 'Bob', 30, '2021-02-01'),
111
+ (3, 'Charlie', 35, '2021-03-01')
112
+ """
113
+ )
114
+ return
115
+
116
+
117
+ @app.cell(hide_code=True)
118
+ def _(mo):
119
+ mo.md(
120
+ r"""# [4. Basic Queries](https://duckdb.org/docs/stable/sql/query_syntax/select)"""
121
+ )
122
+ return
123
+
124
+
125
+ @app.cell
126
+ def _basic_queries(database_connection):
127
+ # Select all data
128
+ user_results = database_connection.execute("SELECT * FROM users").fetchall()
129
+ for user_row in user_results:
130
+ print(user_row)
131
+ return user_results, user_row
132
+
133
+
134
+ @app.cell(hide_code=True)
135
+ def _(mo):
136
+ mo.md(
137
+ r"""# [5. Working with Polars](https://duckdb.org/docs/stable/guides/python/polars.html)"""
138
+ )
139
+ return
140
+
141
+
142
+ @app.cell
143
+ def _polars_dataframe(database_connection, pl):
144
+ # Create a Polars DataFrame
145
+ polars_dataframe = pl.DataFrame(
146
+ {
147
+ "id": [1, 2, 3],
148
+ "name": ["Alice", "Bob", "Charlie"],
149
+ "age": [25, 30, 35],
150
+ "registration_date": ["2021-01-01", "2021-02-01", "2021-03-01"],
151
+ }
152
+ )
153
+
154
+ # Register the Polars DataFrame as a DuckDB table
155
+ database_connection.register("users_polars", polars_dataframe)
156
+
157
+ # Query the Polars DataFrame using DuckDB
158
+ polars_results = database_connection.execute(
159
+ "SELECT * FROM users_polars"
160
+ ).fetchall()
161
+ print("New Table:")
162
+ for polars_row in polars_results:
163
+ print(polars_row)
164
+ return polars_dataframe, polars_results, polars_row
165
+
166
+
167
+ @app.cell(hide_code=True)
168
+ def _(mo):
169
+ mo.md(
170
+ r"""# [6. Join Operations](https://duckdb.org/docs/stable/guides/performance/join_operations.html)"""
171
+ )
172
+ return
173
+
174
+
175
+ @app.cell
176
+ def _join_operations(database_connection):
177
+ join_results = database_connection.execute(
178
+ """
179
+ SELECT u.id, u.name, u.age, nu.registration_date
180
+ FROM users u
181
+ JOIN users_polars nu ON u.age < nu.age
182
+ """
183
+ )
184
+ print("Join Result:")
185
+ for join_row in join_results.fetchall():
186
+ print(join_row)
187
+ return join_results, join_row
188
+
189
+
190
+ @app.cell(hide_code=True)
191
+ def _(mo):
192
+ mo.md(
193
+ r"""# [7. Aggregate Functions](https://duckdb.org/docs/stable/sql/functions/aggregates.html)"""
194
+ )
195
+ return
196
+
197
+
198
+ @app.cell
199
+ def _aggregate_functions(database_connection):
200
+ aggregate_results = database_connection.execute(
201
+ """
202
+ SELECT AVG(age) as avg_age, MAX(age) as max_age, MIN(age) as min_age
203
+ FROM (SELECT * FROM users UNION ALL SELECT * FROM users_polars) AS all_users
204
+ """
205
+ ).fetchall()
206
+ print(
207
+ f"Average Age: {aggregate_results[0][0]:.1f}, "
208
+ f"Max Age: {aggregate_results[0][1]}, "
209
+ f"Min Age: {aggregate_results[0][2]}"
210
+ )
211
+ return (aggregate_results,)
212
+
213
+
214
+ @app.cell(hide_code=True)
215
+ def _(mo):
216
+ mo.md(
217
+ r"""# [8. Converting Results to Polars DataFrames](https://duckdb.org/docs/stable/guides/python/polars.html)"""
218
+ )
219
+ return
220
+
221
+
222
+ @app.cell
223
+ def _convert_to_polars(database_connection):
224
+ # -- 8. Converting Results to Polars DataFrames --
225
+ # Convert the result to a Polars DataFrame
226
+ polars_result_df = database_connection.execute("SELECT * FROM users").df()
227
+ print("Result as Polars DataFrame:")
228
+ print(polars_result_df)
229
+ return (polars_result_df,)
230
+
231
+
232
+ @app.cell(hide_code=True)
233
+ def _():
234
+ import marimo as mo
235
+ import duckdb
236
+ import polars as pl
237
+
238
+ return duckdb, mo, pl
239
+
240
+
241
+ if __name__ == "__main__":
242
+ app.run()