Created
July 31, 2025 09:58
-
-
Save chuongmep/0f5ca89810a641d8ba88f585bda47e38 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from fastapi import FastAPI, HTTPException | |
| import duckdb | |
| import pandas as pd | |
| import os | |
| app = FastAPI() | |
| # Kết nối đến DuckDB file (hoặc dùng ':memory:') | |
| conn = duckdb.connect("my_database.duckdb") | |
| @app.get("/") | |
| def read_root(): | |
| return {"message": "DuckDB API đang chạy"} | |
| @app.post("/insert/") | |
| def insert_data(name: str, age: int): | |
| try: | |
| conn.execute("CREATE TABLE IF NOT EXISTS users (name TEXT, age INTEGER)") | |
| conn.execute("INSERT INTO users VALUES (?, ?)", (name, age)) | |
| return {"status": "OK"} | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) from e | |
| @app.get("/save_parquet/") | |
| def save_parquet(): | |
| try: | |
| df = conn.execute("SELECT * FROM users").fetchdf() | |
| # create data directory if it doesn't exist | |
| if not os.path.exists("./data"): | |
| os.makedirs("./data") | |
| df.to_parquet("./data/users.parquet") | |
| return {"status": "Parquet file saved"} | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) from e | |
| @app.get("/load_parquet/") | |
| def load_parquet(): | |
| try: | |
| df = pd.read_parquet("./data/users.parquet") | |
| # get and return data | |
| return df.to_dict(orient="records") | |
| except FileNotFoundError as e: | |
| raise HTTPException(status_code=404, detail="Parquet file not found") from e | |
| @app.get("/users/") | |
| def get_users(): | |
| try: | |
| df = conn.execute("SELECT * FROM users").fetchdf() | |
| return df.to_dict(orient="records") | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) from e | |
| @app.get("/user/{name}") | |
| def get_user(name: str): | |
| try: | |
| df = conn.execute("SELECT * FROM users WHERE name = ?", (name,)).fetchdf() | |
| if df.empty: | |
| raise HTTPException(status_code=404, detail="User not found") | |
| return df.to_dict(orient="records")[0] | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) from e | |
| @app.delete("/user/{name}") | |
| def delete_user(name: str): | |
| try: | |
| result = conn.execute("DELETE FROM users WHERE name = ?", (name,)).rowcount | |
| if result == 0: | |
| raise HTTPException(status_code=404, detail="User not found") | |
| return {"status": "User deleted"} | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) from e | |
| @app.put("/user/{name}") | |
| def update_user(name: str, age: int): | |
| try: | |
| result = conn.execute("UPDATE users SET age = ? WHERE name = ?", (age, name)).rowcount | |
| if result == 0: | |
| raise HTTPException(status_code=404, detail="User not found") | |
| return {"status": "User updated"} | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) from e | |
| def main(): | |
| import uvicorn | |
| uvicorn.run(app, host="127.0.0.1", port=8000) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment