KingbaseES Json 系列八:Json记录操作函数三

本文介绍了KingbaseES数据库用于处理JSON数据的Json_TABLE函数,包括其功能、用法和各种类型的列定义,以及示例展示如何将JSON数据转化为关系视图进行查询。此外,还探讨了错误处理和数据连接的不同策略。
摘要由CSDN通过智能技术生成

KingbaseES Json 系列八--Json记录操作函数三(JSON_TABLE)

JSON 数据类型是用来存储 JSON(JavaScript Object Notation)数据的。KingbaseES为存储JSON数据提供了两种类型:JSON和 JSONB。JSON 和 JSONB 几乎接受完全相同的值集合作为输入。

本文将主要介绍Kingbase数据库的Json记录操作函数第三部分。

准备数据:

CREATE TABLE "public"."jsontable" (
	"id" integer NULL,
	"jsondata" json NULL,
	"jsonvarchar" varchar NULL,
	"jsonarray" json NULL,
	"jsonrecord" json NULL,
	"jsonset" json NULL
);

INSERT INTO "public"."jsontable" ("id","jsondata","jsonvarchar","jsonarray","jsonrecord","jsonset") VALUES
	 (1,'{"f2":{"f3":1},"f4":{"f5":99,"f6":"foo"}}','{"f2": {"f3": 1}, "f4": {"f5": 99, "f6": "foo"}}','[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]','{"a":1,"b":"bcol","c":"cc"}','[{"a":1,"b":"bcol","c":"cc"},{"a":1,"b":"bcol","d":""}]'),
	 (2,'{"a":[1,2,3,4,5]}','{"a": [1, 2, 3, 4, 5]}','[1,2,3,4,5]','{"a":1,"b":"bcol","c":""}','[{"a":1,"b":"bcol","c":""},{"a":1,"b":"bcol","e":""}]'),
	 (3,'{"a":1, "b": ["2", "a b"],"c": {"d":4, "e": "ab c"}}','{"a": 1, "b": ["2", "a b"], "c": {"d": 4, "e": "ab c"}}','[{"f1":1,"f2":null},2,null,3]','{"a":1,"b":"bcol","d":"dd"}','[{"a":1,"b":"bcol","c":"cc_3_1"},{"a":1,"b":"bcol","c":"cc_3_2"}]');

CREATE TABLE jsonb_table_test (js jsonb);
INSERT INTO jsonb_table_test
VALUES (
  '[
    {"a":  1,  "b": [], "c": []},
    {"a":  2,  "b": [1, 2, 3], "c": [10, null, 20]}
   ]'
);

json函数列表

json函数简介

JSON_TABLE

功能:

JSON函数,查询JSON数据并将结果显示为关系视图,可以作为常规SQL表访问。只能在SELECT语句的FROM子句中使用json_table。

用法:
json_table (
  context_item,
  path_expression [ AS json_path_name ] [ PASSING { value AS varname } [, ...] ]
  COLUMNS ( json_table_column [, ...] )
  [
    PLAN ( json_table_plan ) |
    PLAN DEFAULT ( { INNER | OUTER } [ , { CROSS | UNION } ] |
                   { CROSS | UNION } [ , { INNER | OUTER } ] )
  ]
  [{ERROR | EMPTY} ON ERROR]
)

context_item:
  查询的输入数据。
path_expression [ AS json_path_name ] [ PASSING { value AS varname } [, ...] ]:
  定义查询的JSON路径表达式和一个可选的PASSING子句,它可以为path_expression提供数据值。
  输入数据评估的结果称为行模式。行模式用作构造视图中行值的来源。
COLUMNS( json_table_column [, ...] ):
  定义构造视图模式的COLUMNS子句。在此子句中,必须指定要使用SQL/JSON项填充的所有列。
  json_table_column说明请见【子句分项】。
[
  PLAN ( json_table_plan ) |
  PLAN DEFAULT ( { INNER | OUTER } [ , { CROSS | UNION } ] |
                 { CROSS | UNION } [ , { INNER | OUTER } ] )
]:
  定义如何将NESTD PATH子句返回的数据连接到构造的视图。json_table_plan说明请见【子句分项】。
[{ERROR | EMPTY} ON ERROR]:
  指定发生错误时函数返回的值。默认为EMPTY ON ERROR。

子句分项:
  json_table_column:定义构造视图模式的COLUMNS子句。子句包含以下五种类型:
    类型1:
      name type [ PATH json_path_specification ]
                [ { WITHOUT | WITH { CONDITIONAL | [UNCONDITIONAL] } } [ ARRAY ] WRAPPER ]
                (注:此处实际只能支持WITHOUT [ARRAY] WRAPPER)
                [ { KEEP | OMIT } QUOTES [ ON SCALAR STRING ] ]
                (注:此处实际不支持此子句)
                [ { ERROR | NULL | DEFAULT expression } ON EMPTY ]
                [ { ERROR | NULL | DEFAULT expression } ON ERROR ]
      说明:与JSON_QUERY函数相同的方式评估JSON数据,找到一个或多个指定的JSON值,并返回包含这些JSON值的字符串列。
    类型2:
      name type FORMAT json_representation
        [ PATH json_path_specification ]
        [ { WITHOUT | WITH { CONDITIONAL | [UNCONDITIONAL] } } [ ARRAY ] WRAPPER ]
        [ { KEEP | OMIT } QUOTES [ ON SCALAR STRING ] ]
        [ { ERROR | NULL | EMPTY { ARRAY | OBJECT } | DEFAULT expression } ON EMPTY ]
        [ { ERROR | NULL | EMPTY { ARRAY | OBJECT } | DEFAULT expression } ON ERROR ]
      说明:与JSON_VALUE函数相同的方式评估JSON数据,即它找到指定的标量JSON值,并将这些JSON值的列作为SQL值返回。
    类型3:
      name type EXISTS [ PATH json_path_specification ]
        [ { ERROR | TRUE | FALSE | UNKNOWN } ON ERROR ] |
          NESTED PATH json_path_specification [ AS path_name ] COLUMNS ( json_table_column [, ...] )
      说明:与JSON_EXISTS条件相同的方式评估JSON数据,即确定是否存在指定的JSON值。
            它返回‘true’或‘false’的VARCHAR2列,
            或值为1或0的NUMBER列。
    类型4:
      name FOR ORDINALITY
      说明:返回一列生成的数据类型为NUMBER的行号。每个表只能有一个序数列。行编号从1开始。
    类型5:
      NESTED PATH json_path_specification [ AS json_path_name ] COLUMNS ( json_table_column [, ...] )
      说明:将嵌套JSON对象或JSON数组中的JSON值与来自父对象或数据中的JSON值一起展平为单行中的各个列。
           可以递归地使用此子句将来自多层嵌套对象或数组的数据投影到单行中。
  PLAN ( json_table_plan ):定义如何将NESTD PATH子句返回的数据连接到构造的视图。
    完整子句格式:
    PLAN( json_path_name [ { OUTER | INNER } json_table_plan_primary ] |
          json_table_plan_primary { UNION json_table_plan_primary } [...] |
          json_table_plan_primary { CROSS json_table_plan_primary } [...]
        )
    说明:通过设置子句的INNER,OUTER,UNION和CROSS,定义子句中的数据如何连接到视图中。
         INNER JOIN,以便在连接NESTED PATH返回的数据后,如果父行没有任何子  行,则从输出中省略父行。
         LEFT OUTER JOIN,这样即使父行在连接NESTED PATH返回的数据后没有任何子行,也始终包含在输出中,
           如果缺少相应的值,则将NULL值插入到子列中。
         UNION,为每个兄弟列生成的每个值生成一行。其他兄弟的列设置为空。
         CROSS,为兄弟列中的每个值的组合生成一行。
示例:
-- COLUMNS类型1:name type [ PATH json_path_specification ]

select t.* from json_table('
  [
    {"a":  "1",  "b": [], "c": []},
    {"a":  2,  "b": [1, 2, 3], "c": [10, null, 20]},
    {"a":  3,  "b": [1, 2, 3]}
  ]'::jsonb ,
  '$[*]' columns(
   a int path 'lax $.a'
  )
) t;
--结果:
 a 
---
 1
 2
 3
(3 行记录)

-- COLUMNS类型2:name type FORMAT json_representation

select t.* from json_table('
  [
    {"a":  "1",  "b": [], "c": []},
    {"a":  2,  "b": [1, 2, 3], "c": [10, null, 20]},
    {"a":  3,  "b": [1, 2, 3]}
  ]'::jsonb ,
  '$[*]' columns(
   a text FORMAT json path  '$.a'
  )
) t;
--结果:
  a  
-----
 "1"
 2
 3
(3 行记录)

-- COLUMNS类型3:name type EXISTS [ PATH json_path_specification ]

select t.* from json_table('
  [
    {"a":  "1",  "b": [], "c": []},
    {"a":  2,  "b": [1, 2, 3], "c": [10, null, 20]},
    {"a":  3,  "b": [1, 2, 3]}
  ]'::jsonb ,
  '$[*]' columns(
   a text FORMAT json path  '$.c',
   name boolean EXISTS path  '$.c'
  )
) t;
--结果:
       a        | name 
----------------+------
 []             | t
 [10, null, 20] | t
                | f
(3 行记录)

-- COLUMNS类型4:name FOR ORDINALITY

select t.* from json_table('
  [
    {"a":  "1",  "b": [], "c": []},
    {"a":  2,  "b": [1, 2, 3], "c": [10, null, 20]},
    {"a":  3,  "b": [1, 2, 3]}
  ]'::jsonb ,
  '$[*]' columns(
   a text FORMAT json path  '$.a',
   id FOR ORDINALITY --编号
  )
) t;
--结果:
  a  | id 
-----+----
 "1" |  1
 2   |  2
 3   |  3
(3 行记录)

-- COLUMNS类型5:NESTED PATH json_path_specification 

select t.* from json_table('
  [
    {"a":  "1",  "b": [], "c": []},
    {"a":  2,  "b": [1, 2, 3], "c": [10, null, 20]},
    {"a":  3,  "b": [1, 2, 3]}
  ]'::jsonb ,
  '$[*]' columns(
   a text FORMAT json path  '$.a',
   nested path 'strict $.b[*]' as pb columns ( b int path '$' ),
   nested path 'strict $.c[*]' as pc columns ( c int path '$' )
  )
) t;
--结果:
  a  | b | c  
-----+---+----
 "1" |   |   
 2   | 1 |   
 2   | 2 |   
 2   | 3 |   
 2   |   | 10
 2   |   |   
 2   |   | 20
 3   | 1 |   
 3   | 2 |   
 3   | 3 |   
(10 行记录)

-- 设定COLUMNS类型转换失败时的默认处理方式(默认忽略错误)。

select t.* from json_table('
  [
    {"a":  "a",  "b": [], "c": []},
    {"a":  2,  "b": [1, 2, 3], "c": [10, null, 20]},
    {"a":  3,  "b": [1, 2, 3]}
  ]'::jsonb ,
  '$[*]' columns(
   a text path  '$.a',
   a_int int path  '$.a' ERROR ON ERROR -- NULL ON ERROR忽略错误或不设置
  )
) t;
--结果:
错误:  无效的类型 integer 输入语法: "a"

-- 表数据解析

SELECT
	jt.jsonset ,
	ROWNUM,
	t.*
FROM
	jsontable jt ,
	JSON_TABLE(
		jt.jsonset::jsonb ,
		'$[*]'
COLUMNS(
			id FOR ORDINALITY,
			a int PATH '$.a',
			b TEXT PATH '$.b',
			c TEXT PATH '$.c'
		)
	)t;
--结果:
                              jsonset                              | rownum | id | a |  b   |   c    
-------------------------------------------------------------------+--------+----+---+------+--------
 [{"a":1,"b":"bcol","c":"cc"},{"a":1,"b":"bcol","d":""}]           |      1 |  1 | 1 | bcol | cc
 [{"a":1,"b":"bcol","c":"cc"},{"a":1,"b":"bcol","d":""}]           |      2 |  2 | 1 | bcol | 
 [{"a":1,"b":"bcol","c":""},{"a":1,"b":"bcol","e":""}]             |      3 |  1 | 1 | bcol | 
 [{"a":1,"b":"bcol","c":""},{"a":1,"b":"bcol","e":""}]             |      4 |  2 | 1 | bcol | 
 [{"a":1,"b":"bcol","c":"cc_3_1"},{"a":1,"b":"bcol","c":"cc_3_2"}] |      5 |  1 | 1 | bcol | cc_3_1
 [{"a":1,"b":"bcol","c":"cc_3_1"},{"a":1,"b":"bcol","c":"cc_3_2"}] |      6 |  2 | 1 | bcol | cc_3_2
(6 行记录)

-- PLAN数据连接 OUTER与UNION组合

select
    jt.*
  from
    jsonb_table_test jtt,
    json_table (
      jtt.js,'strict $[*]' as p
      columns (
        n for ordinality,
        a int path 'lax $.a' default -1 on empty,
        nested path 'strict $.b[*]' as pb columns ( b int path '$' ),
        nested path 'strict $.c[*]' as pc columns ( c int path '$' )
      )
      plan (p outer (pb union pc))
  ) jt;
--结果:
 n | a | b | c  
---+---+---+----
 1 | 1 |   |   
 2 | 2 | 1 |   
 2 | 2 | 2 |   
 2 | 2 | 3 |   
 2 | 2 |   | 10
 2 | 2 |   |   
 2 | 2 |   | 20
(7 行记录)

-- PLAN数据连接 OUTER与CROSS组合

select
    jt.*
  from
    jsonb_table_test jtt,
    json_table (
      jtt.js,'strict $[*]' as p
      columns (
        n for ordinality,
        a int path 'lax $.a' default -1 on empty,
        nested path 'strict $.b[*]' as pb columns ( b int path '$' ),
        nested path 'strict $.c[*]' as pc columns ( c int path '$' )
      )
      plan (p outer (pb cross pc))
  ) jt;
--结果:
 n | a | b | c  
---+---+---+----
 1 | 1 |   |   
 2 | 2 | 1 | 10
 2 | 2 | 1 |   
 2 | 2 | 1 | 20
 2 | 2 | 2 | 10
 2 | 2 | 2 |   
 2 | 2 | 2 | 20
 2 | 2 | 3 | 10
 2 | 2 | 3 |   
 2 | 2 | 3 | 20
(10 行记录)


-- PLAN数据连接 INNER与UNION组合

select
    jt.*
  from
    jsonb_table_test jtt,
    json_table (
      jtt.js,'strict $[*]' as p
      columns (
        n for ordinality,
        a int path 'lax $.a' default -1 on empty,
        nested path 'strict $.b[*]' as pb columns ( b int path '$' ),
        nested path 'strict $.c[*]' as pc columns ( c int path '$' )
      )
      plan (p inner (pb union pc))
  ) jt;
--结果:
 n | a | b | c  
---+---+---+----
 2 | 2 | 1 |   
 2 | 2 | 2 |   
 2 | 2 | 3 |   
 2 | 2 |   | 10
 2 | 2 |   |   
 2 | 2 |   | 20
(6 行记录)

-- PLAN数据连接 INNER与CROSS组合

select
    jt.*
  from
    jsonb_table_test jtt,
    json_table (
      jtt.js,'strict $[*]' as p
      columns (
        n for ordinality,
        a int path 'lax $.a' default -1 on empty,
        nested path 'strict $.b[*]' as pb columns ( b int path '$' ),
        nested path 'strict $.c[*]' as pc columns ( c int path '$' )
      )
      plan (p inner (pb cross pc))
  ) jt;
--结果:
 n | a | b | c  
---+---+---+----
 2 | 2 | 1 | 10
 2 | 2 | 1 |   
 2 | 2 | 1 | 20
 2 | 2 | 2 | 10
 2 | 2 | 2 |   
 2 | 2 | 2 | 20
 2 | 2 | 3 | 10
 2 | 2 | 3 |   
 2 | 2 | 3 | 20
(9 行记录)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值