Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PartiQLを使ってみた / friday13json-niigata-12

kasacchiful
September 13, 2019

PartiQLを使ってみた / friday13json-niigata-12

2019/09/13(金)に開催された「JSON日の金曜日@新潟 12週目」 に発表した資料です。

kasacchiful

September 13, 2019
Tweet

More Decks by kasacchiful

Other Decks in Programming

Transcript

  1. PartiQLとは何か https://partiql.org/ AWSが今年の8⽉にオープンソースとして公開 https://aws.amazon.com/jp/blogs/opensource/announcing-partiql-one-query- language-for-all-your-data/ SQL互換 RDB以外に、KVS / JSON /

    CSV など、様々なデータソースに対して横断的に検索できる 問い合わせ⾔語およびそのリファレンス実装 参考: AWS、SQL互換の新問い合わせ⾔語「PartiQL」をオープンソースで公開。RDB、 KVS、JSON、CSVなどをまとめて検索可能 - Publickey
  2. データ Tutorial/code/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'} >> } }
  3. REPL起動と結果 > bin/partiql -e Tutorial/code/q1.env Welcome to the PartiQL REPL!

    PartiQL> SELECT e.id, e.name AS employeeName, e.title AS title FROM hr.employees e WHERE e.title = 'Dev Mgr' | | | | | ===' << { 'id': 4, 'employeeName': 'Susan Smith', 'title': 'Dev Mgr' } >> --- OK! (50 ms)
  4. 以下は、 hr.employees テーブルをPartiQLデータモデルで⽰した場合 { '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'} >> } } デリミタ: << .. >> unorderd collection (SQLのテーブルのようなデータを扱う場合)
  5. JSONで表すとこんな感じ これをPartiQLで扱うには、PartiQLデータモデルとして変換した上で使⽤することになる { "hr" : { "employees": [ { "id":

    3, "name": "Bob Smith", "title": null }, { "id": 4, "name": "Susan Smith", "title": "Dev Mgr" }, { "id": 6, "name": "Jane Smith", "title": "Software Eng 2"} ] } } JSONはダブルクォーテーションだが、PartiQLはシングルクォーテーション (SQL互換の ため)
  6. 変換させる [ ] → << >> " → ' {

    'hr' : { 'employees': << { 'id': 3, 'name': 'Bob Smith', 'title': null }, { 'id': 4, 'name': 'Susan Smith', 'title': 'Dev Mgr' }, { 'id': 6, 'name': 'Jane Smith', 'title': 'Software Eng 2'} >> } }
  7. ネストするデータはどうか Tutorial/code/q2.env { 'hr': { 'employeesNest': << { 'id': 3,

    'name': 'Bob Smith', 'title': null, 'projects': [ { 'name': 'AWS Redshift Spectrum querying' }, { 'name': 'AWS Redshift security' }, { 'name': 'AWS Aurora security' } ] -- タブルの中に配列がある }, { 'id': 4, 'name': 'Susan Smith', 'title': 'Dev Mgr', 'projects': [] }, { 'id': 6, 'name': 'Jane Smith', 'title': 'Software Eng 2', 'projects': [ { 'name': 'AWS Redshift security' } ] } >> } }
  8. PartiQLクエリ SELECT e.name AS employeeName, p.name AS projectName FROM hr.employeesNest

    AS e, e.projects AS p -- ここが標準SQL の拡張表記 WHERE p.name LIKE '%security%'
  9. 結果 bin/partiql -e Tutorial/code/q2.env Welcome to the PartiQL REPL! PartiQL>

    SELECT e.name AS employeeName, p.name AS projectName FROM hr.employeesNest AS e, e.projects AS p WHERE p.name LIKE '%security%' | | | | | ===' << { 'employeeName': 'Bob Smith', 'projectName': 'AWS Redshift security' }, { 'employeeName': 'Bob Smith', 'projectName': 'AWS Aurora security' }, { 'employeeName': 'Jane Smith', 'projectName': 'AWS Redshift security' } >> --- OK! (69 ms)
  10. 他にもこんなこともできるよ (サブクエリ) SELECT e.name AS employeeName FROM hr.employeesNest AS e

    WHERE EXISTS ( SELECT * FROM e.projects AS p WHERE p.name LIKE '%security%') return << { 'employeeName': 'Bob Smith' }, { 'employeeName': 'Jane Smith' } >>
  11. 他にもこんなこともできるよ (Group by 句 / Having 句) SELECT e.name AS

    employeeName FROM hr.employeesNest AS e, e.projects AS p WHERE p.name LIKE '%security%' GROUP BY e.id, e.name HAVING COUNT(*) > 1 return << { 'employeeName': 'Bob Smith' } >>
  12. 他にもこんなこともできるよ (集計) SELECT e.name AS employeeName, COUNT(p.name) AS queryProjectsNum FROM

    hr.employeesNest e LEFT JOIN e.projects AS p ON p.name LIKE '%querying%' GROUP BY e.id, e.name return << { 'employeeName': 'Bob Smith', 'queryProjectsNum': 1 }, { 'employeeName': 'Susan Smith', 'queryProjectsNum': 0 }, { 'employeeName': 'Jane Smith', 'queryProjectsNum': 0 } >>