-
Notifications
You must be signed in to change notification settings - Fork 9
Dataframes: Demonstrate pandas.read_sql()
with both urllib3 vs. psycopg3
#651
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
""" | ||
About | ||
===== | ||
|
||
Evaluate reading data from CrateDB into pandas dataframes. | ||
|
||
Example program to demonstrate reading data in batches from CrateDB into | ||
pandas, using SQLAlchemy, supporting urllib3 vs. psycopg3. | ||
|
||
|
||
Setup | ||
===== | ||
:: | ||
|
||
pip install --upgrade click pandas 'sqlalchemy-cratedb[all]' | ||
|
||
|
||
Synopsis | ||
======== | ||
:: | ||
|
||
# Run CrateDB. | ||
docker run --rm -it --publish=4200:4200 --publish=5432:5432 crate:latest | ||
|
||
# Use CrateDB, either talking HTTP, or PostgreSQL wire protocol. | ||
python read_pandas.py --dburi='crate+urllib3://crate@localhost:4200' | ||
python read_pandas.py --dburi='crate+psycopg://crate@localhost:5432' | ||
|
||
# Use bulk size parameter to exercise paging. | ||
python read_pandas.py --bulk-size 50 | ||
|
||
# Use CrateDB Cloud. | ||
python read_pandas.py --dburi='crate://admin:<PASSWORD>@example.aks1.westeurope.azure.cratedb.net:4200?ssl=true' | ||
|
||
|
||
Details | ||
======= | ||
To watch the HTTP traffic to your local CrateDB instance, invoke:: | ||
|
||
sudo ngrep -d lo0 -Wbyline port 4200 | ||
|
||
""" | ||
import logging | ||
|
||
import click | ||
import pandas as pd | ||
import sqlalchemy as sa | ||
from pueblo.util.logging import setup_logging | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
SQLALCHEMY_LOGGING = True | ||
|
||
|
||
class DatabaseWorkload: | ||
|
||
table_name = "testdrive_pandas" | ||
|
||
def __init__(self, dburi: str): | ||
self.dburi = dburi | ||
|
||
def get_engine(self, **kwargs): | ||
return sa.create_engine(self.dburi, **kwargs) | ||
|
||
def process(self, bulk_size: int): | ||
""" | ||
Exercise different insert methods of pandas, SQLAlchemy, and CrateDB. | ||
""" | ||
|
||
logger.info(f"Connecting to {self.dburi}") | ||
logger.info(f"Reading data with bulk_size={bulk_size}") | ||
|
||
engine = self.get_engine() | ||
frames = pd.read_sql(sql="SELECT * FROM sys.summits;", con=engine, chunksize=bulk_size) | ||
for df in frames: | ||
print(df) | ||
|
||
|
||
def tweak_log_levels(level=logging.INFO): | ||
|
||
# Enable SQLAlchemy logging. | ||
if SQLALCHEMY_LOGGING: | ||
logging.getLogger("sqlalchemy").setLevel(level) | ||
|
||
|
||
@click.command() | ||
@click.option("--dburi", type=str, default="crate://localhost:4200", required=False, help="SQLAlchemy database connection URI.") | ||
@click.option("--bulk-size", type=int, default=5_000, required=False, help="Bulk size / chunk size.") | ||
@click.help_option() | ||
def main(dburi: str, bulk_size: int): | ||
setup_logging() | ||
tweak_log_levels() | ||
dbw = DatabaseWorkload(dburi=dburi) | ||
dbw.process(bulk_size) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import shlex | ||
import subprocess | ||
|
||
|
||
def run(command: str): | ||
subprocess.check_call(shlex.split(command)) | ||
|
||
|
||
def test_read_urllib3(): | ||
cmd = "time python read_pandas.py --dburi=crate+urllib3://crate@localhost:4200" | ||
run(cmd) | ||
|
||
|
||
def test_read_psycopg3(): | ||
cmd = "time python read_pandas.py --dburi=crate+psycopg://crate@localhost:5432" | ||
run(cmd) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or 5432 if they use the psycopg example
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, but sniffing the PostgreSQL wire protocol, you will probably not be able to see sensible things as a human. This is different to the HTTP protocol, because it is marshalling from/to JSON.
Do you think anything should be improved here, e.g. by adding such a comment that it only makes sense with HTTP/JSON?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just for info, I regularly use this with the pgsql protocol, not everything is human-readable but SQL statements are visible in clear text which is very helpful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah. Nice to learn about. Thanks! ✨