Using PostgreSQL for JSON Storage

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

内容简介: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》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

用户体验的要素

用户体验的要素

Jesse James Garrett / 范晓燕 / 机械工业出版社 / 2007年10月 / 25.00

这不是一本关于“怎样做(How-to)”的书。有很多很多讨论如何建设网站的书,这本不是。 这不是一本关于技术的书。在这里你找不到一行代码。 这不是一本有答案的书。相反,这本书说的是“如何提出正确的问题”。 这本书将告诉你,在你阅读其他书籍的之前,你需要提前了解什么。如果你需要一个大的概念,如果你需要了解用户体验设计师所做出的决策的环境,这本书很适合你。 这本书经过精心设计,......一起来看看 《用户体验的要素》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具