Données semi-structurées

Introduction à Redshift

Jason Myers

Principal Architect

Données semi-structurées

  • Stockées dans le type SUPER de Redshift
  • Fonctions SQL dédiées selon le format
  • JSON est un type de données semi-structurées
Introduction à Redshift

Fonctions de validation JSON

  • IS_VALID_JSON : vérifie qu'un objet JSON complet est valide
SELECT IS_VALID_JSON('{"one":1, "two":2}');
IS_VALID_JSON
=============
true
SELECT IS_VALID_JSON('{"one":1, "two":2');
IS_VALID_JSON
=============
false
  • IS_VALID_JSON_ARRAY : vérifie qu'un tableau JSON est valide.
SELECT IS_VALID_JSON_ARRAY('{"one":1}')
IS_VALID_JSON_ARRAY
===================
false
SELECT IS_VALID_JSON_ARRAY('[1,2,3]')
IS_VALID_JSON_ARRAY
===================
true
Introduction à Redshift

Extraire d'un objet JSON

  • JSON_EXTRACT_PATH_TEXT retourne la valeur à un chemin JSON
  • Prend une chaîne ou un champ JSON, puis une ou plusieurs clés du chemin
SELECT JSON_EXTRACT_PATH_TEXT('{"one":1, "two":2}', 'one');
JSON_EXTRACT_PATH_TEXT
======================
1
Introduction à Redshift

Tentative d'analyse d'un JSON non valide

-- Tentative d'extraction d'un chemin à partir d'un JSON mal formé
SELECT JSON_EXTRACT_PATH_TEXT('{"one":1, "two":2', 'one');
JSON parsing error
DETAIL:  
  =========================================
  error:  JSON parsing error
  code:      8001
  context:   invalid json object 
  query:     [child_sequence:'one']
  location:  funcs_json.hpp:202
  process:   padbmaster [pid=1073807529]
  =========================================
Introduction à Redshift

Extraction à partir de chemins JSON imbriqués

  • Fournir plusieurs clés en arguments
SELECT JSON_EXTRACT_PATH_TEXT('
         {
           "one_object":{
              "nested_three": 3, 
              "nested_four":4
           }, 
           "two":2
         }', 
         'one_object', 'nested_three');
JSON_EXTRACT_PATH_TEXT
======================
3
Introduction à Redshift

Extraction de chemins JSON imbriqués inexistants

  • Retourne NULL
    SELECT JSON_EXTRACT_PATH_TEXT('
           {
             "one_object":{
                "nested_three": 3, 
                "nested_four":4
             }, 
             "two":2
           }', 
           'two', 'nested_five');
    
JSON_EXTRACT_PATH_TEXT
======================
NULL
Introduction à Redshift

Extraction d'un tableau JSON

  • JSON_EXTRACT_ARRAY_ELEMENT_TEXT retourne la valeur à l'indice du tableau
  • Prend une chaîne ou un champ JSON, puis un entier d'indice à partir de zéro.
SELECT JSON_EXTRACT_ARRAY_ELEMENT_TEXT('[1.1,400,13]', 2);
JSON_EXTRACT_ARRAY_ELEMENT_TEXT
===============================
13
SELECT JSON_EXTRACT_ARRAY_ELEMENT_TEXT('[1.1,400,13]', 3);
JSON_EXTRACT_ARRAY_ELEMENT_TEXT
===============================
NULL
Introduction à Redshift

Extraction d'un tableau JSON imbriqué

SELECT JSON_EXTRACT_ARRAY_ELEMENT_TEXT(
           JSON_EXTRACT_PATH_TEXT('
               {
                 "one":1, 
                 "nested_two":[3,4,5]
               }', 
               -- Extraction de la valeur de nested_two avec JSON_EXTRACT_PATH_TEXT
               'nested_two'
            ),
            -- Extraction de l'élément en position 1 du tableau avec JSON_EXTRACT_ARRAY_ELEMENT_TEXT
            1
       );
JSON_EXTRACT_ARRAY_ELEMENT_TEXT
===============================
4
Introduction à Redshift

Raccourci pour extraire d'un tableau JSON imbriqué

  • L'indice du tableau doit être une chaîne
-- Passer deux clés (« nested_two », « 1 ») pour sélectionner
-- le deuxième élément du tableau de la clé nested_two
SELECT JSON_EXTRACT_PATH_TEXT('
          {
            "one":1, 
            "nested_two":[3,4,5]
          }', 
          'nested_two', '1'
       );
JSON_EXTRACT_PATH_TEXT
======================
4
Introduction à Redshift

Convertir les types dans des CTE

WITH location_details AS (
    SELECT '{
        "location": "Salmon Challis National Forest",
      }'::SUPER::VARCHAR AS data
) 
  • Accessible en tant que location_details.data
Introduction à Redshift

Passons à la pratique !

Introduction à Redshift

Preparing Video For Download...