Python function to query a SQLite file stored on S3
Project description
sqlite-s3-query
![Test Coverage](https://pypi-camo.freetls.fastly.net/48d3b145aa899400137c1119c7c900337be7c2f9/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f38653663323563333535323164366233333866612f746573745f636f766572616765)
Python function to query a SQLite file stored on S3. It uses multiple HTTP range requests per query to avoid downloading the entire file, and so is suitable for large databases.
All the HTTP requests for a query request the same version of the database object in S3, so queries should complete succesfully even if the database is replaced concurrently by another S3 client. Versioning must be enabled on the S3 bucket.
Operations that write to the database are not supported.
Installation
sqlite-s3-query depends on APSW, which is not available on PyPI, but can be installed directly from GitHub.
pip install sqlite_s3_query
pip install https://github.com/rogerbinns/apsw/releases/download/3.36.0-r1/apsw-3.36.0-r1.zip --global-option=fetch --global-option=--version --global-option=3.36.0 --global-option=--all --global-option=build --global-option=--enable-all-extensions
Usage
from sqlite_s3_query import sqlite_s3_query
results_iter = sqlite_s3_query(
'SELECT * FROM my_table WHERE my_column = ?', params=('my-value',),
url='https://my-bucket.s3.eu-west-2.amazonaws.com/my-db.sqlite',
)
for row in results_iter:
print(row)
If in your project you use multiple queries to the same file, functools.partial
can be used to make an interface with less duplication.
from functools import partial
from sqlite_s3_query import sqlite_s3_query
query_my_db = partial(sqlite_s3_query,
url='https://my-bucket.s3.eu-west-2.amazonaws.com/my-db.sqlite',
)
for row in query_my_db('SELECT * FROM my_table WHERE my_col = ?', params=('my-value',)):
print(row)
for row in query_my_db('SELECT * FROM my_table_2 WHERE my_col = ?', params=('my-value',)):
print(row)
The AWS region and the credentials are taken from environment variables, but this can be changed using the get_credentials
parameter. Below shows the default implementation of this that can be overriden.
import os
from functools import partial
from sqlite_s3_query import sqlite_s3_query
query_my_db = partial(sqlite_s3_query
url='https://my-bucket.s3.eu-west-2.amazonaws.com/my-db.sqlite',
get_credentials=lambda: (
os.environ['AWS_DEFAULT_REGION'],
os.environ['AWS_ACCESS_KEY_ID'],
os.environ['AWS_SECRET_ACCESS_KEY'],
os.environ.get('AWS_SESSION_TOKEN'), # Only needed for temporary credentials
),
)
for row in query_my_db('SELECT * FROM my_table_2 WHERE my_col = ?', params=('my-value',)):
print(row)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for sqlite_s3_query-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fc55d54fa7d1c1a1d8f7397fbafc288b968a28573e7e441014ad8c4468c5e7e8 |
|
MD5 | 2db4c96d9d770c6f350fa52444c78e3e |
|
BLAKE2b-256 | 42e5c3fa39bc65e8ab595ee2a3f6553abce6ce927af11a197eb8ffe7658af33e |