Using PostgreSQL for JSON Storage

栏目: IT技术 · 发布时间: 6年前

内容简介:Let's imagine a scenario in which you are dealing with JSON in your application and you want to store it in your database. You let out a heavy sigh and think, "I guess I am going to have to add something besides my favorite DB (Postgres) to myarchitecture.

Let's imagine a scenario in which you are dealing with JSON in your application and you want to store it in your database. You let out a heavy sigh and think, "I guess I am going to have to add something besides my favorite DB (Postgres) to myarchitecture. I wish I could just keep using PostgreSQL."

You start thinking that instead of one problem, now you have many. You're probably going to have to learn a new data query syntax, data creation statement, install new software, and worst of all for you as an app developer, running some new server in production. Now rather than developing your features, you are going to be spending valuable time learning, experimenting, and praying.

Well my friend, today is the day your wishes come true. In the blog post we will talk a little about how you can use PostgreSQL for all your JSON needs. We will also point you at some free learningresources so you candig in deeper.

JSON versus JSONB

Since 9.2, released in September 2012, PostgreSQL has had a JSON type. This original JSON type was not much more than just a simple storage field that let you dump JSON into your database table. It is just a simple text field that checks to make sure your JSON is well formed. Other than that it doesn't do much and I would not recommend using it.

With PostgreSQL release 9.4 in December 2014, a JSOB type was added. Though I joke that the B stands for better it really stands for Binary. When you put JSON data into a JSONB column, in addition to checking for well formed JSON, you now have the ability to indexing and query and retrieve portions of the document. Generally for all your work you should use JSONB unless you have a compelling reason not to. Here are a couple of nice discussions on the tradeoffs and choosing JSON versus JSONB

What can you do with JSONB in PostgreSQL

Say you had some JSON like:

{
    "person":
        {
            "first_name": "Steve",
            "last_name": "Pousty"
        },
    "score" : 100,
    "status" : "Awesome"
    "best_feature" : "humbleness"
}

Once you put it in a JSOB column named json_content (and make a GIN Index for faster queries) you can do all sorts of fun things. Please note that I will be using the JSONB navigation and function syntax found in PostgreSQL version 11. There was a major improvement to JSON document navigation and querying in version 12 which will be the focus of another blog post.

In the select part of the query

Let's get the users last name.

SELECT json_content ##> {person, last_name} 
FROM mytable;

The #> or #> is the JSON path navigator with the difference being #> returns JSON and the ##> returns the JSON text value.

In the Where clause

Using that same document navigation syntax, we can then combine that with the containment check. Just like the name sounds, we will check to see if the stored JSON contains the JSON we are looking for. For example, if we wanted to return only those records that had a status of awesome we would write:

SELECT json_content 
FROM mytable 
WHERE json_content @> '{"status": "Awesome"}':jsonb;

This

@>

operator looks for JSON that contains the JSON on the right side of the operator.

The Beauty of All This

The best part of working with JSON in PostgreSQL is that you get to leverage all the normal SQL you already love along with these JSON functions. SQL processing can be used to greatly reduce the amount of code you need in your application. For example, here is the query to get all the distinct status types in the table:

SELECT distinct(json_content ##> {status} as status, 
       count(json_content) 
FROM mytable 
GROUP BY json_content ##> {status};

Learn more

So if you are intrigued by what you saw here and want to learn more here are somegreat resources (if I do say so myself).

We have anonline tutorial to get your started with JSON in PostgreSQL. It is free and available 24/7 and  I also did  a live stream walking through the material above.

Let us know @crunchydata what you think of the material! I would also love to hear about how YOU are using JSON with PostgreSQL.

Thanks and happy coding.


以上所述就是小编给大家介绍的《Using PostgreSQL for JSON Storage》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

计算机算法基础

计算机算法基础

沈孝钧 / 机械工业出版社 / 2013-11 / 45.00元

计算机算法是计算机科学的一个重要分支,也是一个难点。本书作者根据自己20多年在国内、国外的教学与科研实践,系统地总结了计算机算法的设计与分析方法,覆盖了大部分最主要的算法技术,包括:分治法、贪心法、动态规划、图的遍历技术、穷举搜索等,涉及一系列重要的算法问题,包括排序问题、选择问题、最小生成树问题、最短路径问题、网络流问题、二分图的匹配问题、字符串的匹配问题和几何算法问题等,还介绍了问题本身的计算......一起来看看 《计算机算法基础》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具