Archivi NoSQL non tabellari

Introduzione ai database NoSQL

Jake Roach

Data Engineer

Database di documenti

Definizione: Uno strumento di archiviazione NoSQL che salva dati in un formato flessibile e semi-strutturato, composto da coppie chiave-valore, chiave-array e chiave-oggetto (simile a JSON).

$$

$$

Logo JSON di Postgres.

{
    "title": "Python for Data Analysis",
    "price": 53.99,
    "topics": [
        "Data Science",
        "Data Analytics",
        ...
    ],
    "author": {
        "first": "William"
        ...
    }
}
Introduzione ai database NoSQL

Interrogare dati JSON con Postgres JSON

Dati JSON evidenziati archiviati in una tabella Postgres.

SELECT
    books -> 'title' AS title,
    books -> 'price' AS price
FROM data_science_resources
WHERE 
    books -> 'author' ->> 'last' = 'Viafore';

Con il seguente risultato:

Risultato della query JSON in Postgres.

Introduzione ai database NoSQL

Connessione a un database Postgres

import sqlalchemy

# Create a connection string, and an engine
connection_string = "postgresql+psycopg2://<user>:<password>@<host>:<port>/<database>"
db_engine = sqlalchemy.create_engine(connection_string)

Per creare una connessione a un database Postgres:

  • Crea una connection string
  • Crea un engine con sqlalchemy.create_engine
  • La variabile db_engine sarà già pronta prima dell'esercizio
Introduzione ai database NoSQL

Scrivere ed eseguire query JSON in Postgres

import pandas as pd

# Build the query
query = """
    SELECT
        books -> 'title' AS title,
        books -> 'price' AS price
    FROM data_science_resources;
"""

# Execute the query
result = pd.read_sql(query, db_engine)
print(result)

Per scrivere ed eseguire una query:

  • Crea la query in una stringa
  • Passa query e db_engine alla funzione pd.read_sql()
  • Stampa il DataFrame risultante
Introduzione ai database NoSQL

Altri archivi NoSQL non tabellari

Chiave-valore
{
    "name": "Jane Doe",
    "age": 25,
    "email": "[email protected]"
}

$$

Logo di Redis.

Grafo

Immagine di un database a grafo.

Introduzione ai database NoSQL

Esercitiamoci!

Introduzione ai database NoSQL

Preparing Video For Download...