Stocker des données JSON dans Postgres

Introduction à NoSQL

Jake Roach

Data Engineer

INSÉRER et COPIER des enregistrements JSON dans Postgres

INSERT INTO students (school, age, address, parent_meta) VALUES (
    'GP',
    18,
    'U',
    '{\"guardian\": \"mother\", ...  \"P2\": \"at_home\"}}'
);

Remplir une table avec le contenu d'un fichier via COPY ... FROM

COPY students FROM 'students.csv' DELIMITER ',' CSV, HEADER;
Introduction à NoSQL

Convertir des données tabulaires en JSON

Un tableau de données tabulaires et, à côté, une table affichant le premier tableau au format JSON après l'utilisation de la fonction row_to_json.

Fonction row_to_json

  • Convertit une ligne en JSON
  • À utiliser avec la fonction row(), en passant les noms de colonnes
SELECT
    row_to_json(row(
        school,
        age,
        address
    ))
FROM students;
Introduction à NoSQL

Extraire des clés depuis du JSON

Deux tables : l'une avec une seule colonne de type JSON Postgres, l'autre contenant la sortie de la fonction json_object_keys.

Fonction json_object_keys

  • Extrait les clés d'une colonne de type JSON
SELECT
    json_object_keys(parent_meta)
FROM students;
  • Associer à DISTINCT pour trouver toutes les clés uniques
SELECT
    DISTINCT json_object_keys(parent_meta)
FROM students;
Introduction à NoSQL

Révision

SELECT
    row_to_json(row(
        <column-1>,
        <column-2>,
        ...
    ))
FROM <table-name>;
SELECT
    DISTINCT json_object_keys(parent_meta)
FROM <table-name>;
Introduction à NoSQL

Passons à la pratique !

Introduction à NoSQL

Preparing Video For Download...