Înțelegerea datelor JSON în Postgres

Introducere în NoSQL

Jake Roach

Data Engineer

JSON și JSONB în Postgres

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

JSON

  • Stochează date în format JSON
  • Perechi cheie-valoare, tablouri, obiecte imbricate

$$

JSONB

  • Datele sunt stocate în format binar
  • Similar cu BSON în MongoDB
  • Stocare și recuperare mai eficiente
  • Permite indexare suplimentară
Introducere în NoSQL

De ce date semi-structurate în Postgres?

O tabelă Postgres cu o coloană de tip JSON.

Introducere în NoSQL

Interogarea datelor JSON în Postgres

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

$$

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

De asemenea, vom putea:

  • Insera înregistrări JSON într-o tabelă Postgres
  • Converti tabular în JSON și invers
  • Extrage înregistrări individuale din obiecte JSON
Introducere în NoSQL

Executarea interogărilor cu sqlalchemy și 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)
Introducere în NoSQL

Să exersăm!

Introducere în NoSQL

Preparing Video For Download...