從 Postgres 查詢 JSON 與 JSONB 資料
有了 Postgres 內建的 JSON 與 JSONB 資料型別,在 Postgres 資料表中儲存並操作半結構化資料就很容易。在這個練習中,你會實際看到 Postgres 提供用來查詢 nested_reviews 資料表中 JSON 型別資料的工具。加油!
本練習屬於課程
NoSQL 入門
練習說明
- 使用
sqlalchemy以使用者repl連線到disneyland資料庫。 - 使用先前建立的
db_engine,執行儲存在變數query中的查詢。 - 輸出
resultsDataFrame 的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"])