Skip to main content

A package for streaming cross-server dataset joins in memory

Project description

crosstream 🐍🌊🤝

This Python package helps join large datasets across servers efficiently. It accomplishes this by streaming the data, allowing it to:

  • read each dataset only once
  • not need to store the complete datasets in memory

This is particularly helpful when your datasets are larger than the available memory on your machine or when you want to minimize network reads.

This package supports CSV and pyodbc datasources.

Installation

From PyPI:

pip install crosstream

From source:

git clone https://github.com/bertwagner/crosstream.git
cd crosstream
pip install .

Usage Examples

Basic hash join

Ideally you want to make your smaller dataset the first dataset in your join.

import crosstream as cs
import csv

file1 = 'small_dataset.csv'
file2 = 'large_dataset.csv'

# join using column indexes or column names
c1 = cs.read_csv(file1,True,[0,1])
c2 = cs.read_csv(file2, True, ['col1','col2'])

# specify the output file
with open('joined_output.csv', 'w') as f:
    w = csv.writer(f)
    
    # write header column names
    w.writerow(c1.column_names + c2.column_names)

    for row_left,row_right in cs.inner_hash_join(c1,c2):
        # write matched results to our joined_output.csv
        w.writerow(row_left + row_right)

More examples can be found under the tests directory.

Custom method for determining equality

By default, this package will join only if all values are equal.

If you want to perform a transformation on your data before comparing for equality, or use more complicated join equality logic, you can pass in your own function into override_build_join_key to define how equality is determined:

# define a function for transforming join key data before it's hashed
def custom_join_key_transform(value):
    transformed = value.replace(' ','')
    return transformed

And then pass that into the inner_hash_join() method:

...
for row_left,row_right in cs.inner_hash_join(c1,c2,override_join_key_transform=custom_join_key_transform)
...

Custom method for processing matched hashes

By default this package returns tuples of the joined rows. If you want to customize what the output of your matched data looks like (perform transformations after a match is found), you can pass a function to the override_process_matched_hashes argument:

# define a function for performing additional transformations or adding additional outputs before the columns are returned
def custom_process_matched_hashes(bucket_row,probe_row, bucket_join_column_indexes, probe_join_column_indexes):
    # adding a new column indicating the weights of these matches are equal to 1
    weight=1.0
    return tuple(bucket_row),tuple(probe_row),(weight,)

And then pass that into the inner_hash_join() method:

for row_left,row_right,weight in cs.inner_hash_join(c1,c2,override_process_matched_hashes=custom_process_matched_hashes):

Tests

If you want to run the tests, you will need to ensure you have the sqlite odbc driver installed:

apt-get install libsqliteodbc unixodbc

Then execute the tests:

pytest

And check for coverage:

pytest --cov-config=tests/.coveragerc --cov=crosstream --cov-report term-missing

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

crosstream-3.0.0.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

crosstream-3.0.0-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file crosstream-3.0.0.tar.gz.

File metadata

  • Download URL: crosstream-3.0.0.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.2

File hashes

Hashes for crosstream-3.0.0.tar.gz
Algorithm Hash digest
SHA256 7769895a332869d566bc115aaed8b691fbdb65d01d41654f3ea493d2dcb23daa
MD5 1b709bb64d0b49f0fc5cdb2afd897f04
BLAKE2b-256 e3abb2f60b10d5da514f0d6415796c440877673c2460298ca91207792c80bf54

See more details on using hashes here.

File details

Details for the file crosstream-3.0.0-py3-none-any.whl.

File metadata

  • Download URL: crosstream-3.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.2

File hashes

Hashes for crosstream-3.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6d3b34c65d3260480a8b42d5a2776f337e59c61660ef390072aaedcf56870e3d
MD5 dd5806a13045c6198e8a75d2d5370ef8
BLAKE2b-256 a1a597a7f22fecc006c4e060144ac9ae481585cff94158c0d5cee1886fb05b98

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page