-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic example using chdb to serve GET/POST API requests, compatible with ClickHouse Play client queries. No action needed as this is not part of the compiled code.
- Loading branch information
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from flask import Flask, request | ||
import chdb | ||
import os | ||
|
||
# chdb API server example with GET/POST support, compatible with play app | ||
# for a full server example see https://github.com/metrico/chdb-server | ||
|
||
app = Flask(__name__, static_folder="", static_url_path="") | ||
|
||
@app.route('/', methods=["GET"]) | ||
def clickhouse(): | ||
query = request.args.get('query', default="", type=str) | ||
format = request.args.get('default_format', default="JSONCompact", type=str) | ||
if not query: | ||
return "Query not found", 400 | ||
|
||
res = chdb.query(query, format) | ||
return res.get_memview().tobytes() | ||
|
||
@app.route('/', methods=["POST"]) | ||
def play(): | ||
query = request.data | ||
format = request.args.get('default_format', default="JSONCompact", type=str) | ||
if not query: | ||
return "Query not found", 400 | ||
|
||
res = chdb.query(query, format) | ||
return res.get_memview().tobytes() | ||
|
||
@app.errorhandler(404) | ||
def handle_404(e): | ||
return "Not found", 404 | ||
|
||
host = os.getenv('HOST', '0.0.0.0') | ||
port = os.getenv('PORT', 8123) | ||
app.run(host=host, port=port) |