在SQL Server 2016中存储JSON格式的数据

2023-05-16

In this article, I continue to review the exciting features available in SQL Server 2016. One such feature is the long awaited T-SQL support for JSON formatted data. In this article we take a look at how JSON support will impact data warehouse solutions.

在本文中,我将继续回顾SQL Server 2016中可用的激动人心的功能。这样的功能之一是期待已久的T-SQL对JSON格式数据的支持。 在本文中,我们将研究JSON支持如何影响数据仓库解决方案。

背景 (Background)

Since the advent of EXtensible Markup Language (XML) many modern web applications have focused on providing data that is both human-readable and machine-readable. From a relational database perspective, SQL Server kept up with these modern web applications by providing support for XML data in a form of an XML data type and several functions that could be used to parse, query and manipulate XML formatted data.

自从可扩展标记语言(XML)出现以来,许多现代的Web应用程序都集中于提供人类可读和机器可读的数据。 从关系数据库的角度来看,SQL Server通过以XML数据类型的形式提供对XML数据的支持以及一些可用于解析,查询和操作XML格式的数据的功能,从而与这些现代Web应用程序保持同步。

As a result of being supported in SQL Server, data warehouse solutions based off SQL Server were then able to source XML-based OLTP data into a data mart. To illustrate this point, let’s take a look at the XML representation of our fictitious Fruit Sales data shown in Figure 1.

由于受到SQL Server支持,因此基于SQL Server的数据仓库解决方案便能够将基于XML的OLTP数据提供给数据集市。 为了说明这一点,让我们看一下图1所示的虚拟Fruit Sales数据的XML表示。


To process this data in data warehouse, we would first have to convert it into relational format of rows and columns using T-SQL XML built-in functions such as the nodes() function. Figure 2 shows a script that makes use of the nodes() function to convert the sample data shown in Figure 1.

要在数据仓库中处理此数据,我们首先必须使用T-SQL XML内置函数(例如, nodes()函数)将其转换为行和列的关系格式。 图2显示了一个脚本,该脚本利用node()函数来转换图1所示的样本数据。


The results of the above script are shown in Figure 3 in a recognisable format for data warehouse.

以上脚本的结果在图3中以可识别的格式显示在数据仓库中。


Soon after XML became a dominant language for data interchange for many modern web applications, JavaScript Object Notation (JSON) was introduced as a lightweight data-interchange format that is more convenient for web applications to process than XML. Likewise most relational database vendors released newer versions of their database systems that included the support for JSON formatted data. Unfortunately, Microsoft SQL Server was not one of those vendors and up until SQL Server 2014, JSON data was not supported. Obviously this lack of support for JSON, created challenges for data warehouse environments that are based off SQL Server.

XML成为许多现代Web应用程序中用于数据交换的主要语言后不久,JavaScript对象表示法(JSON)被引入为一种轻量级的数据交换格式,比XML更便于Web应用程序处理。 同样,大多数关系数据库供应商也发布了其数据库系统的较新版本,其中包括对JSON格式数据的支持。 不幸的是,Microsoft SQL Server并不是这些供应商之一,直到SQL Server 2014才不支持JSON数据。 显然,这种对JSON的缺乏支持给基于SQL Server的数据仓库环境带来了挑战。

Although there were workarounds (i.e. using Json.Net) to addressing the lack of JSON support in SQL Server, there was always sense that these workarounds were inadequate, time-wasting, and were forcing data warehouse development teams to pick up a new skill (i.e. learn .Net). Fortunately, the release of SQL Server 2016 has ensured that development teams can throw away their JSON workarounds as JSON is supported in SQL Server 2016.

尽管存在一些解决方法(即使用Json.Net )来解决SQL Server中缺少JSON支持的问题,但总能感觉到这些解决方法不够充分,浪费时间,并且迫使数据仓库开发团队采用新技能(即学习.NET)。 幸运的是,SQL Server 2016的发布确保开发团队可以放弃其JSON解决方法,因为SQL Server 2016支持JSON。

将JSON数据解析到数据仓库中 (Parsing JSON Data into Data Warehouse)

Similarly to XML support in SQL Server, SQL Server supports of JSON can be classified into two ways:

与SQL Server中的XML支持类似,JSONSQL Server支持可以分为两种方式:

  1. Converting Relational dataset into JSON format

    将关系数据集转换为JSON格式

  2. Converting JSON dataset into relational format

    将JSON数据集转换为关系格式

However, for the purposes of this discussion we are focusing primarily on the second part – which is converting a JSON formatted data (retrieved from OLTP sources) into a relational format of rows and columns. To illustrate our discussion points we once again make use of the fictitious fruit sales dataset. This time around the fictitious dataset has been converted into a JSON format as shown in Figure 4.

但是,出于讨论的目的,我们主要关注第二部分-将JSON格式的数据(从OLTP来源检索)转换为行和列的关系格式。 为了说明我们的讨论要点,我们再次使用虚拟水果销售数据集。 这次围绕虚拟数据集的操作已转换为JSON格式, 如图4所示。


ISJSON函数 (ISJSON function)

As part of supporting JSON formatted data in other relational databases such as MySQL and PostgreSQL 9.2, there is a separate JSON data type that has been introduced by these vendors. Amongst other things, JSON data type conducts validation checks to ensure that values being stored are indeed of valid JSON format.

作为在其他关系数据库(例如MySQL和PostgreSQL 9.2)中支持JSON格式的数据的一部分,这些供应商已经引入了一种单独的JSON数据类型。 除其他外,JSON数据类型执行验证检查以确保所存储的值确实为有效JSON格式。

Unfortunately, SQL Server 2016 (and ORACLE 12c) do not have a special data type for storing JSON data instead a variable character (varchar/nvarchar) data type is used. Therefore, a recommended practice to dealing with JSON data in SQL Server 2016 is to firstly ensure that you are indeed dealing with a valid JSON data. The simplest way to do so is to use the ISJSON function. This is a built-in T-SQL function that returns 1 for a valid JSON dataset and 0 for invalids.

不幸的是,SQL Server 2016(和ORACLE 12c )没有用于存储JSON数据的特殊数据类型,而是使用了可变字符(varchar / nvarchar)数据类型。 因此,在SQL Server 2016中处理JSON数据的建议做法是,首先确保您确实在处理有效的JSON数据。 最简单的方法是使用ISJSON函数。 这是一个内置的T-SQL函数,对于有效的JSON数据集返回1,对于无效的返回0。

Figure 5 shows us the implementation of ISJSON function whereby we validate our fictitious sample dataset.

图5向我们展示了ISJSON函数的实现,从而验证了虚拟样本数据集。



OPENJSON函数 (OPENJSON function)

Now that we have confirmed that we are working with a valid JSON dataset, the next step is to convert the data into a table format. Again, we have a built-in T-SQL function to do this in a form of OPENJSON. OPENJSON works similar to OPENXML in that it takes in an object and convert its data into rows and columns.

现在,我们已经确认我们正在使用有效的JSON数据集,下一步是将数据转换为表格格式。 同样,我们有一个内置的T-SQL函数以OPENJSON的形式执行此操作。 OPENJSON的工作方式与OPENXML类似,因为它接受一个对象并将其数据转换为行和列。

Figure 6 shows a complete T-SQL script for converting JSON object into rows and columns.

图6显示了一个完整的T-SQL脚本,用于将JSON对象转换为行和列。


Once we execute the above script, we get relational output shown in Figure 7.

一旦执行了上面的脚本,我们将得到图7所示的关系输出。


Now that we have our relational dataset, we can process this data into data warehouse.

现在我们有了关系数据集,我们可以将此数据处理到数据仓库中。

JSON_VALUE函数 (JSON_VALUE function)

Prior to concluding our discussion of JSON in SQL Server 2016, it is worth mentioning that in addition to OPENJSON, you have other functions such as JSON_VALUE that could be used to query JSON data. However this function returns a scalar value which means that unlike the multiple rows and columns returned using OPENJSON, JSON_VALUE returns a single value as shown in Figure 8.

在结束我们对SQL Server 2016中JSON的讨论之前,值得一提的是,除了OPENJSON之外,您还有其他功能(例如JSON_VALUE)可用于查询JSON数据。 但是,此函数返回标量值,这意味着与使用OPENJSON返回的多个行和列不同,JSON_VALUE返回单个值,如图8所示。


If you the JSON object that you are querying doesn’t have multiple elements, than you don’t have to specify the row index (i.e. [0]) as shown in Figure 9.

如果您要查询的JSON对象没有多个元素,则不必指定行索引(即[0]), 如图9所示。


结论 (Conclusion)

The long wait is finally over and with the release of SQL Server 2016, JSON is now supported. Similarly to XML, T-SQL support the conversion of JSON object to relational format as well the conversion of relational tables to a JSON object. This support is implemented via built-in T-SQL functions such as OPENJSON and JSON_VALUE. In spite of all the excitement with the support of JSON is SQL Server 2016, we still don’t have a JSON data type. The ISJSON function can then be used to validate JSON text.

漫长的等待终于结束了,随着SQL Server 2016的发布,现在支持JSON。 与XML类似,T-SQL支持将JSON对象转换为关系格式,以及将关系表转换为JSON对象。 通过内置的T-SQL函数(例如OPENJSON和JSON_VALUE)实现此支持。 尽管对JSON的支持令人兴奋,但SQL Server 2016仍然没有JSON数据类型。 然后可以使用ISJSON函数来验证JSON文本。

资料下载 (Downloads)

  • XML Sample DataXML示例数据
  • JSON Sample DataJSON样本数据

参考 (Reference)

  • XML Data in SQL Server SQL Server中的XML数据
  • JSON Data in SQL Server 2016 SQL Server 2016中的JSON数据
  • XML nodes() Method XML nodes()方法

翻译自: https://www.sqlshack.com/warehousing-json-formatted-data-in-sql-server-2016/

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在SQL Server 2016中存储JSON格式的数据 的相关文章

  • 如何使用 WHERE x IN 子句编写PreparedStatement 的SQL?

    我有一个如下所示的查询 SELECT last name first name middle initial FROM names WHERE last name IN smith jones brown 我需要能够对 IN 子句中的列表进
  • 在 BEFORE INSERT 触发器中使用 IF EXISTS (SELECT ...) (Oracle)

    我的代码不起作用 Oracle 告诉我创建触发器时出现构建错误 显然我无法获得有关构建错误的更准确信息 我以前确实没有做过很多SQL 所以我对语法不太熟悉 我有一种预感 Oracle 不喜欢我的 IF EXISTS SELECT THEN
  • 获取下一个ID而不插入行

    在 SQL SQL Server 中是否可以在插入行之前从表中的标识列检索下一个 ID 整数 而无需实际插入行 如果删除了最近的行 则这不一定是最高 ID 加 1 我问这个问题是因为我们偶尔需要用新行更新实时数据库 行的 ID 在我们的代码
  • Django 模型 - 外键作为主键

    我有以下2张表 在 models py 中 class Foo models Model uuid models CharField UUID primary key True default uuid4 and class FooExt
  • 优化mysql中日期类型字段的查询

    我目前准备了以下查询 select sum amount as total from incomes where YEAR date 2019 and MONTH date 07 and incomes deleted at is null
  • jquery ajax“发布”调用

    我是 jQuery 和 Ajax 的新手 并且在 发布 方面遇到问题 我正在使用 jQuery Ajax post 调用将数据保存到数据库 当我尝试保存数据时 它将 null 传递给我的 C 方法 jQuery 看起来像这样 functio
  • 扁平化/反规范化 SQL 查找表的最佳方法?

    我有很多这样的表 Lookup HealthCheckupRisks ID Name 1 Anemia 2 Anorexic 3 Bulemic 4 Depression 122 Syphilis PatientRisksOnCheckup
  • MYSQL 查询 WHERE IN 与 OR

    我开发了一个使用 OR 查询的系统 SELECT FROM tableA JOIN tableB ON idA idB WHERE idA 1 OR idA 2 OR idA 3 OR idA 4 OR idA 5 OR idA 100 与
  • jq - 按值排序

    我有以下结构 ID XX guid 1 ID YY guid 2 我尝试过运行 jq sort by guid conn json 但是我收到一个错误 Cannot index string with string guid 请您告诉我如何
  • 尝试通过比较不同的表从 SQL 查询输出正确的值

    我对 SQL 非常陌生 需要有关如何使用正确的查询完成此任务的帮助 我有 2 张桌子需要使用 表 TB1 有 id Name 1 bob 2 blow 3 joe 表 TB2 有 compid property 1 bob 2 blow 我
  • 使用全文搜索查找精确匹配

    使用 Sql Server 2008 如何使用全文搜索来实际找到精确的字符串匹配 我对此感到非常困难 而且我在网上找不到令人满意的解决方案 例如 如果我正在搜索字符串 Bojan Skrchevski 我希望第一个结果正是如此 到目前为止
  • Foreach Ajax Json - Jquery

    谁能帮我 我有一个数组 stars Chris Pine Keira Knightley Kevin Costner 我想做的是 foreach star 我想将输入附加到 div 和 foreach star 它们在输入中具有值 这就是我
  • 多个连接到同一个表

    我有这组表格和数据 CREATE TABLE item id INT PRIMARY KEY name VARCHAR CREATE TABLE property id INT PRIMARY KEY name VARCHAR CREATE
  • 让 WSO2 IOT 服务器作为 Windows 服务运行

    我在Server 2012R2上安装了WSO2 IOT Server 我安装了 YAJSW 包装器并按照 WSO2 说明进行配置 当我启动 runConsole bat 来配置 Windows 服务时 出现以下错误 我设置了 JAVA HO
  • 使用把手显示来自 parse.com 的 json 响应

    我想将 json 响应传递给车把 我已经查看了解析文档和 stackoverflow 问题 但我似乎无法弄清楚这一点 这是回应 results address 755 W Yale createdAt 2013 02 09T01 12 15
  • T-SQL 中是否有 LIKE 语句的替代方案?

    我有一个场景我需要执行以下操作 SELECT FROM dbo MyTable WHERE Url LIKE
  • 如何使用内联 SQL 参数化 IN 语句的集合? [复制]

    这个问题在这里已经有答案了 可能的重复 参数化 SQL IN 子句 https stackoverflow com questions 337704 parameterizing a sql in clause 你好 我有一个查询 如下所示
  • Mysql获取特定表的最后一个id

    我必须从特定的插入表中获取最后的插入 ID 可以说我有这个代码 INSERT INTO blahblah test1 test 2 VALUES test1 test2 INSERT INTO blahblah2 test1 test 2
  • Spark DataFrame 序列化为无效 json

    TL DR 当我倾倒 Spark 时DataFrame作为 json 我总是得到类似的结果 key1 v11 key2 v21 key1 v12 key2 v22 key1 v13 key2 v23 这是无效的 json 我可以手动编辑转储
  • 检查 JSON 模式中不同的嵌套属性

    有没有办法在嵌套 JSON 模式中实现相当于 CHECK 约束的 Postgres 假设我们有具有两个属性的数据 每个属性都有嵌套属性 JSON Schema 如何使第一个对象所需的内容依赖于第二个对象 我的真实案例场景是为 GeoJSON

随机推荐