Comprender datos JSON en Postgres

Introducción a NoSQL

Jake Roach

Data Engineer

JSON y JSONB en Postgres

{
    'guardian': 'mother',
    'status': 'A',
    'educations': [4, 4],
    'jobs': {
        'P1': 'teacher',
        'P2': 'at_home'
    }
}
CREATE TABLE students
    <column-name> JSON,
    <column-bame> JSONB
;

JSON

  • Almacena datos en formato JSON
  • Pares clave-valor, arrays, objetos anidados

$$

JSONB

  • Los datos se almacenan en formato binario
  • Similar a BSON en MongoDB
  • Almacenamiento y consulta más eficientes
  • Permite indexación adicional
Introducción a NoSQL

¿Por qué datos semiestructurados en Postgres?

Una tabla de Postgres con una columna de tipo JSON.

Introducción a NoSQL

Consultar datos JSON con Postgres

SELECT
    address,
    famsize,
    ...
FROM students
[WHERE | GROUP BY | ORDER BY];

$$

  • row_to_json, json_to_record
  • Operadores ->, ->>, #>, #>>
  • json_extract_path, json_extract_path_text

También podrás:

  • Insertar registros en formato JSON en una tabla de Postgres
  • Tabular a JSON y JSON a tabular
  • Extraer registros individuales de objetos JSON
Introducción a NoSQL

Ejecutar consultas con sqlalchemy y pandas

import sqlalchemy
import pandas as pd
# Create a connection
db_engine = sqlalchemy.create_engine(
    "postgresql+psycopg2://<user>:<password>@<host>:5432/<database>"
)
# Write a query
query = "SELECT * FROM table_name;"
# Execute the query, show results
results = pd.read_sql(query, db_engine)
Introducción a NoSQL

¡Vamos a practicar!

Introducción a NoSQL

Preparing Video For Download...