開始使用免費開始

從 Postgres 查詢 JSON 與 JSONB 資料

有了 Postgres 內建的 JSONJSONB 資料型別,在 Postgres 資料表中儲存並操作半結構化資料就很容易。在這個練習中,你會實際看到 Postgres 提供用來查詢 nested_reviews 資料表中 JSON 型別資料的工具。加油!

本練習屬於課程

NoSQL 入門

檢視課程

練習說明

  • 使用 sqlalchemy 以使用者 repl 連線到 disneyland 資料庫。
  • 使用先前建立的 db_engine,執行儲存在變數 query 中的查詢。
  • 輸出 results DataFrame 的 review 欄位,並觀察回傳的資料。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

import pandas as pd
import sqlalchemy

# Create a connection to the reviews database
db_engine = sqlalchemy.create_engine("postgresql+psycopg2://____:password@localhost:5432/____")

query = """SELECT * FROM nested_reviews;"""

# Execute the query, check out the results
results = pd.____(____, ____)

# Print the review column from the results DataFrame
____(results["review"])
編輯並執行程式碼