に最終更新
EXPLODE_JSON_OBJECT
説明
explode_json_object テーブル関数は JSON オブジェクトを複数の行に展開し、各行にキーと値のペアを含めます。
JSON オブジェクトをクエリで扱いやすい形式に変換する際によく使用します。この関数がサポートするのは、要素を含む JSON オブジェクトのみです。
LATERAL VIEW と組み合わせて使用してください。
構文
EXPLODE_JSON_OBJECT(<json>)
パラメータ
<json>JSON 型。内容は JSON オブジェクトである必要があります。
戻り値
<json>のすべての要素からなる、単一列で複数行の結果を返します。列の型はNullable<Struct<String, JSON>>です。<json>が NULL、または JSON オブジェクトではない場合(配列[]など)、0 行を返します。<json>が空のオブジェクト({}など)の場合、0 行を返します。
例
- データの準備
create table example(
k1 int
) properties(
"replication_num" = "1"
);
insert into example values(1); - 通常の引数
select * from example lateral view explode_json_object('{"k1": "v1", "k2": 123}') t2 as c;+------+------------------------------+
| k1 | c |
+------+------------------------------+
| 1 | {"col1":"k1", "col2":""v1""} |
| 1 | {"col1":"k2", "col2":"123"} |
+------+------------------------------+ - キーと値のペアを別々の列に展開
select * from example lateral view explode_json_object('{"k1": "v1", "k2": 123}') t2 as k, v;+------+------+------+
| k1 | k | v |
+------+------+------+
| 1 | k1 | "v1" |
| 1 | k2 | 123 |
+------+------+------+vの型は JSON です。 - 空のオブジェクト
select * from example lateral view explode_json_object('{}') t2 as c;Empty set (0.03 sec) - NULL 引数
select * from example lateral view explode_json_object(NULL) t2 as c;Empty set (0.03 sec) - オブジェクト以外の引数
select * from example lateral view explode_json_object('[]') t2 as c;Empty set (0.03 sec)