Skip to content

Commit 6c62951

Browse files
committed
updates
1 parent 90ee1f2 commit 6c62951

File tree

6 files changed

+359
-66
lines changed

6 files changed

+359
-66
lines changed

docs/en/developer/00-drivers/01-python.md

Lines changed: 82 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ title: Python
44

55
Databend offers the following Python packages enabling you to develop Python applications that interact with Databend:
66

7-
- [databend-py (**Recommended**)](https://github.com/databendcloud/databend-py): Provides a direct interface to the Databend database. It allows you to perform standard Databend operations such as user login, database and table creation, data insertion/loading, and querying.
7+
- [databend-driver (**Recommended**)](https://pypi.org/project/databend-driver/): A Python driver for Databend, providing both synchronous and asynchronous interfaces to interact with the Databend database, execute SQL queries, and handle data operations.
88
- [databend-sqlalchemy](https://github.com/databendcloud/databend-sqlalchemy): Provides a SQL toolkit and [Object-Relational Mapping](https://en.wikipedia.org/wiki/Object%E2%80%93relational_mapping) to interface with the Databend database. [SQLAlchemy](https://www.sqlalchemy.org/) is a popular SQL toolkit and ORM for Python, and databend-SQLAlchemy is a dialect for SQLAlchemy that allows you to use SQLAlchemy to interact with Databend.
99

10-
Both packages require Python version 3.5 or higher. To check your Python version, run `python --version` in your command prompt. To install the latest `databend-py` or `databend-sqlalchemy` package:
10+
Both packages require Python version 3.7 or higher. To check your Python version, run `python --version` in your command prompt. To install the latest `databend-driver` or `databend-sqlalchemy` package:
1111

1212
```bash
13-
# install databend-py
14-
pip install databend-py
13+
# install databend-driver
14+
pip install databend-driver
1515

1616
# install databend-sqlalchemy
1717
pip install databend-sqlalchemy
@@ -47,9 +47,7 @@ This table illustrates the correspondence between Databend semi-structured data
4747
| BITMAP | str |
4848
| GEOMETRY | str |
4949

50-
In the following tutorial, you'll learn how to utilize the packages above to develop your Python applications. The tutorial will walk you through creating a SQL user in Databend and then writing Python code to create a table, insert data, and perform data queries.
51-
52-
## Tutorial-1: Integrating with Databend using Python
50+
## Tutorial-1: Integrating with Self-Hosted Databend
5351

5452
Before you start, make sure you have successfully installed a local Databend. For detailed instructions, see [Local and Docker Deployments](/guides/deploy/deploy/non-production/deploying-local).
5553

@@ -64,75 +62,63 @@ CREATE USER user1 IDENTIFIED BY 'abc123';
6462
GRANT ALL on *.* TO user1;
6563
```
6664

67-
### Step 2. Configuring Connection String (for databend-py)
68-
69-
`databend-py` supports various parameters that can be configured either as URL parameters or as properties passed to the Client. The two examples provided below demonstrate equivalent ways of setting these parameters for the common DSN:
70-
71-
Example 1: Using URL parameters
72-
73-
```python
74-
# Format: <schema>://<username>:<password>@<host_port>/<database>?<connection_params>
75-
client = Client.from_url('http://root@localhost:8000/db?secure=False&copy_purge=True&debug=True')
76-
```
77-
78-
Example 2: Using Client parameters
79-
80-
```python
81-
client = Client(
82-
host='tenant--warehouse.ch.datafusecloud.com',
83-
database="default",
84-
user="user",
85-
port="443",
86-
password="password", settings={"copy_purge": True, "force": True})
87-
```
88-
89-
To create a valid DSN, select appropriate connection parameters outlined [here](https://github.com/databendcloud/databend-py/blob/main/docs/connection.md) based on your requirements.
90-
91-
### Step 3. Write a Python Program
65+
### Step 2. Write a Python Program
9266

9367
In this step, you'll create a simple Python program that communicates with Databend. The program will involve tasks such as creating a table, inserting data, and executing data queries.
9468

9569
import Tabs from '@theme/Tabs';
9670
import TabItem from '@theme/TabItem';
9771

9872
<Tabs groupId="python">
99-
<TabItem value="databend-py" label="databend-py">
100-
101-
You will use the databend-py library to create a client instance and execute SQL queries directly.
73+
<TabItem value="databend-driver" label="databend-driver">
10274

103-
1. Install databend-py.
75+
1. Install databend-driver.
10476

10577
```shell
106-
pip install databend-py
78+
pip install databend-driver
10779
```
10880

10981
2. Copy and paste the following code to the file `main.py`:
11082

11183
```python title='main.py'
112-
from databend_py import Client
84+
from databend_driver import BlockingDatabendClient
11385

11486
# Connecting to a local Databend with a SQL user named 'user1' and password 'abc123' as an example.
115-
# Feel free to use your own values while maintaining the same format.
116-
# Setting secure=False means the client will connect to Databend using HTTP instead of HTTPS.
117-
client = Client('user1:[email protected]', port=8000, secure=False)
118-
client.execute("CREATE DATABASE IF NOT EXISTS bookstore")
119-
client.execute("USE bookstore")
120-
client.execute("CREATE TABLE IF NOT EXISTS booklist(title VARCHAR, author VARCHAR, date VARCHAR)")
121-
client.execute("INSERT INTO booklist VALUES('Readings in Database Systems', 'Michael Stonebraker', '2004')")
87+
client = BlockingDatabendClient('databend://user1:[email protected]:8000/?sslmode=disable')
12288

123-
_, results = client.execute("SELECT * FROM booklist")
124-
for (title, author, date) in results:
125-
print("{} {} {}".format(title, author, date))
126-
client.execute('drop table booklist')
127-
client.execute('drop database bookstore')
89+
# Create a cursor to interact with Databend
90+
cursor = client.cursor()
12891

129-
# Close Connect.
130-
client.disconnect()
92+
# Create database and use it
93+
cursor.execute("CREATE DATABASE IF NOT EXISTS bookstore")
94+
cursor.execute("USE bookstore")
95+
96+
# Create table
97+
cursor.execute("CREATE TABLE IF NOT EXISTS booklist(title VARCHAR, author VARCHAR, date VARCHAR)")
98+
99+
# Insert data into the table
100+
cursor.execute("INSERT INTO booklist VALUES('Readings in Database Systems', 'Michael Stonebraker', '2004')")
101+
102+
# Query data from the table
103+
cursor.execute("SELECT * FROM booklist")
104+
rows = cursor.fetchall()
105+
106+
# Print the results
107+
for row in rows:
108+
print(f"{row[0]} {row[1]} {row[2]}")
109+
110+
# Drop the table and database
111+
cursor.execute('DROP TABLE booklist')
112+
cursor.execute('DROP DATABASE bookstore')
113+
114+
# Close the cursor
115+
cursor.close()
131116
```
132117

133118
3. Run `python main.py`:
134119

135-
```text
120+
```bash
121+
python main.py
136122
Readings in Database Systems Michael Stonebraker 2004
137123
```
138124

@@ -230,31 +216,61 @@ Readings in Database Systems Michael Stonebraker 2004
230216
</TabItem>
231217
</Tabs>
232218

233-
## Tutorial-2: Integrating with Databend Cloud using Python (databend-py)
219+
## Tutorial-2: Integrating with Databend Cloud using databend-driver
234220

235221
Before you start, make sure you have successfully created a warehouse and obtained the connection information. For how to do that, see [Connecting to a Warehouse](/guides/cloud/using-databend-cloud/warehouses#connecting).
236222

237223
### Step 1. Install Dependencies with pip
238224

239225
```shell
240-
pip install databend-py
226+
pip install databend-driver
241227
```
242228

243-
### Step 2. Connect with databend-py
229+
### Step 2. Connect with databend-driver
230+
231+
1. Copy and paste the following code to the file `main.py`:
244232

245233
```python
246-
from databend_py import Client
247-
248-
client = Client.from_url(f"databend://{USER}:{PASSWORD}@${HOST}:443/{DATABASE}?&warehouse={WAREHOUSE_NAME}&secure=True)
249-
client.execute('DROP TABLE IF EXISTS data')
250-
client.execute('CREATE TABLE if not exists data (x Int32,y VARCHAR)')
251-
client.execute('DESC data')
252-
client.execute("INSERT INTO data (Col1,Col2) VALUES ", [1, 'yy', 2, 'xx'])
253-
_, res = client.execute('select * from data')
254-
print(res)
234+
from databend_driver import BlockingDatabendClient
235+
236+
# Connecting to Databend Cloud with your credentials (replace PASSWORD, HOST, DATABASE, and WAREHOUSE_NAME)
237+
client = BlockingDatabendClient(f"databend://cloudapp:{PASSWORD}@{HOST}:443/{DATABASE}?warehouse={WAREHOUSE_NAME}")
238+
239+
# Get a cursor from the client to execute queries
240+
cursor = client.cursor()
241+
242+
# Drop the table if it exists
243+
cursor.execute('DROP TABLE IF EXISTS data')
244+
245+
# Create the table if it doesn't exist
246+
cursor.execute('CREATE TABLE IF NOT EXISTS data (x Int32, y String)')
247+
248+
# Describe the table
249+
cursor.execute('DESC data')
250+
251+
# Insert data into the table
252+
cursor.execute("INSERT INTO data (x, y) VALUES (1, 'yy'), (2, 'xx')")
253+
254+
# Select all data from the table
255+
cursor.execute('SELECT * FROM data')
256+
257+
# Fetch all rows from the result
258+
rows = cursor.fetchall()
259+
260+
# Print the result
261+
for row in rows:
262+
print(row.values())
263+
```
264+
265+
2. Run `python main.py`:
266+
267+
```bash
268+
python main.py
269+
(1, 'yy')
270+
(2, 'xx')
255271
```
256272

257-
## Tutorial-3: Integrating with Databend Cloud using Python (databend-sqlalchemy)
273+
## Tutorial-3: Integrating with Databend Cloud using databend-sqlalchemy
258274

259275
Before you start, make sure you have successfully created a warehouse and obtained the connection information. For how to do that, see [Connecting to a Warehouse](/guides/cloud/using-databend-cloud/warehouses#connecting).
260276

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"label": "Programming"
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"label": "Python"
3+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
title: Integrating with Databend Cloud using databend-driver
3+
---
4+
5+
In this tutorial, we'll walk you through how to use the `databend-driver` to connect to Databend Cloud, create a table, insert data, and retrieve results with Python.
6+
7+
## Before You Start
8+
9+
Before you start, make sure you have successfully created a warehouse and obtained the connection information. For how to do that, see [Connecting to a Warehouse](/guides/cloud/using-databend-cloud/warehouses#connecting).
10+
11+
## Step 1: Install Dependencies with pip
12+
13+
```shell
14+
pip install databend-driver
15+
```
16+
17+
## Step 2: Connect with databend-driver
18+
19+
1. Copy and paste the following code to the file `main.py`:
20+
21+
```python
22+
from databend_driver import BlockingDatabendClient
23+
24+
# Connecting to Databend Cloud with your credentials (replace PASSWORD, HOST, DATABASE, and WAREHOUSE_NAME)
25+
client = BlockingDatabendClient(f"databend://cloudapp:{PASSWORD}@{HOST}:443/{DATABASE}?warehouse={WAREHOUSE_NAME}")
26+
27+
# Get a cursor from the client to execute queries
28+
cursor = client.cursor()
29+
30+
# Drop the table if it exists
31+
cursor.execute('DROP TABLE IF EXISTS data')
32+
33+
# Create the table if it doesn't exist
34+
cursor.execute('CREATE TABLE IF NOT EXISTS data (x Int32, y String)')
35+
36+
# Describe the table
37+
cursor.execute('DESC data')
38+
39+
# Insert data into the table
40+
cursor.execute("INSERT INTO data (x, y) VALUES (1, 'yy'), (2, 'xx')")
41+
42+
# Select all data from the table
43+
cursor.execute('SELECT * FROM data')
44+
45+
# Fetch all rows from the result
46+
rows = cursor.fetchall()
47+
48+
# Print the result
49+
for row in rows:
50+
print(row.values())
51+
```
52+
53+
2. Run `python main.py`:
54+
55+
```bash
56+
python main.py
57+
(1, 'yy')
58+
(2, 'xx')
59+
```
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
title: Integrating with Databend Cloud using databend-sqlalchemy
3+
---
4+
5+
In this tutorial, we'll walk you through how to use the `databend-sqlalchemy` library to connect to Databend Cloud, create a table, insert data, and query results using Python.
6+
7+
## Before You Start
8+
9+
Before you start, make sure you have successfully created a warehouse and obtained the connection information. For how to do that, see [Connecting to a Warehouse](/guides/cloud/using-databend-cloud/warehouses#connecting).
10+
11+
## Step 1: Install Dependencies with pip
12+
13+
```shell
14+
pip install databend-sqlalchemy
15+
```
16+
17+
## Step 2: Connect with databend_sqlalchemy
18+
19+
1. Copy and paste the following code to the file `main.py`:
20+
21+
```python
22+
from databend_sqlalchemy import connector
23+
24+
# Connecting to Databend Cloud with your credentials (replace PASSWORD, HOST, DATABASE, and WAREHOUSE_NAME)
25+
cursor = connector.connect(f"databend://cloudapp:{PASSWORD}@{HOST}:443/{DATABASE}?warehouse={WAREHOUSE_NAME}").cursor()
26+
cursor.execute('DROP TABLE IF EXISTS data')
27+
cursor.execute('CREATE TABLE IF NOT EXISTS data( Col1 TINYINT, Col2 VARCHAR )')
28+
cursor.execute("INSERT INTO data (Col1, Col2) VALUES (%s, %s), (%s, %s)", [1, 'yy', 2, 'xx'])
29+
cursor.execute("SELECT * FROM data")
30+
print(cursor.fetchall())
31+
```
32+
33+
2. Run `python main.py`:
34+
35+
```bash
36+
python main.py
37+
[(1, 'yy'), (2, 'xx')]
38+
```

0 commit comments

Comments
 (0)