โหลดข้อมูลเข้า Postgres
ในแบบฝึกหัดนี้ จะได้เขียนข้อมูลบางส่วนลงใน PostgreSQL data warehouse ซึ่งมีประโยชน์มากเมื่อต้องการนำผลลัพธ์จากการแปลงข้อมูลไปใช้งานในแอปพลิเคชัน
ตัวอย่างเช่น ผลลัพธ์ของการแปลงข้อมูลอาจมีคอลัมน์ที่เพิ่มคำแนะนำภาพยนตร์ไว้ด้วย และต้องการนำข้อมูลนั้นไปแสดงในร้านค้าออนไลน์
ใน workspace มี DataFrame ของ pandas ชื่อ film_pdf พร้อมใช้งานแล้ว
สำหรับการอ้างอิง โครงสร้างของ connection URI สำหรับ sqlalchemy มีดังนี้:
postgresql://[user[:password]@][host][:port][/database]
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Data Engineering เบื้องต้น
คำแนะนำการฝึกหัด
- กรอก connection URI ให้ครบเพื่อสร้าง database engine โดย user และ password คือ
replและpasswordตามลำดับ host คือlocalhostและ port คือ5432ครั้งนี้ database คือdwh - กำหนดค่าให้ฟังก์ชันใช้ schema
"store"ในฐานข้อมูล และหากตารางมีอยู่แล้วให้แทนที่ทั้งหมด
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# 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)