Estrai

Introduzione al Data Engineering

Vincent Vankrunkelsven

Data Engineer, DataCamp

Estrarre dati: cosa significa?

Diagramma che rappresenta una semplice fase di estrazione

Introduzione al Data Engineering

Estrazione da file di testo

Non strutturato

  • Testo semplice
  • Es. capitolo di un libro

 

Call me Ishmael. Some years ago, never 
mind how long precisely, having little or
no money in my purse, and nothing particular
to interest me on shore, I thought ....

File flat

  • Riga = record
  • Colonna = attributo
  • Es. .tsv o .csv

 

Year,Make,Model,Price
1997,Ford,E350,3000.00
1999,Chevy,"Venture Extended Edition",4900.00
1999,Chevy,"Venture Extended Edition",5000.00
1996,Jeep,Grand Cherokee,4799.00
Introduzione al Data Engineering

JSON

  • JavaScript Object Notation
  • Semi-strutturato
  • Atomico
    • number
    • string
    • boolean
    • null
  • Composto
    • array
    • object
{
  "an_object": {
    "nested": [
      "one",
      "two",
      "three",
      {
        "key": "four"
      }
    ]
  }
}
import json

result = json.loads('{"key_1": "value_1", "key_2":"value_2"}') print(result["key_1"])
value_1
Introduzione al Data Engineering

Dati sul Web

Richieste

Diagramma che rappresenta il modello richiesta-risposta

Esempio

  1. Vai su Google
  2. Richiesta al server di Google
  3. Google risponde con la pagina web
Introduzione al Data Engineering

Dati sul Web tramite API

  • Invio dati in formato JSON
  • API: application programming interface
  • Esempi
    • GitHub API
{ "login": "datacamp", "type": "Organization", "created_at": "2013-05-14T13:01:20Z" }
  • Hackernews API
import requests

response = requests.get("https://hacker-news.firebaseio.com/v0/item/16222426.json") print(response.json())
{'by': 'neis', 'descendants': 0, 'id': 16222426, 'score': 17, 'time': 1516800333, 'title': .... }
Introduzione al Data Engineering

Dati nei database

Database applicativi

  • Transazioni
  • Inserimenti o modifiche
  • OLTP
  • Orientati alle righe

Database analitici

  • OLAP
  • Orientati alle colonne
Introduzione al Data Engineering

Estrazione dai database

Connection string/URI

postgresql://[user[:password]@][host][:port]

Uso in Python

import sqlalchemy
connection_uri = "postgresql://repl:password@localhost:5432/pagila" 
db_engine = sqlalchemy.create_engine(connection_uri)

import pandas as pd pd.read_sql("SELECT * FROM customer", db_engine)
Introduzione al Data Engineering

Esercitiamoci!

Introduzione al Data Engineering

Preparing Video For Download...