Skip to content

Commit fb34bc6

Browse files
committed
Merge branch 'feat-website-adjust' of github.com:datafuselabs/databend-docs into feat-website-adjust
* 'feat-website-adjust' of github.com:datafuselabs/databend-docs: Update integrating-with-databend-cloud-using-databend-driver.md Update 01-python.md updates refator: adjust dir feat: text # Conflicts: # docs/en/guides/20-cloud/00-new-account.md
2 parents 4f9cfd0 + dccc738 commit fb34bc6

6 files changed

+282
-236
lines changed

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

+8-236
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 Databend, 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,236 +47,8 @@ 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.
50+
## Tutorials
5151

52-
## Tutorial-1: Integrating with Databend using Python
53-
54-
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).
55-
56-
### Step 1. Prepare a SQL User Account
57-
58-
To connect your program to Databend and execute SQL operations, you must provide a SQL user account with appropriate privileges in your code. Create one in Databend if needed, and ensure that the SQL user has only the necessary privileges for security.
59-
60-
This tutorial uses a SQL user named 'user1' with password 'abc123' as an example. As the program will write data into Databend, the user needs ALL privileges. For how to manage SQL users and their privileges, see [User & Role](/sql/sql-commands/ddl/user/).
61-
62-
```sql
63-
CREATE USER user1 IDENTIFIED BY 'abc123';
64-
GRANT ALL on *.* TO user1;
65-
```
66-
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
92-
93-
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.
94-
95-
import Tabs from '@theme/Tabs';
96-
import TabItem from '@theme/TabItem';
97-
98-
<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.
102-
103-
1. Install databend-py.
104-
105-
```shell
106-
pip install databend-py
107-
```
108-
109-
2. Copy and paste the following code to the file `main.py`:
110-
111-
```python title='main.py'
112-
from databend_py import Client
113-
114-
# 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')")
122-
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')
128-
129-
# Close Connect.
130-
client.disconnect()
131-
```
132-
133-
3. Run `python main.py`:
134-
135-
```text
136-
Readings in Database Systems Michael Stonebraker 2004
137-
```
138-
139-
</TabItem>
140-
141-
<TabItem value="databend-sqlalchemy with Object" label="databend-sqlalchemy (Connector)">
142-
143-
You will use the databend-sqlalchemy library to create a connector instance and execute SQL queries using the cursor object.
144-
145-
1. Install databend-sqlalchemy.
146-
147-
```shell
148-
pip install databend-sqlalchemy
149-
```
150-
151-
2. Copy and paste the following code to the file `main.py`:
152-
153-
```python title='main.py'
154-
from databend_sqlalchemy import connector
155-
156-
# Connecting to a local Databend with a SQL user named 'user1' and password 'abc123' as an example.
157-
# Feel free to use your own values while maintaining the same format.
158-
conn = connector.connect(f"http://user1:[email protected]:8000").cursor()
159-
conn.execute("CREATE DATABASE IF NOT EXISTS bookstore")
160-
conn.execute("USE bookstore")
161-
conn.execute("CREATE TABLE IF NOT EXISTS booklist(title VARCHAR, author VARCHAR, date VARCHAR)")
162-
conn.execute("INSERT INTO booklist VALUES('Readings in Database Systems', 'Michael Stonebraker', '2004')")
163-
conn.execute('SELECT * FROM booklist')
164-
165-
results = conn.fetchall()
166-
for (title, author, date) in results:
167-
print("{} {} {}".format(title, author, date))
168-
conn.execute('drop table booklist')
169-
conn.execute('drop database bookstore')
170-
171-
# Close Connect.
172-
conn.close()
173-
```
174-
175-
3. Run `python main.py`:
176-
177-
```text
178-
Readings in Database Systems Michael Stonebraker 2004
179-
```
180-
181-
</TabItem>
182-
183-
<TabItem value="databend-sqlalchemy with Engine" label="databend-sqlalchemy (Engine)">
184-
185-
You will use the databend-sqlalchemy library to create an engine instance and execute SQL queries using the connect method to create connections that can execute queries.
186-
187-
1. Install databend-sqlalchemy.
188-
189-
```shell
190-
pip install databend-sqlalchemy
191-
```
192-
193-
2. Copy and paste the following code to the file `main.py`:
194-
195-
```python title='main.py'
196-
from sqlalchemy import create_engine, text
197-
198-
# Connecting to a local Databend with a SQL user named 'user1' and password 'abc123' as an example.
199-
# Feel free to use your own values while maintaining the same format.
200-
# Setting secure=False means the client will connect to Databend using HTTP instead of HTTPS.
201-
engine = create_engine("databend://user1:[email protected]:8000/default?secure=False")
202-
203-
connection1 = engine.connect()
204-
connection2 = engine.connect()
205-
206-
with connection1.begin() as trans:
207-
connection1.execute(text("CREATE DATABASE IF NOT EXISTS bookstore"))
208-
connection1.execute(text("USE bookstore"))
209-
connection1.execute(text("CREATE TABLE IF NOT EXISTS booklist(title VARCHAR, author VARCHAR, date VARCHAR)"))
210-
connection1.execute(text("INSERT INTO booklist VALUES('Readings in Database Systems', 'Michael Stonebraker', '2004')"))
211-
212-
result = connection2.execute(text("SELECT * FROM booklist"))
213-
results = result.fetchall()
214-
215-
for (title, author, date) in results:
216-
print("{} {} {}".format(title, author, date))
217-
218-
# Close Connect.
219-
connection1.close()
220-
connection2.close()
221-
engine.dispose()
222-
```
223-
224-
3. Run `python main.py`:
225-
226-
```text
227-
Readings in Database Systems Michael Stonebraker 2004
228-
```
229-
230-
</TabItem>
231-
</Tabs>
232-
233-
## Tutorial-2: Integrating with Databend Cloud using Python (databend-py)
234-
235-
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).
236-
237-
### Step 1. Install Dependencies with pip
238-
239-
```shell
240-
pip install databend-py
241-
```
242-
243-
### Step 2. Connect with databend-py
244-
245-
```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)
255-
```
256-
257-
## Tutorial-3: Integrating with Databend Cloud using Python (databend-sqlalchemy)
258-
259-
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).
260-
261-
### Step 1. Install Dependencies with pip
262-
263-
```shell
264-
pip install databend-sqlalchemy
265-
```
266-
267-
### Step 2. Connect with Databend SQLAlchemy
268-
269-
```python
270-
from databend_sqlalchemy import connector
271-
272-
cursor = connector.connect(f"databend://{USER}:{PASSWORD}@${HOST}:443/{DATABASE}?&warehouse={WAREHOUSE_NAME}).cursor()
273-
cursor.execute('DROP TABLE IF EXISTS data')
274-
cursor.execute('CREATE TABLE IF NOT EXISTS data( Col1 TINYINT, Col2 VARCHAR )')
275-
cursor.execute("INSERT INTO data (Col1,Col2) VALUES ", [1, 'yy', 2, 'xx'])
276-
cursor.execute("SELECT * FROM data")
277-
print(cursor.fetchall())
278-
```
279-
280-
:::tip
281-
Replace `{USER}, {PASSWORD}, {HOST}, {WAREHOUSE_NAME} and {DATABASE}` in the code with your connection information. For how to obtain the connection information, see [Connecting to a Warehouse](/guides/cloud/using-databend-cloud/warehouses#connecting).
282-
:::
52+
- [Integrating with Databend Cloud using databend-driver](/tutorials/programming/python/integrating-with-databend-cloud-using-databend-driver)
53+
- [Integrating with Databend Cloud using databend-sqlalchemy](/tutorials/programming/python/integrating-with-databend-cloud-using-databend-sqlalchemy)
54+
- [Integrating with Self-Hosted Databend](/tutorials/programming/python/integrating-with-self-hosted-databend)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"label": "Programming"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"label": "Python"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
# Insert data into the table
37+
cursor.execute("INSERT INTO data (x, y) VALUES (1, 'yy'), (2, 'xx')")
38+
39+
# Select all data from the table
40+
cursor.execute('SELECT * FROM data')
41+
42+
# Fetch all rows from the result
43+
rows = cursor.fetchall()
44+
45+
# Print the result
46+
for row in rows:
47+
print(row.values())
48+
```
49+
50+
2. Run `python main.py`:
51+
52+
```bash
53+
python main.py
54+
(1, 'yy')
55+
(2, 'xx')
56+
```
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)