@@ -4,22 +4,22 @@ title: Python
44
55Databend 提供了以下 Python 包,使您能够开发与 Databend 交互的 Python 应用程序:
66
7- - [ databend-py (** 推荐** )] ( https://github.com/databendcloud /databend-py ) : 提供直接与 Databend 数据库交互的接口。它允许您执行标准的 Databend 操作,如用户登录、数据库和表的创建、数据插入/加载和查询 。
8- - [ databend-sqlalchemy] ( https://github.com/databendcloud/databend-sqlalchemy ) : 提供了一个 SQL 工具包和 [ 对象关系映射] ( https://en.wikipedia.org/wiki/Object%E2%80%93relational_mapping ) 以与 Databend 数据库接口 。[ SQLAlchemy] ( https://www.sqlalchemy.org/ ) 是一个流行的 Python SQL 工具包和 ORM,而 databend-SQLAlchemy 是 SQLAlchemy 的一个方言,允许您使用 SQLAlchemy 与 Databend 交互。
7+ - [ databend-driver (** 推荐** )] ( https://pypi.org/project /databend-driver/ ) :一个用于 Databend 的 Python 驱动,提供同步和异步接口来与 Databend 交互、执行 SQL 查询和处理数据操作 。
8+ - [ databend-sqlalchemy] ( https://github.com/databendcloud/databend-sqlalchemy ) : 提供了一个 SQL 工具包和[ 对象关系映射] ( https://en.wikipedia.org/wiki/Object%E2%80%93relational_mapping ) 来与 Databend 数据库交互 。[ SQLAlchemy] ( https://www.sqlalchemy.org/ ) 是一个流行的 SQL 工具包和 Python 的 ORM,而 databend-SQLAlchemy 是 SQLAlchemy 的一个方言,允许您使用 SQLAlchemy 与 Databend 交互。
99
10- 这两个包都需要 Python 3.5 或更高版本。要检查您的 Python 版本,请在命令提示符中运行 ` python --version ` 。要安装最新的 ` databend-py ` 或 ` databend-sqlalchemy ` 包:
10+ 这两个包都需要 Python 3.7 或更高版本。要检查您的 Python 版本,请在命令提示符中运行 ` python --version ` 。要安装最新的 ` databend-driver ` 或 ` databend-sqlalchemy ` 包:
1111
1212``` bash
13- # 安装 databend-py
14- pip install databend-py
13+ # 安装 databend-driver
14+ pip install databend-driver
1515
1616# 安装 databend-sqlalchemy
1717pip install databend-sqlalchemy
1818```
1919
2020## 数据类型映射
2121
22- 下表展示了 Databend 通用数据类型与其对应的 Python 数据类型的对应关系 :
22+ 下表展示了 Databend 通用数据类型与其对应的 Python 数据类型之间的对应关系 :
2323
2424| Databend | Python |
2525| --------- | ----------------- |
@@ -36,7 +36,7 @@ pip install databend-sqlalchemy
3636| VARCHAR | str |
3737| BINARY | bytes |
3838
39- 下表展示了 Databend 半结构化数据类型与其对应的 Python 数据类型的对应关系 :
39+ 下表展示了 Databend 半结构化数据类型与其对应的 Python 数据类型之间的对应关系 :
4040
4141| Databend | Python |
4242| -------- | ------ |
@@ -47,236 +47,8 @@ pip install databend-sqlalchemy
4747| BITMAP | str |
4848| GEOMETRY | str |
4949
50- 在以下教程中,您将学习如何利用上述包来开发您的 Python 应用程序。教程将引导您在 Databend 中创建一个 SQL 用户,然后编写 Python 代码来创建表、插入数据和执行数据查询。
50+ ## 教程
5151
52- ## 教程 -1:使用 Python 与 Databend 集成
53-
54- 在开始之前,请确保您已成功安装本地 Databend。有关详细说明,请参阅 [ 本地和 Docker 部署] ( /guides/deploy/deploy/non-production/deploying-local ) 。
55-
56- ### 步骤 1. 准备一个 SQL 用户账户
57-
58- 要将您的程序连接到 Databend 并执行 SQL 操作,您必须在代码中提供一个具有适当权限的 SQL 用户账户。如果需要,请在 Databend 中创建一个,并确保该 SQL 用户仅具有必要的权限以确保安全。
59-
60- 本教程使用名为 'user1' 且密码为 'abc123' 的 SQL 用户作为示例。由于程序将向 Databend 写入数据,该用户需要 ALL 权限。有关如何管理 SQL 用户及其权限,请参阅 [ 用户与角色] ( /sql/sql-commands/ddl/user/ ) 。
61-
62- ``` sql
63- CREATE USER user1 IDENTIFIED BY ' abc123' ;
64- GRANT ALL on * .* TO user1;
65- ```
66-
67- ### 步骤 2. 配置连接字符串(针对 databend-py)
68-
69- ` databend-py ` 支持多种参数,这些参数可以作为 URL 参数或作为传递给 Client 的属性进行配置。下面提供的两个示例展示了设置这些参数的等效方式,适用于常见的 DSN:
70-
71- 示例 1:使用 URL 参数
72-
73- ``` python
74- # 格式:<schema>://<username>:<password>@<host_port>/<database>?<connection_params>
75- client = Client.from_url(' http://root@localhost:8000/db?secure=False©_purge=True&debug=True' )
76- ```
77-
78- 示例 2:使用 Client 参数
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- 要创建有效的 DSN,请根据您的需求选择适当的连接参数,详见 [ 这里] ( https://github.com/databendcloud/databend-py/blob/main/docs/connection.md ) 。
90-
91- ### 步骤 3. 编写一个 Python 程序
92-
93- 在这一步中,您将创建一个简单的 Python 程序,该程序与 Databend 通信。程序将涉及创建表、插入数据和执行数据查询等任务。
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- 您将使用 databend-py 库来创建客户端实例并直接执行 SQL 查询。
102-
103- 1 . 安装 databend-py。
104-
105- ``` shell
106- pip install databend-py
107- ```
108-
109- 2 . 将以下代码复制并粘贴到文件 ` main.py ` 中:
110-
111- ``` python title='main.py'
112- from databend_py import Client
113-
114- # 以名为 'user1' 且密码为 'abc123' 的 SQL 用户连接到本地 Databend 为例。
115- # 请根据实际情况使用您自己的值,同时保持相同的格式。
116- # 设置 secure=False 表示客户端将使用 HTTP 而非 HTTPS 连接到 Databend。
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- # 关闭连接。
130- client.disconnect()
131- ```
132-
133- 3 . 运行 ` 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- 您将使用 databend-sqlalchemy 库来创建连接器实例,并通过游标对象执行 SQL 查询。
144-
145- 1 . 安装 databend-sqlalchemy。
146-
147- ``` shell
148- pip install databend-sqlalchemy
149- ```
150-
151- 2 . 将以下代码复制并粘贴到文件 ` main.py ` 中:
152-
153- ``` python title='main.py'
154- from databend_sqlalchemy import connector
155-
156- # 以名为 'user1' 且密码为 'abc123' 的 SQL 用户连接到本地 Databend 为例。
157- # 请根据实际情况使用您自己的值,同时保持相同的格式。
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- # 关闭连接。
172- conn.close()
173- ```
174-
175- 3 . 运行 ` 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- 您将使用 databend-sqlalchemy 库来创建引擎实例,并通过 connect 方法创建可执行查询的连接。
186-
187- 1 . 安装 databend-sqlalchemy。
188-
189- ``` shell
190- pip install databend-sqlalchemy
191- ```
192-
193- 2 . 将以下代码复制并粘贴到文件 ` main.py ` 中:
194-
195- ``` python title='main.py'
196- from sqlalchemy import create_engine, text
197-
198- # 以名为 'user1' 且密码为 'abc123' 的 SQL 用户连接到本地 Databend 为例。
199- # 请根据实际情况使用您自己的值,同时保持相同的格式。
200- # 设置 secure=False 表示客户端将使用 HTTP 而非 HTTPS 连接到 Databend。
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- # 关闭连接。
219- connection1.close()
220- connection2.close()
221- engine.dispose()
222- ```
223-
224- 3 . 运行 ` python main.py ` :
225-
226- ``` text
227- Readings in Database Systems Michael Stonebraker 2004
228- ```
229-
230- </TabItem >
231- </Tabs >
232-
233- ## 教程 -2:使用 Python 与 Databend Cloud 集成(databend-py)
234-
235- 在开始之前,请确保您已成功创建计算集群并获取连接信息。有关如何操作,请参阅 [ 连接到计算集群] ( /guides/cloud/using-databend-cloud/warehouses#connecting ) 。
236-
237- ### 步骤 1. 使用 pip 安装依赖
238-
239- ``` shell
240- pip install databend-py
241- ```
242-
243- ### 步骤 2. 使用 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- # # 教程 -3:使用 Python 与 Databend Cloud 集成(databend-sqlalchemy)
258-
259- 在开始之前,请确保您已成功创建计算集群并获取连接信息。有关如何操作,请参阅 [连接到计算集群](/ guides/ cloud/ using- databend- cloud/ warehouses# connecting)。
260-
261- # ## 步骤 1. 使用 pip 安装依赖
262-
263- ```shell
264- pip install databend- sqlalchemy
265- ```
266-
267- # ## 步骤 2. 使用 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- 请将代码中的 `{USER }, {PASSWORD }, {HOST }, {WAREHOUSE_NAME } 和 {DATABASE }` 替换为您的连接信息。有关如何获取连接信息,请参阅 [连接到仓库](/ guides/ cloud/ using- databend- cloud/ warehouses# connecting)。
282- :::
52+ - [ 使用 databend-driver 与 Databend Cloud 集成] ( /tutorials/programming/python/integrating-with-databend-cloud-using-databend-driver )
53+ - [ 使用 databend-sqlalchemy 与 Databend Cloud 集成] ( /tutorials/programming/python/integrating-with-databend-cloud-using-databend-sqlalchemy )
54+ - [ 与自托管的 Databend 集成] ( /tutorials/programming/python/integrating-with-self-hosted-databend )
0 commit comments