Dockerコンテナ

文字コードを SQL_ASCII から UTF-8 に変更する

JSONBを格納するテーブルを作る

$ psql
psql (9.4beta2)
Type "help" for help.

docker=# CREATE DATABASE sample;
CREATE DATABASE
docker=# \c sample
You are now connected to database "sample" as user "docker".
sample=# CREATE TABLE articlej_tbl (
sample(#   id bigserial primary key,
sample(#   onCreate timestamp default NULL,
sample(#   onUpdate timestamp default NULL,
sample(#   contents jsonb NOT NULL
sample(# );
CREATE TABLE
sample=# CREATE INDEX articlej_tbl_idx_contents ON articlej_tbl USING gin (contents);
CREATE INDEX
sample=#

JSONBの格納

docker@42649b72cb9c:~$ psql -n -d sample
psql (9.4beta2)
Type "help" for help.

sample=# \d
                List of relations
 Schema |        Name         |   Type   | Owner
--------+---------------------+----------+--------
 public | articlej_tbl        | table    | docker
 public | articlej_tbl_id_seq | sequence | docker
(2 rows)

sample=# INSERT INTO articlej_tbl (onCreate,onUpdate,contents) VALUES (
now(),
now(),
'{"title" : "ポスト「京」",
"category" : ["Computer","Science"],
"format" : "HTML",
"author" : "浅川 直輝",
"publisher" : "日経コンピュータ",
"date" : "2014/10/02",
"url" : "http://itpro.nikkeibp.co.jp/atcl/news/14/100201173/",
"keywords" : ["理研","京","富士通","SPARC","64","X","TMSC"],
"summary" : "理化学研究所は2014年10月1日、理研のスパコン「京」の後継となるエクサ級(1エクサFLOPS前後)のスパコンについて、
基本設計を共同で行う事業者として富士通を選定したと発表した。",
"memo" : ""}'
);
INSERT 0 1
sample=# INSERT INTO articlej_tbl (onCreate,onUpdate,contents) VALUES (
now(),
now(),
'{"title" : "火星ミッション模擬実験、参加者に聞く",
"category" : ["Space","Planet"],
"format" : "PDF",
"author" : "Kelly McMillan",
"publisher" : "National Geographic",
"date" : "2014/10/16",
"url" : "http://www.nationalgeographic.co.jp/news/news_article.php?file_id=20141016003",
"keywords" : ["火星","ミッション","NASA"],
"summary" : "NASAの支援により、ハワイ大学マノア校が実施する宇宙探査模擬実験プロジェクト「HI-SEAS」(Hawaii Space Exploration Analog and Simulation)の一環で、
火星の宇宙ステーションでの生活を模した実験が始まる。",
"memo" : ""}'
);
INSERT 0 1
sample=#

検索

select * from articlej_tbl;

select.png

select id from articlej_tbl where contents @> '{"format" : "HTML"}'; (JSONの要素で絞込)

sample=# select id from articlej_tbl where contents @> '{"format" : "HTML"}';
 id
----
  1
(1 row)

sample=# select id from articlej_tbl where contents @> '{"format" : "PDF"}';
 id
----
  2
(1 row)

select '{"a":1, "b":2}'::jsonb ? 'b'; (Key を含むか?)

sample=# select '{"a":1, "b":2}'::jsonb ? 'b';
 ?column?
----------
 t
(1 row)

sample=# select '{"a":1, "b":2}'::jsonb ? 'c';
 ?column?
----------
 f
(1 row)

select '{"a":1, "b":2, "c":3}'::jsonb ?| array['b', 'd']; (b, d いずれかの key を含む)

sample=# select '{"a":1, "b":2, "c":3}'::jsonb ?| array['b', 'd'];
 ?column?
----------
 t
(1 row)

select '{"a":1, "b":2, "c":3}'::jsonb ?& array['b', 'd']; (b, d どちらの key を含む)

sample=# select '{"a":1, "b":2, "c":3}'::jsonb ?& array['b', 'd'];
 ?column?
----------
 f
(1 row)

現時点では、JSONB の比較演算子 (Operator) は、@>、<@、?、?|、?& だけ

JSONからの要素の取り出し

select contents->'keywords' from articlej_tbl; (JSON から JSON オブジェクトを取り出す)

sample=# select contents->'keywords' from articlej_tbl;
                          ?column?
------------------------------------------------------------
 ["理研", "京", "富士通", "SPARC", "64", "X", "TMSC"]
 ["火星", "ミッション", "NASA"]
(2 rows)

select contents->'keywords'->1 from articlej_tbl; (JSON 配列から n 番目の要素オブジェクトを取り出す)

sample=# select contents->'keywords'->1 from articlej_tbl;
     ?column?
-------------------
 "京"
 "ミッション"
(2 rows)

select contents->>'keywords' from articlej_tbl; (JSON から TEXT を取り出す)

sample=# select contents->>'keywords' from articlej_tbl;
                          ?column?
------------------------------------------------------------
 ["理研", "京", "富士通", "SPARC", "64", "X", "TMSC"]
 ["火星", "ミッション", "NASA"]
(2 rows)

sample=# select pg_typeof(contents->>'keywords') from articlej_tbl;
 pg_typeof
-----------
 text
 text
(2 rows)

sample=# select pg_typeof(contents->'keywords') from articlej_tbl;
 pg_typeof
-----------
 jsonb
 jsonb
(2 rows)

select contents->'keywords'->>1 from articlej_tbl; (JSON 配列から n 番目の TEXT を取り出す)

sample=# select contents->'keywords'->>1 from articlej_tbl;
    ?column?
-----------------
 京
 ミッション
(2 rows)

select contents#>'{keywords,1}' from articlej_tbl; (JSON からパス指定で JSON オブジェクトを取り出す)

sample=# select contents#>'{keywords,1}' from articlej_tbl;
     ?column?
-------------------
 "京"
 "ミッション"
(2 rows)

select contents#>>'{keywords,1}' from articlej_tbl; (JSON からパス指定で TEXT を取り出す)

sample=# select contents#>>'{keywords,1}' from articlej_tbl;
     ?column?
-------------------
 "京"
 "ミッション"
(2 rows)

Computer


添付ファイル: fileselect.png 1966件 [詳細]

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS   sitemap
Last-modified: 2014-12-11 (木) 02:14:56 (3414d)
Short-URL: http://hondou.homedns.org/pukiwiki/index.php?cmd=s&k=1c7975db3e
ISBN10
ISBN13
9784061426061