AWS のクエリー言語 PartiQL を試してみたかったので、Docker に環境を作ってみた。
自分用の覚書。
環境
PartiQL を触ってみたいだけなので、Docker で環境を作成。
PartiQL の実行には、JDK が必要なので、無償の OpenJDK をベースにする。
DockerFile
openjdk の最新版に、PartiQL のv0.1.0を取得・展開して、パスを通します。
DockerFile
FROM openjdk
MAINTAINER rex0220
LABEL title="partiQLImage"\
version="0.1"\
description="This is a sample of partiQL."
RUN mkdir /partiql
WORKDIR /partiql
RUN curl -o /tmp/tmp.tgz -OL https://github.com/partiql/partiql-lang-kotlin/releases/download/v0.1.0-alpha/partiql-cli-0.1.0.tgz
RUN tar xfz /tmp/tmp.tgz
RUN rm /tmp/tmp.tgz
RUN mv partiql-cli-0.1.0 cli
ENV PATH $PATH:/partiql/cli/bin
WORKDIR /
image の作成
PowerShell
partiql > docker image build -f Dockerfile -t partiql . Sending build context to Docker daemon 2.56kB
Step 1/11 : FROM openjdk
latest: Pulling from library/openjdk
a316717fc6ee: Pull complete 809137453b07: Pull complete 35e9b3466a6b: Pull complete Digest: sha256:3ae5d7bf57be05ea4bb57bde5ca0322f016a5ed7c5bca824bdc8f317fbd15af5
Status: Downloaded newer image for openjdk:latest
---> 8a8b42cf3239
Step 2/11 : MAINTAINER rex0220
---> Running in 4891cf740df9
Removing intermediate container 4891cf740df9
---> bcf856520ad5
Step 3/11 : LABEL title="partiQLImage" version="0.1" description="This is a sample of partiQL."
---> Running in f397baf23d14
Removing intermediate container f397baf23d14
---> ce20e927a8ed
Step 4/11 : RUN mkdir /partiql
---> Running in 26c7240ccd46
Removing intermediate container 26c7240ccd46
---> 7dabaa9a0035
Step 5/11 : WORKDIR /partiql
---> Running in a73ea3298c43
Removing intermediate container a73ea3298c43
---> 7099cb4491ba
Step 6/11 : RUN curl -o /tmp/tmp.tgz -OL https://github.com/partiql/partiql-lang-kotlin/releases/download/v0.1.0-alpha/partiql-cli-0.1.0.tgz
---> Running in a98dced60903
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 612 0 612 0 0 94 0 --:--:-- 0:00:06 --:--:-- 154
100 3669k 100 3669k 0 0 114k 0 0:00:32 0:00:32 --:--:-- 199k
Removing intermediate container a98dced60903
---> 93b6a532f82c
Step 7/11 : RUN tar xfz /tmp/tmp.tgz
---> Running in 12bf7307cf54
Removing intermediate container 12bf7307cf54
---> 75751ee59cb6
Step 8/11 : RUN rm /tmp/tmp.tgz
---> Running in 91513f67a2db
Removing intermediate container 91513f67a2db
---> 834c60530802
Step 9/11 : RUN mv partiql-cli-0.1.0 cli
---> Running in a3231e1618b4
Removing intermediate container a3231e1618b4
---> 6240d570721e
Step 10/11 : ENV PATH $PATH:/partiql/cli/bin
---> Running in 17d10e57e4e5
Removing intermediate container 17d10e57e4e5
---> 9be604549cfb
Step 11/11 : WORKDIR /
---> Running in 83e58d2939ef
Removing intermediate container 83e58d2939ef
---> cbf3c4af5ce6
Successfully built cbf3c4af5ce6
Successfully tagged partiql:latest
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.
partiql >
作成した image
ベースにした openjdk と partiql の image が作成されます。
PowerShell
partiql > docker images REPOSITORY TAG IMAGE ID CREATED SIZE
partiql latest cbf3c4af5ce6 4 minutes ago 503MB
openjdk latest 8a8b42cf3239 4 weeks ago 490MB
partiql の実行
container を立ち上げて、partiql の実行を行うと、入力モードになる。
リテラルの配列を select すると、json 形式で返ってくる。
- partiql の入力モードから戻るには
- Control+D in macOS or Unix
- Control+C on Windows
PowerShell
partiql > docker container run --rm -it partiql bash
bash-4.2# partiql
Welcome to the PartiQL REPL!
PartiQL> select * from [1,2,3,4]
|
==='
<<
{
'_1': 1
},
{
'_1': 2
},
{
'_1': 3
},
{
'_1': 4
}
>>
---
OK! (112 ms)
VALUE のみの select
いろいろ select の指定があり、VALUE を指定すると 値のみ返ってくる。
PartiQL
PartiQL> SELECT VALUE v FROM [1,2,3] AS v
|
==='
<<
1,
2,
3
>>
---
OK! (12 ms)
サンプルコード
Tutorial/code/ には、サンプルの sql, data, output がある。
shell
bash-4.2# pwd
/partiql/cli/Tutorial/code
bash-4.2# ls
q10.env q11.sql q13.output q15.env q16.sql q18.output q2.env q3.sql q5.output q7.env q8.output tutorial-all-data.env
q10.output q12.env q13.sql q15.output q17.env q18.sql q2.output q4.env q5.sql q7.output q8.sql
q10.sql q12.output q14.env q15.sql q17.output q1.env q2.sql q4.output q6.env q7.sql q9.env
q11.env q12.sql q14.output q16.env q17.sql q1.output q3.env q4.sql q6.output q8.env q9.output
q11.output q13.env q14.sql q16.output q18.env q1.sql q3.output q5.env q6.sql q8.out q9.sql
bash-4.2# cat q1.env
{
'hr': {
'employees': <<
-- a tuple is denoted by { ... } in the PartiQL data model
{ 'id': 3, 'name': 'Bob Smith', 'title': null },
{ 'id': 4, 'name': 'Susan Smith', 'title': 'Dev Mgr' },
{ 'id': 6, 'name': 'Jane Smith', 'title': 'Software Eng 2'}
>>
}
}
bash-4.2# cat q1.sql
SELECT e.id,
e.name AS employeeName,
e.title AS title
FROM hr.employees e
WHERE e.title = 'Dev Mgr'
bash-4.2# cat q1.output
<<
{
'id': 4,
'employeeName': 'Susan Smith',
'title': 'Dev Mgr'
}
>>
---
OK! (16 ms)
サンプルコード実行方法
cat q1.sql|partiql -e q1.env
-i オプションは、入力データファイル指定
クエリーは、-q オプションでクエリー文字列を指定、または標準入力で指定
※ partiql の -i オプションで データファイル を指定できると思うが、エラーになる。
暫定対応として、標準入力で sql 、-e オプションでデータファイルを指定したら 実行できた。
q1.sql
SELECT e.id,
e.name AS employeeName,
e.title AS title
FROM hr.employees e
WHERE e.title = 'Dev Mgr'
sh
bash-4.2# cat q1.sql|partiql -e q1.env
Welcome to the PartiQL REPL!
PartiQL> | | | | | ==='
<<
{
'id': 4,
'employeeName': 'Susan Smith',
'title': 'Dev Mgr'
}
>>
---
OK! (37 ms)
PartiQL> bash-4.2#
DockerHub への push
tag を変更して、DockerHub に push
rex0220/partiql で、公開。
PoweShell
partiql > docker images REPOSITORY TAG IMAGE ID CREATED SIZE
partiql latest cbf3c4af5ce6 About an hour ago 503MB
openjdk latest 8a8b42cf3239 4 weeks ago 490MB
partiql > docker tag cb rex0220/partiql partiql > docker images REPOSITORY TAG IMAGE ID CREATED SIZE
partiql latest cbf3c4af5ce6 About an hour ago 503MB
rex0220/partiql latest cbf3c4af5ce6 About an hour ago 503MB
openjdk latest 8a8b42cf3239 4 weeks ago 490MB
partiql > docker login Authenticating with existing credentials...
Login Succeeded
partiql > docker push rex0220/partiql
The push refers to repository [docker.io/rex0220/partiql]
2f514a96e4d8: Pushed
149a07636d29: Pushed
d417a1127c68: Pushed
9564382de366: Pushed
cf48a4dacf9e: Pushed
ed6eb6eb5933: Mounted from library/openjdk
bad7e554be9e: Mounted from library/openjdk
4fee40bcfecf: Mounted from library/openjdk
latest: digest: sha256:3091c015bb48d6871473e45448798ceec894ba6e945eff6bb7fe17d498b4d24e size: 2001
あとがき
SQL で出来ない対象ソースや指定があり、面白そう。