|
1 | 1 | # lealone-python |
2 | | -可以使用 Python开发微服务或用户自定义函数的插件 |
| 2 | + |
| 3 | +可以使用 Python 开发微服务或用户自定义函数的插件 |
| 4 | + |
| 5 | +运行 Python 插件需要事先安装 GraalVM,目前不支持 Windows |
| 6 | + |
| 7 | + |
| 8 | +## 安装 GraalVM |
| 9 | + |
| 10 | +安装 GraalVM 请参考 https://www.graalvm.org/22.0/docs/getting-started/ |
| 11 | + |
| 12 | +GraalVM 可以直接替换 JDK |
| 13 | + |
| 14 | +这里假设安装后的目录是 /home/lealone/graalvm-ee-java17-22.0.0.2 |
| 15 | + |
| 16 | +接着配置一下 JAVA_HOME 和 PATH 环境变量 |
| 17 | + |
| 18 | +export JAVA_HOME=/home/lealone/graalvm-ee-java17-22.0.0.2 |
| 19 | + |
| 20 | +export PATH=$JAVA_HOME/bin:$PATH |
| 21 | + |
| 22 | +最后还需要安装 Python 组件 |
| 23 | + |
| 24 | +gu install python |
| 25 | + |
| 26 | +更多信息参考 https://www.graalvm.org/22.0/reference-manual/python/ |
| 27 | + |
| 28 | + |
| 29 | +## 编译需要 |
| 30 | + |
| 31 | +* jdk 1.8+ |
| 32 | +* maven 3.8+ |
| 33 | + |
| 34 | + |
| 35 | +## 打包插件 |
| 36 | + |
| 37 | +运行 `mvn clean package -Dmaven.test.skip=true` |
| 38 | + |
| 39 | +生成 jar 包 `target/lealone-python-plugin-6.0.0.jar` |
| 40 | + |
| 41 | +假设 jar 包的绝对路径是 `/home/lealone/lealone-plugins/python/target/lealone-python-plugin-6.0.0.jar` |
| 42 | + |
| 43 | + |
| 44 | +## 创建插件 |
| 45 | + |
| 46 | +先参考[ lealone 快速入门](https://github.com/lealone/Lealone-Docs/blob/master/应用文档/Lealone数据库快速入门.md) 启动 lealone 数据库并打开一个命令行客户端 |
| 47 | + |
| 48 | +然后执行以下命令创建插件: |
| 49 | + |
| 50 | +```sql |
| 51 | +create plugin python |
| 52 | + implement by 'org.lealone.plugins.python.PythonServiceExecutorFactory' |
| 53 | + class path '/home/lealone/lealone-plugins/python/target/lealone-python-plugin-6.0.0.jar'; |
| 54 | +``` |
| 55 | + |
| 56 | +要 drop 插件可以执行以下命令: |
| 57 | + |
| 58 | +```sql |
| 59 | +drop plugin python; |
| 60 | +``` |
| 61 | + |
| 62 | +执行 drop plugin 会把插件占用的内存资源都释放掉 |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | +## 使用 Python 开发微服务应用 |
| 67 | + |
| 68 | +/home/lealone/lealone-plugins/python/src/test/resources/python/hello_service.py |
| 69 | + |
| 70 | +```Python |
| 71 | +def hello(name): |
| 72 | + return "hello " + name; |
| 73 | +``` |
| 74 | + |
| 75 | +## 创建服务 |
| 76 | + |
| 77 | +执行以下 SQL 创建 python_hello_service |
| 78 | + |
| 79 | +```sql |
| 80 | +create service if not exists python_hello_service ( |
| 81 | + hello(name varchar) varchar |
| 82 | +) |
| 83 | +language 'python' implement by '/home/lealone/lealone-plugins/python/src/test/resources/python/hello_service.py'; |
| 84 | +``` |
| 85 | + |
| 86 | +## 调用服务 |
| 87 | + |
| 88 | +执行以下 SQL 就可以直接调用 python_hello_service 了 |
| 89 | + |
| 90 | +```sql |
| 91 | +sql> execute service python_hello_service hello('test'); |
| 92 | ++-------------------------+ |
| 93 | +| 'PYTHON_HELLO_SERVICE.HELLO()' | |
| 94 | ++-------------------------+ |
| 95 | +| hello test | |
| 96 | ++-------------------------+ |
| 97 | +(1 row, 2 ms) |
| 98 | + |
| 99 | +sql> |
| 100 | +``` |
| 101 | + |
| 102 | + |
0 commit comments