From 6b0503c173c0c912b8b66aa0bd6bd96e54ae1331 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AA=91=E9=A9=AC=E5=B0=8F=E7=8C=AB?= <1435130236@qq.com> Date: Tue, 6 Apr 2021 23:09:21 +0800 Subject: [PATCH 1/2] add python blog --- .../01.getting-started-with-python.md | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 content/zh/post/jingjingwu/01.getting-started-with-python.md diff --git a/content/zh/post/jingjingwu/01.getting-started-with-python.md b/content/zh/post/jingjingwu/01.getting-started-with-python.md new file mode 100644 index 00000000..b10afa35 --- /dev/null +++ b/content/zh/post/jingjingwu/01.getting-started-with-python.md @@ -0,0 +1,86 @@ ++++ + +title = "OpenGauss数据库之Python驱动快速入门" + +date = "2021-04-04" + +tags = ["openGauss step by step系列"] + +archives = "2021-04" + +author = "吴京京" + +summary = "step by step系列之:openGauss1.0.1 Docker版本单机安装指南" + +img = "/zh/post/jiangdianbin/title/img38.png" + +times = "22:59" + ++++ + +# OpenGauss数据库之Python驱动 + +openGauss是一款开源关系型数据库管理系统,采用木兰宽松许可证v2发行。openGauss内核源自PostgreSQL,深度融合华为在数据库领域多年的经验,结合企业级场景需求,持续构建竞争力特性。 + +为了给大家介绍如何使用Python驱动连接OpenGauss数据库,我翻译了psycopg2的文档:[psycopg2中文文档](https://wjmcat.gitee.io/py-opengauss/)。 + +## 数据库部署 + +由于OpenGauss数据库与操作系统有一定的依赖关系,所以在简单应用上的开发,我推荐使用[Docker](https://hub.docker.com/r/enmotech/opengauss)来进行部署,部署的脚本如下所示: + +```shell +# 拉取镜像 +docker pull enmotech/opengauss + +# 开启opengauss数据库服务 +docker run --name opengauss --privileged=true -d -v /home/ubuntu/projects/opengauss/connect_test:/opengauss_test/ -e GS_PASSWORD=Secretpassword@123 -p 5432:5432 enmotech/opengauss:latest +``` + +在开启镜像服务之后,并没有创建对应的数据库,所以此时需要进入到Docker镜像当中来创建对应的数据库实例: +```shell +export GAUSSHOME=/usr/local/opengauss +export PATH=$GAUSSHOME/bin:$GAUSSHOME:$GAUSSHOME/lib:$PATH +export LD_LIBRARY_PATH=$GAUSSHOME/lib:$LD_LIBRARY_PATH +export DATADIR='/var/lib/opengauss/data' + +docker exec -it opengauss /bin/bash +gsql -U gaussdb -W Secretpassword@123 -d postgres -p 5432 +CREATE DATABASE test_db WITH ENCODING 'UTF8' template = template0; +gs_ctl reload -D $DATADIR +``` + +以上代码即可创建对应的数据库实例:`test_db`; + +## 安装教程 + +```shell +pip install psycopg2-binary +``` + +## Getting Started + +```python +import psycopg2 + +# 连接数据库 +conn = psycopg2.connect("dbname=test user=opengauss") + +# 打开数据库相关操作的游标对象 +cur = conn.cursor() + +# 执行一个查询 +cur.execute("SELECT * FROM my_data") + +# 获取所有查询结果 +records = cur.fetchall() +``` + +在以上代码中,展示了最简单的数据连接与数据库查询,使用方法非常简单,并符合[DB API v2](https://www.python.org/dev/peps/pep-0249/)的规范,从而让很多上有工具原生支持opengauss的操作,比如可直接在Sqlalchemy ORM工具中连接Opengauss数据库,这是一个非常重要的特性。 + +此处只是一个非常简单的数据库连接示例,后续我将持续发布一些深入的使用Opengauss Python数据库驱动的案例,录制了一个线上视频提供参考: + +[BiliBili-opengauss使用之python驱动](https://www.bilibili.com/video/BV1G54y1b7g4/) + +## 总结 + +Opengauss数据库是一个可处理高并发的高性能数据库,基于PostgreSql生态可轻松实现Python驱动应用程序的开发。 -- Gitee From 47cff7a2a977ddc92d8e0345c8d9ece66727b4b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AA=91=E9=A9=AC=E5=B0=8F=E7=8C=AB?= <1435130236@qq.com> Date: Wed, 7 Apr 2021 11:14:36 +0800 Subject: [PATCH 2/2] update getting started with python --- .../01.getting-started-with-python.md | 194 ++++++++++++++++-- 1 file changed, 175 insertions(+), 19 deletions(-) diff --git a/content/zh/post/jingjingwu/01.getting-started-with-python.md b/content/zh/post/jingjingwu/01.getting-started-with-python.md index b10afa35..940e69b1 100644 --- a/content/zh/post/jingjingwu/01.getting-started-with-python.md +++ b/content/zh/post/jingjingwu/01.getting-started-with-python.md @@ -22,59 +22,215 @@ times = "22:59" openGauss是一款开源关系型数据库管理系统,采用木兰宽松许可证v2发行。openGauss内核源自PostgreSQL,深度融合华为在数据库领域多年的经验,结合企业级场景需求,持续构建竞争力特性。 -为了给大家介绍如何使用Python驱动连接OpenGauss数据库,我翻译了psycopg2的文档:[psycopg2中文文档](https://wjmcat.gitee.io/py-opengauss/)。 +可是目前针对于OpenGauss数据库的Python应用程序的开发少之又少,这其中的一个原因在于不知道用什么驱动来连接该数据库,特别是Python应用程序,在此我将给大家介绍如何使用Python驱动连接OpenGauss数据库,同时翻译了psycopg2的文档:[psycopg2中文文档](https://wjmcat.gitee.io/py-opengauss/),目前仍在维护和调整中,如果有任何建议,欢迎提PR来进行修正。 -## 数据库部署 +## 一、数据库部署 -由于OpenGauss数据库与操作系统有一定的依赖关系,所以在简单应用上的开发,我推荐使用[Docker](https://hub.docker.com/r/enmotech/opengauss)来进行部署,部署的脚本如下所示: +教程的第一步当然是引导大家部署数据库了,由于OpenGauss数据库与操作系统有一定的依赖关系,为了屏蔽掉不同操作系统之间部署的区别,我推荐使用[Docker](https://hub.docker.com/r/enmotech/opengauss)来进行部署,部署的脚本如下所示: ```shell -# 拉取镜像 +# 1.拉取镜像 docker pull enmotech/opengauss -# 开启opengauss数据库服务 -docker run --name opengauss --privileged=true -d -v /home/ubuntu/projects/opengauss/connect_test:/opengauss_test/ -e GS_PASSWORD=Secretpassword@123 -p 5432:5432 enmotech/opengauss:latest +# 2.开启opengauss数据库服务 +docker run --name opengauss \ + --privileged=true -d \ + -e GS_USERNAME=gaussdb \ + -e GS_PASSWORD=Secretpassword@123 \ + -p 5432:5432 \ + enmotech/opengauss:latest ``` -在开启镜像服务之后,并没有创建对应的数据库,所以此时需要进入到Docker镜像当中来创建对应的数据库实例: +在以上代码中,默认数据库用户名为`gaussdb`,数据库密码为`Secretpassword@123`,开启服务的端口为`5432`,相信熟悉Docker的同学一眼就能看明白代码的含义。 + +可是在此部署的镜像当中只存在一个默认数据库:`gaussdb`,如果要添加新的数据库节点的话可以使用以下代码: + ```shell +# 1. 进入运行的Docker容器 +docker exec -it opengauss /bin/bash + +# 2. 设置环境变量 export GAUSSHOME=/usr/local/opengauss export PATH=$GAUSSHOME/bin:$GAUSSHOME:$GAUSSHOME/lib:$PATH export LD_LIBRARY_PATH=$GAUSSHOME/lib:$LD_LIBRARY_PATH export DATADIR='/var/lib/opengauss/data' -docker exec -it opengauss /bin/bash +# 3. 使用gsql登陆opengauss数据库 gsql -U gaussdb -W Secretpassword@123 -d postgres -p 5432 + +# 4. 创建test_db数据库 CREATE DATABASE test_db WITH ENCODING 'UTF8' template = template0; + +# 5. 重新加载OpenGauss数据库 gs_ctl reload -D $DATADIR ``` -以上代码即可创建对应的数据库实例:`test_db`; +以上命令执行完毕之后即可创建对应的数据库。 ## 安装教程 +要想使用Python驱动在OpenGauss数据库上开发应用,非常简单,只需要安装以下包即可: + ```shell pip install psycopg2-binary ``` -## Getting Started +安装步骤只需要一步即可,接下来就可以开始应用程序的开发。 + +## Simple Operations with OpenGauss + +为了演示与数据库的基本操作,我将从创建会话连接、创建表、插入数据、修改数据、删除数据以及查询数据等六个方面来演示。 + +任何数据库的操作都是需要先创建连接来管理对应的事务,OpenGauss也不例外: + +### 创建会话连接 ```python -import psycopg2 +from psycopg2 import connect + + +def create_conn(): + """get connection from envrionment variable by the conn factory + + Returns: + [type]: the psycopg2's connection object + """ + env = os.environ + params = { + 'database': env.get('OG_DATABASE', 'opengauss'), + 'user': env.get('OG_USER', 'gaussdb'), + 'password': env.get('OG_PASSWORD', 'Secretpassword@123'), + 'host': env.get('OG_HOST', '127.0.0.1'), + 'port': env.get('OG_PORT', 5432) + } + conn: connection = connect(**params) + return conn +``` -# 连接数据库 -conn = psycopg2.connect("dbname=test user=opengauss") +以上代码中从环境变量中获取对应配置,从而创建与数据库的会话连接。 -# 打开数据库相关操作的游标对象 -cur = conn.cursor() +### 创建表 -# 执行一个查询 -cur.execute("SELECT * FROM my_data") +所有的数据操作都是在表上的操作,所以接下来就是需要创建对应的表: -# 获取所有查询结果 -records = cur.fetchall() +```python +def create_table(conn): + """check and create table by example + + Args: + table_name (str): the name of the table + corsor (type): the corsor type to get into operation with db + """ + sql = f"""SELECT EXISTS + ( + SELECT 1 + FROM pg_tables + WHERE tablename = '{table_name}' + );""" + with conn: + with conn.cursor() as cursor: + cursor.execute(sql) + result = cursor.fetchone() + if not result[0]: + logger.info(f'creating table<{table_name}>') + sql = f"""CREATE TABLE {table_name} (id serial PRIMARY KEY, name varchar, course varchar, grade integer);""" + result = cursor.execute(sql) + conn.commit() ``` +以上代码中,首先是检测斗对应的表是否存在,如果不存在的话,便创建对应的表。 + + +### 插入数据 + +```python +def insert_data(conn) -> int: + """insert faker data + + Args: + cnn ([type]): the connection object to the databse + """ + faker = Faker(locale=['zh-cn']) + sql = f"insert into {table_name} (name, course, grade) values (%s,%s,%s) RETURNING *;" + with conn: + with conn.cursor() as cursor: + age = random.randint(20, 80) + result = cursor.execute(sql, (faker.name(), faker.name(), age)) + result = cursor.fetchone() + logger.info(f'add data<{result}> to the databse') + conn.commit() + return result[0] if result else None +``` + +使用SQL语句来插入数据,语法与Mysql等数据库有些不一样,可是大同小异,都是能够看懂。在语句的后面返回当前操作的结果,也就是能够获取插入数据的ID。 + +### 修改数据 + +```python +def update_data(conn, student): + """insert faker data + + Args: + cnn ([type]): the connection object to the databse + """ + faker = Faker(locale=['zh-cn']) + sql = f"update {table_name} name=%s, course=%s, grade=%s where id={student.id};" + with conn: + with conn.cursor() as cursor: + age = random.randint(20, 80) + result = cursor.execute(sql, (faker.name(), faker.name(), age)) + result = cursor.fetchone() + logger.info(f'update data<{result}> to the databse') + conn.commit() +``` + +修改数据只需要使用以上代码的SQL语句即可,相信熟悉SQL的同学一眼就能看懂。 + +接下来就是删除数据了: + +### 删除数据 + +```python +def delete_data_by_id(conn, id: int): + """delete data by primary key + + Args: + conn ([type]): the connection object + id (int): the primary key of the table + """ + sql = f"delete from {table_name} where id = %s;" + with conn: + with conn.cursor() as cursor: + cursor.execute(sql, (id,)) + logger.info(f'delete data from databse by id<{id}>') +``` + +### 获取数据 + +```python +def get_data_by_id(conn, id: int): + """fetch data by id + + Args: + conn ([type]): the connection object + id (int): the primary key of the table + + Returns: + [type]: the tuple data of the table + """ + sql = f"select * from {table_name} where id = %s;" + with conn: + with conn.cursor() as cursor: + cursor.execute(sql, (id,)) + result = cursor.fetchone() + logger.info(f'select data<{result}> from databse') + return result +``` + +在以上代码中,通过SELECT语句筛选出对应的数据,这个接口是非常简单且通用的。 + +在以上代码中有一个规律,所有的操作都是需要在Cursor上进行操作,这是因为每一个原子事务的控制都是基于cursor对象,这样通过细粒度的控制能够很好的调度应用程序中所有的数据库交互操作。 + 在以上代码中,展示了最简单的数据连接与数据库查询,使用方法非常简单,并符合[DB API v2](https://www.python.org/dev/peps/pep-0249/)的规范,从而让很多上有工具原生支持opengauss的操作,比如可直接在Sqlalchemy ORM工具中连接Opengauss数据库,这是一个非常重要的特性。 此处只是一个非常简单的数据库连接示例,后续我将持续发布一些深入的使用Opengauss Python数据库驱动的案例,录制了一个线上视频提供参考: -- Gitee