Dati semistrutturati

Introduzione a Redshift

Jason Myers

Principal Architect

Dati semistrutturati

  • Salvati nel tipo SUPER di Redshift
  • Funzioni SQL dedicate per formati diversi
  • JSON è un tipo di dati semistrutturati
Introduzione a Redshift

Funzioni di validazione JSON

  • IS_VALID_JSON verifica che un intero oggetto JSON sia valido
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 verifica che un array JSON sia valido.
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
Introduzione a Redshift

Estrazione da un oggetto JSON

  • JSON_EXTRACT_PATH_TEXT restituisce il valore a un percorso JSON
  • Accetta una stringa/colonna JSON e una o più chiavi del percorso
SELECT JSON_EXTRACT_PATH_TEXT('{"one":1, "two":2}', 'one');
JSON_EXTRACT_PATH_TEXT
======================
1
Introduzione a Redshift

Tentativo di analizzare JSON non valido

-- Trying to extract a path from poorly formed JSON
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]
  =========================================
Introduzione a Redshift

Estrazione da percorsi JSON annidati

  • Passa più chiavi come argomenti
SELECT JSON_EXTRACT_PATH_TEXT('
         {
           "one_object":{
              "nested_three": 3, 
              "nested_four":4
           }, 
           "two":2
         }', 
         'one_object', 'nested_three');
JSON_EXTRACT_PATH_TEXT
======================
3
Introduzione a Redshift

Estrazione da percorsi JSON annidati inesistenti

  • Restituisce NULL
    SELECT JSON_EXTRACT_PATH_TEXT('
           {
             "one_object":{
                "nested_three": 3, 
                "nested_four":4
             }, 
             "two":2
           }', 
           'two', 'nested_five');
    
JSON_EXTRACT_PATH_TEXT
======================
NULL
Introduzione a Redshift

Estrazione da un array JSON

  • JSON_EXTRACT_ARRAY_ELEMENT_TEXT restituisce il valore all'indice dell'array
  • Accetta una stringa/colonna JSON e un intero come indice a partire da zero.
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
Introduzione a Redshift

Estrazione da un array JSON annidato

SELECT JSON_EXTRACT_ARRAY_ELEMENT_TEXT(
           JSON_EXTRACT_PATH_TEXT('
               {
                 "one":1, 
                 "nested_two":[3,4,5]
               }', 
               -- Extracting the value at nested_two with JSON_EXTRACT_PATH_TEXT
               'nested_two'
            ),
            -- Extracting item at position one of the array with JSON_EXTRACT_ARRAY_ELEMENT_TEXT
            1
       );
JSON_EXTRACT_ARRAY_ELEMENT_TEXT
===============================
4
Introduzione a Redshift

Scorciatoia per estrarre da un array JSON annidato

  • L'indice dell'array deve essere una stringa
-- Passing two keys ('nested_two', '1') to select
-- the second element of the nested_two keys array value
SELECT JSON_EXTRACT_PATH_TEXT('
          {
            "one":1, 
            "nested_two":[3,4,5]
          }', 
          'nested_two', '1'
       );
JSON_EXTRACT_PATH_TEXT
======================
4
Introduzione a Redshift

Cast nelle CTE

WITH location_details AS (
    SELECT '{
        "location": "Salmon Challis National Forest",
      }'::SUPER::VARCHAR AS data
) 
  • Accessibile come location_details.data
Introduzione a Redshift

Passons à la pratique !

Introduzione a Redshift

Preparing Video For Download...