0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

RedshiftでJSONを使いたいときはSUPER型を使う

Last updated at Posted at 2022-03-30

はじめに

どうやらRedshiftにはJson型がない様子。でもどうしてもJSONを読みたいんだ!!
というときはSUPER型を使うみたいです。

やってみた

SUPER型のあるテーブル作成

CREATE TABLE json_sample(
    id INT NOT NULL,
    json_field SUPER
);

INSERTするときはJSON_PARSE関数を忘れずに

重要!! 投入するときJSON_PARSE関数が必要です。当関数が無いと単純な文字列として認識されちゃいます。
この例でいうと4番目の行がNG例です。

INSERT INTO json_sample
values 
(1, JSON_PARSE('{"hoge":1,"piyo":2}')),
(2, JSON_PARSE('{"hoge":3,"piyo":4}')),
(3, JSON_PARSE('{"foo":"a","bar":"b"}')),
(4, '{"foo":"c","bar":"d"}')
;

結果

要素名で取得できるのが素敵

SELECT 
  id,
  json_field,
  json_field.hoge,
  json_field.piyo,
  json_field.foo,
  json_field.bar
FROM
json_sample;
id json_field hoge piyo foo bar
1 {"hoge":1, "piyo":2} 1 2 NULL NULL
2 {"hoge":3, "piyo":4} 3 4 NULL NULL
3 {"foo":"a", "bar":"b"} NULL NULL "a" "b"
4 "{\"foo\":\"c\",\"bar\":\"d\"}" NULL NULL NULL NULL

文献

RedshiftにJSONを格納してTableauで可視化してみた
https://dev.classmethod.jp/articles/visualize_json_from_redshift_with_tableau/

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?