Introduzione al Data Engineering
Vincent Vankrunkelsven
Data Engineer, DataCamp
| customer_id | state | created_at | |
|---|---|---|---|
| 1 | [email protected] | New York | 2019-01-01 07:00:00 |
| customer_id | username | domain | |
|---|---|---|---|
| 1 | [email protected] | jane.doe | theweb.com |
customer_df # Pandas DataFrame con i dati dei clienti # Dividi la colonna email in 2 colonne sul simbolo '@' split_email = customer_df.email.str.split("@", expand=True)# A questo punto, split_email avrà 2 colonne: # la prima con tutto prima di @ e la seconda con # tutto dopo @ # Crea 2 nuove colonne usando il DataFrame risultante. customer_df = customer_df.assign( username=split_email[0], domain=split_email[1], )
Estrai i dati in PySpark
import pyspark.sql spark = pyspark.sql.SparkSession.builder.getOrCreate()spark.read.jdbc("jdbc:postgresql://localhost:5432/pagila","customer",properties={"user":"repl","password":"password"})
Una nuova tabella ratings
| customer_id | film_id | rating |
|---|---|---|
| 1 | 2 | 1 |
| 2 | 1 | 5 |
| 2 | 2 | 3 |
| ... | ... | ... |
La tabella customer
| customer_id | first_name | last_name | ... |
|---|---|---|---|
| 1 | Jane | Doe | ... |
| 2 | Joe | Doe | ... |
| ... | ... | ... | ... |
customer_id si sovrappone con la tabella ratings
customer_df # PySpark DataFrame con i dati dei clienti ratings_df # PySpark DataFrame con i dati dei rating# Raggruppa i rating ratings_per_customer = ratings_df.groupBy("customer_id").mean("rating")# Fai il join su customer ID customer_df.join( ratings_per_customer, customer_df.customer_id==ratings_per_customer.customer_id )
Introduzione al Data Engineering