Arrow -> PostgreSQL encoder
Project description
pgpq
Convert PyArrow RecordBatches to Postgres' native binary format.
Usage
"""Example for README.md"""
from tempfile import mkdtemp
import psycopg
import pyarrow.dataset as ds
import requests
from pgpq import ArrowToPostgresBinaryEncoder
# let's get some example data
tmpdir = mkdtemp()
with open(f"{tmpdir}/yellow_tripdata_2023-01.parquet", mode="wb") as f:
resp = requests.get(
"https://d37ci6vzurychx.cloudfront.net/trip-data/yellow_tripdata_2023-01.parquet"
)
resp.raise_for_status()
f.write(resp.content)
# load an arrow dataset
# arrow can load datasets from partitioned parquet files locally or in S3/GCS
# it handles buffering, matching globs, etc.
dataset = ds.dataset(tmpdir)
# create an encoder object which will do the encoding
# and give us the expected Postgres table schema
encoder = ArrowToPostgresBinaryEncoder(dataset.schema)
# get the expected Postgres destination schema
# note that this is _not_ the same as the incoming arrow schema
# and not necessarily the schema of your permanent table
# instead it's the schema of the data that will be sent over the wire
# which for example does not have timezones on any timestamps
pg_schema = encoder.schema()
# assemble ddl for a temporary table
# it's often a good idea to bulk load into a temp table to:
# (1) Avoid indexes
# (2) Stay in-memory as long as possible
# (3) Be more flexible with types
# (you can't load a SMALLINT into a BIGINT column without casting)
cols = [f'"{col_name}" {col.data_type.ddl()}' for col_name, col in pg_schema.columns]
ddl = f"CREATE TEMP TABLE data ({','.join(cols)})"
with psycopg.connect("postgres://postgres:postgres@localhost:5432/postgres") as conn:
with conn.cursor() as cursor:
cursor.execute(ddl) # type: ignore
with cursor.copy("COPY data FROM STDIN WITH (FORMAT BINARY)") as copy:
copy.write(encoder.write_header())
for batch in dataset.to_batches():
copy.write(encoder.write_batch(batch))
copy.write(encoder.finish())
# load into your actual table, possibly doing type casts
# cursor.execute("INSERT INTO \"table\" SELECT * FROM data")
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
pgpq-0.9.0.tar.gz
(24.9 kB
view hashes)
Built Distributions
pgpq-0.9.0-cp37-abi3-win_amd64.whl
(501.0 kB
view hashes)
pgpq-0.9.0-cp37-abi3-win32.whl
(442.2 kB
view hashes)
Close
Hashes for pgpq-0.9.0-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 905f820238b970a516cbbd7fe5fed576ba157bd054da8abcae9c3d9e39447605 |
|
MD5 | 5e5140ff363212e43a42743443c97de5 |
|
BLAKE2b-256 | 490bb1480d4d2daf1018489f340b2cc3c9653386eb1173cab963c3af61da4498 |
Close
Hashes for pgpq-0.9.0-pp39-pypy39_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ea1e93168d79a30f99f17f0279e65978e6877b09097b0106503b7b543747aeaf |
|
MD5 | b0cd9accaeda462e0a01174a79912777 |
|
BLAKE2b-256 | 885b6b3a353258f09dbca0c466ba9aa0f0cd7d3382e829ce4af593be8e47f49f |
Close
Hashes for pgpq-0.9.0-pp39-pypy39_pp73-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8c2ad904babf35d966a441a1a6680b6eaef0a47093047c2914d86c8c0b5530a8 |
|
MD5 | 5f7a7771eb511330c041fe50820e5680 |
|
BLAKE2b-256 | a24abe6cc6f93a218920ad0308ac47422f25e0612048b404627cbc6b8bc2c08b |
Close
Hashes for pgpq-0.9.0-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 160836cecce86fa60c02ce25166ff20ae03f87957cd0f85ab4aacd64c9891730 |
|
MD5 | 3c51218487f662a28b7779b0f3440aa5 |
|
BLAKE2b-256 | 99a83e3d173e0df878309a2bf67faa1f3c07e026c1a952c16f7086f344203807 |
Close
Hashes for pgpq-0.9.0-pp38-pypy38_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 329ec29c9784f380d0321aac0886363b016bf7da394e5e72bd329fe34ed041e9 |
|
MD5 | dea4aa9a1a4fe1df18a5ae6acfb9d741 |
|
BLAKE2b-256 | 9583e2821409067d6a71955af2ec01297c7fb4ee10dfbf307663b71b663ae6c7 |
Close
Hashes for pgpq-0.9.0-pp38-pypy38_pp73-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a8f963b79b54e10d6ce015b37a6ede386e9db450780623453cdcef18407d646c |
|
MD5 | fd96e85f93d603dcca7e7ae6bdb97097 |
|
BLAKE2b-256 | 6ee82e8dc2b4128619d5b764a673249b77bcbedf22dc9a9d895770850b9cb48b |
Close
Hashes for pgpq-0.9.0-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 98aa74978b0b85076aaec6022bc0cef68417ac8922d8e53e7c8deabe70f35bab |
|
MD5 | 9a48d4160e28a9efc4fb7641ae2d5fe5 |
|
BLAKE2b-256 | a7b8f492eb30c71d0bbf4e63396eeaceadc60a78b366413568c28bd4ecd6b3d0 |
Close
Hashes for pgpq-0.9.0-pp37-pypy37_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 259b0aca8b06a1757dc16e668457a3a30a8074cd095612c2312c2e400e7f5418 |
|
MD5 | cdbec72a1182a888a8307b88148be990 |
|
BLAKE2b-256 | c5785535b0c119b987d90e57192e2549b6923868416a6c388b98aa4edffc6096 |
Close
Hashes for pgpq-0.9.0-pp37-pypy37_pp73-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c6463e1b28ffc33efcb37d7fc5d4bf144078fc2f4bd8fc74c114e9883ae66b76 |
|
MD5 | f0f13b66ccfa25ee7a41dd95bacee150 |
|
BLAKE2b-256 | 0a1598a0775d95bac085746d25fb320b78ffdca8abf029da2e5f5067dca5395f |
Close
Hashes for pgpq-0.9.0-cp37-abi3-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1058f1ebb6cf2e879351c42af4cc7836bcd239cdff9690ba5c738b74450e9cc7 |
|
MD5 | 1844874c39ce1c8469539a78f2cc88d8 |
|
BLAKE2b-256 | df96e8f76dfcf12cbb40bda62867ec96aea19af626439578a0a602b309085973 |
Close
Hashes for pgpq-0.9.0-cp37-abi3-musllinux_1_2_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | acd844a36807db1ecb18289f24001d44a3fc8aefff01fe6f7a4b352fda659960 |
|
MD5 | 11e933a4f7789d8bad10d60c035e7aa9 |
|
BLAKE2b-256 | f81ec86c3c8807d48eccddc555e74c3c130d2baa07d1ff0626d87c8d7e2932bc |
Close
Hashes for pgpq-0.9.0-cp37-abi3-musllinux_1_2_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3e7cd18d0fa7fc216216561686cfa1da1c0ce6f2e175771b6f291edcdc0fb7b5 |
|
MD5 | d6abdbbe361bb22fe8d89b9d7b2eb131 |
|
BLAKE2b-256 | 7c647703af6d71c9e5be32015f6a9ecffc797b09bf7ce071a2f0c62c1b3ecdff |
Close
Hashes for pgpq-0.9.0-cp37-abi3-musllinux_1_2_armv7l.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 07068d5bf10fc54fafa6e28ed2c7551422d3366cce931f706ec184ed19ebac73 |
|
MD5 | c5ed8ce413bce8df5555d3109c97e7e0 |
|
BLAKE2b-256 | 07680db33bc0540789ba6ec2b8eda176a7fbd309f6dfc26bd056981de46e77c9 |
Close
Hashes for pgpq-0.9.0-cp37-abi3-musllinux_1_2_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5fac7b1c0f64e0bf7f0c11e81720b527593acc2b83c48d20669fd059e5e67f24 |
|
MD5 | 42c021d8e7fd76083345c080a7b36470 |
|
BLAKE2b-256 | 7cfe10edc9c81f5fbc7cceffb1d5415bd7ef441021791192956f0ffd59254d79 |
Close
Hashes for pgpq-0.9.0-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9a34708f8a1d5ad202c5e7726969638f0dadffd8a9463ef23c00db51e3a110ec |
|
MD5 | 1f74ff90478199af1a4d5d447114cb55 |
|
BLAKE2b-256 | 5e1e0561c2fa8ca9f43781aba5cd3badb379bd3c59d39538cee9ef1461cf8fbd |
Close
Hashes for pgpq-0.9.0-cp37-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a15a348d695e0a4a4998ab1ef0b8bbf5b927cbf53a124a89f9b7c3c51967833d |
|
MD5 | 971822f5388fb89ade70189471e8a321 |
|
BLAKE2b-256 | b4083d6f0c5086d8059ff9ac53785a34dbf6bb3f4cac4ffab8ec7cd5aa23141e |
Close
Hashes for pgpq-0.9.0-cp37-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ea460f7abdd1bb17fdc8b2312f63f141d5e93c21a28f93dd47309db4a5cdfa01 |
|
MD5 | 69bfa135ee973c837ee1ab0bb65dae3c |
|
BLAKE2b-256 | 6c1449decb622edf24b0b5a3a1841748ba455caed9e024a3fa3fdc6ad1320427 |
Close
Hashes for pgpq-0.9.0-cp37-abi3-manylinux_2_17_ppc64.manylinux2014_ppc64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d39a369f2b77db2428850d85622ed5fa0506fc89a58ac9122cb83c1535273231 |
|
MD5 | 682e45caef0bf5e30765f8fc2e8d8126 |
|
BLAKE2b-256 | b09f7812366dd4e04d003e6209bf70a81e884d9685bb2840e66056ccb7f72bdc |
Close
Hashes for pgpq-0.9.0-cp37-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cd00b7bb24e58d7dc14e7a1f51f15c5f6f69ad64502f8ee68b6963c214d10a86 |
|
MD5 | 78a129c810ae6bfce966f3c677adad3a |
|
BLAKE2b-256 | 27fd3359b4079439cc8d0e8746fadede0198de09a8f0c899c804f01ebcad8a19 |
Close
Hashes for pgpq-0.9.0-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ea47fcd7c5fae68f77a12a3a1b32d68f2f28a0dbccb98d89366d6e5973bb5c26 |
|
MD5 | 4fde2e9b4b5d9a4da66b7598f4b206bc |
|
BLAKE2b-256 | e78f8dc0ddfafa31a35d0c32429b8d43583a79ac9369366c6b52a7ef6477f1a5 |
Close
Hashes for pgpq-0.9.0-cp37-abi3-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 105b9bca22409fbb8adc8f2c1fc29d887c8d3bfdb8ab0defe911226f1681d3b1 |
|
MD5 | b3823bc80ac64827b8aea92356e3e7a9 |
|
BLAKE2b-256 | 2b1d9d465ea07f59fe3c256af3a0f40713b59646bed0a8366b26cf63a9888f9f |
Close
Hashes for pgpq-0.9.0-cp37-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b1c11911f7481b82c1a5272792953e40efe1d7e49290d4da787f5a927a1f3b1b |
|
MD5 | 6a709f02eaff9c5a191a50b4b59c39ad |
|
BLAKE2b-256 | 5f146bdd50f6bef237f398a6b3f6c335568c596c39b5c62d0a1a6e44b0441c1e |
Close
Hashes for pgpq-0.9.0-cp37-abi3-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5d16e6f8c86339ae376e9f62ec5a15e73a1af4e39f982bf456904c03395eb2fa |
|
MD5 | 28e298dffeb74431b21301f13aaf9db4 |
|
BLAKE2b-256 | ca5b3ec6ced557b6bde764c8bf527c4fef10ba4fe8ae50c399b74feb30bcc40d |