LoginSignup
0
0

Oracle Machine Learning for Pythonをインストールしてみた。(2024/01/30)

Posted at

はじめに

Oracle DatabaseのIn-Database機械学習機能であるOracle Machine Learningは、PL/SQL、R言語に加えてPythonも利用することができます。
これまでAutonomous Databaseで利用可能だったOracle Machine Learning for Python(OML4Py)がAutonomous Database以外のOracle Databaseでも利用できることになりました。
Autonomous Databaseには、あらかじめインストールされていますが、それ以外のOracle Databaseでは、必要なPythonライブラリおよびOML4Pyサーバー・コンポーネントをデータベースにインストールし、OML4Pyクライアントをインストールする作業が発生しますので、インストールしてみました。

今回はOracle Linux8上のOracle Database 19cの環境にインストールしています。

作業ステップ

  1. OML4Pyをインストールするシステム要件
  2. Pythonのインストール
  3. 必要なサポート・パッケージのインストール
  4. Oracle Database用のOML4Pyサーバーのインストール
  5. OML4Pyクライアントのインストール

OML4Pyをインストールするシステム要件

2024年1月時点でOML4Pyが対応している環境は以下となっています。

OS環境

Intel CPUで稼働するOracle Linuxリリース7 64bit および Oracle Linuxリリース8 64bit
(Oracle Linux x86-64 7.x / Oracle Linux x86-64 8.x)

Database環境 / OML4Py構成要件

Oracle Databaseリリース Pythonバージョン Oracle Machine Learning for Pythonバージョン
19c、21c(Clientのみ) 3.9.5 1.0

Pythonのインストール

Database ServerにPython 3.9.5 をインストールします。Pythonのインストールはoracleユーザで実行します。

1. Python.orgのサイトからソースtgzファイル (Python-3.9.5.tgz)をダウンロード

wget https://www.python.org/ftp/python/3.9.5/Python-3.9.5.tgz

2. ORACLE_HOME以下にpythonディレクトリ を作成し、コンテンツを抽出
$ORACLE_HOME/pythonディレクトリを作成し、ダウンロードしたPythonインストールモジュールを展開します。

mkdir -p $ORACLE_HOME/python
tar -xvzf Python-3.9.5.tgz --strip-components=1 -C $ORACLE_HOME/python
cd $ORACLE_HOME/python

3. OML4Pyに必要なライブラリの確認

以下のLinuxライブラリをインストールされていることを確認、インストールします。
perl-Env、libffi-devel、openssl、openssl-devel、tk-devel、xz-devel、zlib-devel、bzip2-devel、readline-devel、libuuid-devel、ncurses-devel

$ sudo yum install perl-Env libffi-devel openssl openssl-devel tk-devel xz-devel zlib-devel bzip2-devel readline-devel libuuid-devel ncurses-devel

4. Python 3.9.5をビルド
展開したPythonをビルドします。OML4PyのEmbedded Python Executionを使用する場合は、--enable-sharedフラグを使用します。

cd $ORACLE_HOME/python
./configure --enable-shared --prefix=$ORACLE_HOME/python
make clean; make
make altinstall

5. 環境変数の設定
Pythonコマンドを利用する環境変数を設定、稼働の確認します。あわせてpython3.9実行可能ファイルにシンボリックリンクを作成します。

export PYTHONHOME=$ORACLE_HOME/python
export PATH=$PYTHONHOME/bin:$PATH
export LD_LIBRARY_PATH=$PYTHONHOME/lib:$LD_LIBRARY_PATH
cd $ORACLE_HOME/python/bin
ln -s python3.9 python3
python3 -m pip install --upgrade pip

必要なサポート・パッケージのインストール

Oracle Databaseサーバーに以下のOML4Pyの必須パッケージをインストールします。

  • pandas==1.3.4
  • scipy==1.7.3
  • matplotlib==3.3.3
  • cx_Oracle==8.1.0
  • threadpoolctl==2.1.0
  • joblib==0.14.0
  • scikit-learn==1.0.1
  • numpy==1.21.5

Embedded Python Executionプロセスで検出できるように、パッケージを$ORACLE_HOME/OML4Py/modulesにインストールします。

pip3.9 install pandas==1.3.4 --target=$ORACLE_HOME/oml4py/modules
pip3.9 install scipy==1.7.3 --target=$ORACLE_HOME/oml4py/modules
pip3.9 install matplotlib==3.3.3 --target=$ORACLE_HOME/oml4py/modules
pip3.9 install cx_Oracle==8.1.0 --target=$ORACLE_HOME/oml4py/modules
pip3.9 install threadpoolctl==2.1.0 --target=$ORACLE_HOME/oml4py/modules
pip3.9 install joblib==0.14.0 --target=$ORACLE_HOME/oml4py/modules
pip3.9 install scikit-learn==1.0.1 --no-deps --target=$ORACLE_HOME/oml4py/modules
pip3.9 uninstall numpy 
pip3.9 install numpy==1.21.5 --target=$ORACLE_HOME/oml4py/modules

Oracle Database用のOML4Pyサーバーのインストール

Oracle Technology NetworkからOracle Machine Learning for Python をダウンロードし、インストールします。
インストールの前提としてこれまでの作業が完了している必要があります。

  • インターネットへの接続
  • Python 3.9.5
  • OML4Pyサポート・パッケージ
  • Perl 5.8以上 (注:Perlにはperl-Envパッケージが必要)
  • サーバー・コンポーネントをインストールするディレクトリに対する書込み権限

Oracle Machine Learning for Pythonサーバー・インストール・ファイルのダウンロード

Oracle Technology NetworkからOracle Machine Learning for Python をダウンロードページにアクセスし、Serverモジュールをダウンロードします。
image.png

Oracle Machine Learning for Pythonサーバー・インストール・ファイルのインストール

$ORACLE_HOME/oml4pyディレクトリを作成し、ダウンロードしたファイルを展開、インストールします。
インストールPerlスクリプトによって、PYQSYSスキーマおよびユーザーが作成されます。

mkdir $ORACLE_HOME/oml4py
unzip oml4py-server-linux-x86_64-1.0.zip -d $ORACLE_HOME/oml4py
perl -Iserver server/server.pl --help
export PYTHONPATH=$ORACLE_HOME/oml4py/modules
perl -Iserver server/server.pl

「PDB1」にインストールする場合、 --pdb PDB1 を指定(もしくはプロンプトに入力します)
プロンプトに PYQSYSスキーマの表領域を入力します。

実行例

$ perl -Iserver server/server.pl --pdb PDB1

Oracle Machine Learning for Python 1.0 Server.

Copyright (c) 2018, 2022 Oracle and/or its affiliates. All rights reserved.

Checking platform .................. Pass
Checking ORACLE_HOME ............... Pass
Checking ORACLE_SID ................ Pass
Checking sqlplus ................... Pass
Checking ORACLE instance ........... Pass
Checking CDB/PDB ................... Fail
  ERROR: cannot install OML4P in a root container
  PDB to use for OML4P installation [list]:DB1228_PDB1
Checking CDB/PDB ................... Pass
Checking OML4P Server .............. Pass
Checking Python .................... Pass
Checking module dependencies ....... Pass
Checking Python libraries .......... Pass
Checking OML4P version ............. Pass

Choosing PYQSYS tablespaces
  PERMANENT tablespace to use for PYQSYS [list]: SYSTEM
  TEMPORARY tablespace to use for PYQSYS [list]: TEMP

Current configuration
  ORACLE_HOME ...................... /u01/app/oracle/product/19.0.0.0/dbhome_1
  ORACLE_SID ....................... orcl
  PDB .............................. PDB1
  Python Version ................... 3.9.5
  PYTHONHOME ....................... /u01/app/oracle/product/19.0.0.0/dbhome_1/python

  Existing OML4P data and code ..... None
  Existing OML4P AutoML component .. None
  Existing OML4P embed component ... None
  Existing OML4P module version .... None

  PYQSYS PERMANENT tablespace ...... SYSTEM
  PYQSYS TEMPORARY tablespace ...... TEMP

  Operation ........................ Install/Upgrade

Proceed? [yes]yes

Copying embedded python libraries ... Pass
Processing ./server/oml-1.0-cp39-cp39-linux_x86_64.whl
Installing collected packages: oml
Successfully installed oml-1.0
Configuring the database ............ Pass

Done
$

Oracle Database用のOML4Pyサーバーのインストールの確認

$ sqlplus / as sysdba
SQL> alter session set container=PDB1;
SQL> SELECT * FROM sys.pyq_config;

NAME
--------------------------------------------------------------------------------
VALUE
--------------------------------------------------------------------------------
PYTHONHOME
/u01/app/oracle/product/19.0.0.0/dbhome_1/python

PYTHONPATH
/u01/app/oracle/product/19.0.0.0/dbhome_1/oml4py/modules

VERSION
1.0

PLATFORM
ODB

DSWLIST
oml.*;pandas.*;numpy.*;matplotlib.*;sklearn.*

OML4Py を使用するデータベースユーザーの作成とデータベース権限の付与

$ORACLE_HOME/oml4py/serverのpyquser.sqlを実行し、ユーザ(oml_user)を作成します。
引数として、ユーザ名、表領域と表領域Quota、PYQADMINロールを指定します。

$ cd /u01/app/oracle/product/19.0.0.0/dbhome_1/oml4py/server
$ sqlplus / as sysdba
SQL> alter session set container=PDB1;
SQL> @pyquser.sql oml_user USERS TEMP unlimited pyqadmin


Enter value for password: <Passwordを入力>

old   1: create user &&1 identified by &password
new   1: create user oml_user identified by <Password>
old   2: default tablespace &&2
new   2: default tablespace USERS
old   3: temporary tablespace &&3
new   3: temporary tablespace TEMP
old   4: quota &&4 on &&2
new   4: quota unlimited on USERS

User created.

old   4:     'create procedure, create mining model to &&1';
new   4:     'create procedure, create mining model to oml_user';
old   6:   IF lower('&&5') = 'pyqadmin' THEN
new   6:   IF lower('pyqadmin') = 'pyqadmin' THEN
old   7:     execute immediate 'grant PYQADMIN to &&1';
new   7:     execute immediate 'grant PYQADMIN to oml_user';

PL/SQL procedure successfully completed.

必要なデータベース権限を付与します。

GRANT CREATE TABLE to oml_user;
GRANT CREATE VIEW to oml_user;
GRANT CREATE PROCEDURE to oml_user;
GRANT CREATE MINING MODEL to oml_user;
GRANT EXECUTE ON CTXSYS.CTX_DDL to oml_user;
GRANT PYQADMIN to oml_user;

OML4Pyクライアントのインストール

OML4Pyクライアントをインストールします。

前提条件

  • インターネットへの接続
  • クライアントをインストールするディレクトリに対する書込み権限

OML4Pyに必要なライブラリの確認

以下のLinuxライブラリをインストールされていることを確認、インストールします。
perl-Env、libffi-devel、openssl、openssl-devel、tk-devel、xz-devel、zlib-devel、bzip2-devel、readline-devel、libuuid-devel、ncurses-devel

$ sudo yum install perl-Env libffi-devel openssl openssl-devel tk-devel xz-devel zlib-devel bzip2-devel readline-devel libuuid-devel ncurses-devel

Python 3.9.5のインストール

Server側を参照しPython 3.9.5 をインストール

mkdir /usr/lib/oracle/19.22/client64/python
tar -xvzf Python-3.9.5.tgz --strip-components=1 -C /usr/lib/oracle/19.22/client64/python
cd /usr/lib/oracle/19.22/client64/python
./configure --enable-shared --prefix=/usr/lib/oracle/19.22/client64/python

make clean; make
make altinstall

export PYTHONHOME=/usr/lib/oracle/19.22/client64/python
export PATH=$PYTHONHOME/bin:$PATH
export LD_LIBRARY_PATH=$PYTHONHOME/lib:$LD_LIBRARY_PATH

ln -s python3.9 python3
python3 -m pip install --upgrade pip

Oracle Instant Clientのインストール

Oracle Technology Networkから Oracle Instant Client Basicモジュールをダウンロードし、インストールします。

sudo dnf install libnls
sudo rpm -ivh oracle-instantclient19.22-basic-19.22.0.0.0-1.x86_64.rpm

OML4Pyクライアント・インストール・ファイルのインストール

Oracle Technology NetworkからOracle Machine Learning for Python をダウンロードページにアクセスし、Clientモジュールをダウンロードします。
image.png

mkdir /usr/lib/oracle/19.22/client64/oml4py
unzip oml4py-client-linux-x86_64-1.0.zip -d /usr/lib/oracle/19.22/client64/oml4py
cd /usr/lib/oracle/19.22/client64/oml4py
perl -Iclient client/client.pl --help
perl -Iclient client/client.pl

実行例

$ perl -Iclient client/client.pl

Oracle Machine Learning for Python 1.0 Client.

Copyright (c) 2018, 2022 Oracle and/or its affiliates. All rights reserved.
Checking platform .................. Pass
Checking Python .................... Pass
Checking dependencies .............. Pass
Checking OML4P version ............. Pass
Current configuration
  Python Version ................... 3.9.5
  PYTHONHOME ....................... /usr/lib/oracle/19.22/client64/python
  Existing OML4P module version .... None

  Operation ........................ Install/Upgrade

Proceed? [yes]yes

Processing ./client/oml-1.0-cp39-cp39-linux_x86_64.whl
Installing collected packages: oml
Successfully installed oml-1.0

Done

OML4Pyサーバーへの接続とインストールの確認

Pythonセッションで、OML4Pyサーバーに接続し、関数を名前で呼び出し、動作を確認します。
(パラメータの値を環境にあわせます)

import oml
oml.connect(user='oml_user', password='oml_user_password', host='myhost', port=1521, service_name="pdb1")

ユーザー定義Python関数を作成し、OML4Pyスクリプト・リポジトリに格納します。

oml.script.create("TEST", func='def func():return 1 + 1', overwrite=True)

oml.do_eval関数を使用して、ユーザー定義関数をコールします。

res = oml.do_eval(func='TEST')
res
2

テストが終了したら、ユーザー定義関数を削除します。

oml.script.drop("TEST")

おわりに

Autonomous Database以外のOracle DatabaseにOracle Machine Learning for Python(OML4Py)をインストールすることができました。

参考情報

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0