0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

DockerでOracleのデータをCSVに出力する

Posted at

クエリを与えたら、結果をCSVに出力するDockerです。

Dockerfile と query.py を同じディレクトリに配置してビルドします。

$ docker build -t ora_export .
Dockerfile
FROM oraclelinux:7-slim

# Proxy必要の場合
# ENV https_proxy=http://proxy.hoge.com

RUN yum -y install oracle-release-el7 && \
    yum -y install oracle-instantclient19.3-basiclite && \
    yum -y install vim && \
    yum -y install python36 && \
    python3.6 -m pip install cx_Oracle pandas && \
    rm -rf /var/cache/yum

WORKDIR /myapp
ADD query.py /myapp

ENTRYPOINT ["python3.6", "query.py"]
query.py
import sys
import cx_Oracle as cxo
import pandas as pd

args = sys.argv
user = args[1]
password = args[2]
dsn = args[3]

connection = cxo.connect(user=user, password=password, dsn=dsn)

query = sys.stdin.read()
pd_data_frame = pd.read_sql(query, con=connection)
pd_data_frame.to_csv(sys.stdout, index=False, encoding="utf-8")

connection.close()

使い方のサンプルです。
query.sql を作成してコマンドを実行すると、export.csv が出力されます。

query.sql
select systimestamp from dual
コマンド
$ cat query.sql | docker run --rm -i ora_export username password 192.168.1.103:1521/ORCL > export.csv
export.csv
SYSTIMESTAMP
2021-05-11 17:56:07.044225

参考にした記事

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?