源:http://www.oschina.net/question/12_106368?sort=default&p=2#answers
评:
Postgres 9.3 目前正在紧锣密鼓的开发中,该版本值得关注的一个新特性就是 JSON 数据类型。在看完 new functions for data generation 这篇文章后,我们来看看在 commit 记录中关于新的 JSON 特性的说明:
commit a570c98d7fa0841f17bbf51d62d02d9e493c7fcc
Author: Andrew Dunstan
Date: Fri Mar 29 14:12:13 2013 -0400
Add new JSON processing functions and parser API.
The JSON parser is converted into a recursive descent parser, and
exposed for use by other modules such as extensions. The API provides
hooks for all the significant parser event such as the beginning and end
of objects and arrays, and providing functions to handle these hooks
allows for fairly simple construction of a wide variety of JSON
processing functions. A set of new basic processing functions and
operators is also added, which use this API, including operations to
extract array elements, object fields, get the length of arrays and the
set of keys of a field, deconstruct an object into a set of key/value
pairs, and create records from JSON objects and arrays of objects.
Catalog version bumped.
Andrew Dunstan, with some documentation assistance from Merlin Moncure.
基于存储的 JSON 数据,该提交还引入新的 API、运算符和函数用来操作 JSON 数据,共有 4 个运算符和8个新的函数,本文只简单介绍 4 个新的运算符。
下列的数据集用于文章中所有实验:
1 |
postgres=# CREATE TABLE aa (a int , b json);
|
3 |
postgres=# INSERT INTO aa VALUES (1, '{"f1":1,"f2":true,"f3":"Hi I' 'm \"Daisy\""}' );
|
5 |
postgres=# INSERT INTO aa VALUES (2, '{"f1":{"f11":11,"f12":12},"f2":2}' );
|
7 |
postgres=# INSERT INTO aa VALUES (3, '{"f1":[1,"Robert \"M\"",true],"f2":[2,"Kevin \"K\"",false]}' );
|
第一个运算符是 “->”, 用来直接从 JSON 数据库获取字段值,使用文本值来标注字段的键:
1 |
postgres=# SELECT b-> 'f1' AS f1, b-> 'f3' AS f3 FROM aa WHERE a = 1;
|
也可以使用多个键来获取数据或者另外一个子集数据:
01 |
postgres=# SELECT b-> 'f1' -> 'f12' AS f12 FROM aa WHERE a = 2;
|
06 |
postgres=# SELECT b-> 'f1' AS f1 FROM aa WHERE a = 2;
|
另外一个更有趣的方法,当使用整数作为键时,你可直接从存储的数组获取数据:
1 |
postgres=# SELECT b-> 'f1' ->0 as f1_0 FROM aa WHERE a = 3;
|
第二个运算符是 “->>”. 与 “->” 不同的是,该运算符返回指定的文本,“->>” 返回纯文本。
01 |
postgres=# SELECT b->> 'f3' AS f1 FROM aa WHERE a = 1;
|
06 |
postgres=# SELECT b->' f3 ' AS f1 FROM aa WHERE a = 1;
|
与 “->” 相同的是,->> 也可以使用整数或者文本作为键,使用整数时代表在数组中的位置:
01 |
postgres=# SELECT b-> 'f1' ->>1 as f1_0 FROM aa WHERE a = 3;
|
06 |
postgres=# SELECT b-> 'f1' ->1 as f1_0 FROM aa WHERE a = 3;
|
当然,你不能通过字段名来获取数据中的数据:
1 |
postgres=# SELECT b-> 'f1' ->> '1' as f1_0 FROM aa WHERE a = 3;
|
2 |
ERROR: cannot extract field from a non-object
|
同样你不能使用元素数值来获取字段一样:
1 |
postgres=# SELECT b->1 as f1_0 FROM aa WHERE a = 3;
|
2 |
ERROR: cannot extract array element from a non-array
|
最后两个运算符是 “#>” 和 “#>>”. 用来直接获取数组中的元素而无需使用前面两种方法“column->’$FIELD’->$INT_INDEX. 这可以让你的查询更加具备可读性。
01 |
postgres=# SELECT b#> '{f1,1}' as f1_0 FROM aa WHERE a = 3;
|
06 |
postgres=# SELECT b#>> '{f1,1}' as f1_0 FROM aa WHERE a = 3;
|
“#>” 使用有效的 JSON 格式获取文本数据,而 “#>>” 则返回纯文本。
总结一句,这些新的操作符大大方便了很多应用对 JSON 数据的操作。
via otacoo
分享到:
相关推荐
零基础PostgreSQL9.3 DBA最佳实战培训PPT 580页,适合新手入门到运维PostgreSQL数据库系统。
postgresql9.3 jdbc驱动...
并且官方的内置了PGAdmin3图形化工具,增大了客户误操作的机会,也在安装最后默认安装他的那个builder工具,很麻烦,默认是C盘,可是很多用户C盘常用来装系统,于是我自己做了一个安装包,默认装到D盘并且没有pg...
postgresql9.3的官方说明文档,可以很好学习postgresql和postgis的相关技术。
digoal postgresql 9.3 new features extract,
postgresql 9.3 jdbc驱动
具有特殊功能的PostgreSQL 9.3 容器。 可用于从提取。 特征: 基于和 使用给定的密码自动创建您的 PostgreSQL超级用户 集成 将您的公钥添加到容器的 SSH 服务器以便于访问 向主机公开端口22 (SSH) 和 5432 ...
postgresql-9.3-1101.jdbc41.jar包 android连接postgresql安装jar包
PostgreSQL全套课程,全套24.73G,包含免密视频、课件、相关文档,课程目录: 01.PostgreSQL9 从零开始学...03.PostgreSQL 9.3 DBA5天培训 24课 04.PostgreSQL 9.3 优化培训视频 19课 05.DBA团队每天16点技术分享 7课
PostgreSQL 9.3.1 中文文档
sqoop连接Postgres SQL 的驱动包postgresql-9.3-1102-jdbc41.jar。 利用sqoop导出PostgresSQL的数据时所需的驱动包,只需要将该包放到hive或sqoop的lib目录下即可。
同样,PostgreSQL 可以用许多方法扩展,比如, 通过增加新的数据类型、函数、操作符、聚集函数、索引方法、过程语言。并且,因为许可证的灵活,任何人都可以以任何目的免费使用、修改、和分发 PostgreSQL,不管是私用、 ...
由于我们没有什么特殊需求所在在此选择较为简单的方式——二进制安装包安装,二进制包安装的方法一般都是通过不同发行版本的Linux下的包管理器进行的,例如Debian和Ubuntu下是使用apt-get命令或aptitude命令来安装,...
这个版本是ubuntu14.04 lts源中的postgre对应的源码
postgresql-9.3.4-3-windows 64位操作系统 我安装时可以的
postgresql-9.3 文档
postgres jdbc 驱动, postgresql-9.3-1100.jdbc3.jar
同样,PostgreSQL 可以用许多方法扩展,比如, 通过增加新的数据类型、函数、操作符、聚集函数、索引方法、过程语言。并且,因为许可证的灵活,任何人都可以以任何目的免费使用,修改,和分发 PostgreSQL, 不管是...
厨师独奏食谱 Zabbix 2.2 和 PostgreSQL9.3 #描述 本说明书包含 Zabbix2.2 服务器和 Postgresql9.3。 并且它不包含 Zabbix Agent。 它在 Sakura Internet VPS 上进行了测试。(CentOS6.5)我用其他厨师食谱...