載入到 Postgres
在這個練習中,你將把一些資料寫入 PostgreSQL 數據倉儲。當你完成一連串轉換並得到結果,且想在應用程式中使用時,這會很有用。
例如,某個轉換的結果可能新增了一欄電影推薦,而你想在你的線上商店中使用這些推薦。
在你的工作區中有一個名為 film_pdf 的 pandas DataFrame。
提醒你,以下是 sqlalchemy 連線 URI 的結構:
postgresql://[user[:password]@][host][:port][/database]
本練習屬於課程
Data Engineering 入門
練習說明
- 完成連線 URI 以建立資料庫引擎。使用者與密碼分別是
repl與password。主機是localhost,連接埠是5432。這次的資料庫是dwh。 - 完成呼叫以便在資料庫中使用
"store"綱要(schema)。若表格已存在,請完全以新結果取代。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Finish the connection URI
connection_uri = "____://____:____@____:____/____"
db_engine_dwh = sqlalchemy.create_engine(connection_uri)
# Transformation step, join with recommendations data
film_pdf_joined = film_pdf.join(recommendations)
# Finish the .to_sql() call to write to store.film
film_pdf_joined.to_sql("film", ____, schema="____", if_exists="____")
# Run the query to fetch the data
pd.read_sql("SELECT film_id, recommended_film_ids FROM store.film", db_engine_dwh)