Registry / aws / aws-kinesis-agg

aws-kinesis-agg

JSON →
library1.2.3pypypi✓ verified 22d ago

The `aws-kinesis-agg` Python module assists in handling the Kinesis Producer Library (KPL) message aggregation format. It provides utilities for both aggregating multiple user records into a single Kinesis record (for improved throughput and cost efficiency) and deaggregating such records back into their original user records. The library is currently at version 2.0.3 (on GitHub, PyPI shows 1.2.3) with an active, though irregular, release cadence.

pip install aws-kinesis-agg
INSTALL
IMPORT
SIG · AWS-KINESIS-AGG
A
aws-kinesis-agg
awspythonv1.2.3
Install
1.9s avg
Import
Disk
18MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.2.3 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 19.6MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.9s · import 0.000s · 21MB
18MB installed
● package 18MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

KinesisAggregator
from aws_kinesis_agg import KinesisAggregator
from aws_kinesis_agg import KinesisAggregator

This quickstart demonstrates both aggregation and deaggregation. It shows how to use `KinesisAggregator` to combine multiple user records into a single KPL-formatted record and how to use `deaggregate_records` or `iter_deaggregate_records` to extract original user records from an aggregated KPL record. The deaggregation example includes handling base64-encoded data, common when processing Kinesis records from AWS Lambda events.

import base64 from aws_kinesis_agg.aggregator import KinesisAggregator from aws_kinesis_agg.deaggregator import deaggregate_records, iter_deaggregate_records # --- Aggregation Example --- print("--- Aggregation Example ---") aggregator = KinesisAggregator() # Add multiple user records with partition keys and optional explicit hash keys aggregator.add_user_record("my_app_pk", b"data_for_record_1") aggregator.add_user_record("my_app_pk", b"data_for_record_2", explicit_hash_key="12345") aggregator.add_user_record("another_pk", b"data_for_record_3") # Get the aggregated record bytes, ready to be sent to Kinesis aggregated_bytes = aggregator.get_aggregated_record_bytes() print(f"Aggregated record size: {len(aggregated_bytes)} bytes") # --- Deaggregation Example --- print("\n--- Deaggregation Example ---") # Simulate receiving an aggregated record, e.g., from an AWS Lambda Kinesis event. # Lambda events base64-encode the data, so we simulate that. # First, create a fresh aggregated record to ensure valid input for deaggregation fresh_aggregator = KinesisAggregator() fresh_aggregator.add_user_record("deagg_pk_a", b"deaggregated_data_A") fresh_aggregator.add_user_record("deagg_pk_b", b"deaggregated_data_B", explicit_hash_key="98765") fresh_aggregator.add_user_record("deagg_pk_c", b"deaggregated_data_C") simulated_kpl_record_bytes = fresh_aggregator.get_aggregated_record_bytes() # If receiving from Lambda, data would be base64 encoded: simulated_kpl_record_base64 = base64.b64encode(simulated_kpl_record_bytes).decode('utf-8') print(f"Simulated KPL record (base64-encoded for Lambda-like input): {simulated_kpl_record_base64[:70]}...") # Decode the base64 data first, as Lambda would deliver it kinesis_record_data = base64.b64decode(simulated_kpl_record_base64) # 1. Using deaggregate_records (returns a list of UserRecord objects) print("\nDeaggregated records (using deaggregate_records, returns a list):") user_records_list = deaggregate_records(kinesis_record_data) for i, record in enumerate(user_records_list): print(f" Record {i+1}: PartitionKey={record.partition_key}, Data={record.data.decode()}") # 2. Using iter_deaggregate_records (returns an iterator, memory efficient for many records) print("\nDeaggregated records (using iter_deaggregate_records, returns an iterator):") user_records_iterator = iter_deaggregate_records(kinesis_record_data) for i, record in enumerate(user_records_iterator): print(f" Record {i+1}: PartitionKey={record.partition_key}, Data={record.data.decode()}")
Debug
Known issues
gotchaThe PyPI version (`pip install aws-kinesis-agg`) often lags significantly behind the latest GitHub releases. For example, PyPI currently offers 1.2.3 while GitHub has 2.0.3. Users should be aware they might install an outdated version if relying solely on `pip install`.
fix
Check the GitHub repository's releases for the latest version. If a newer version is needed, consider installing directly from GitHub or specifying the exact version if it eventually lands on PyPI (e.g., `pip install aws-kinesis-agg==2.0.3`).
affects: <2.0.3
breakingVersion 2.0.0 introduced support for AWS SDK V2. While the core API of this Python library primarily deals with data formats, this implies a shift in expected behavior or compatibility with KPL clients built with newer AWS SDKs. Users migrating from pre-2.0.0 versions should re-test their integrations.
fix
Review your Kinesis producer and consumer applications to ensure compatibility, especially if you are using older versions of AWS SDKs elsewhere in your stack. Test thoroughly after upgrading.
affects: <2.0.0
gotchaThis library is exclusively designed for records aggregated using the Kinesis Producer Library (KPL) format. It will not correctly deaggregate standard Kinesis records or records aggregated by other custom methods. Feeding non-KPL aggregated data may result in errors or malformed output.
fix
Before deaggregating, ensure the Kinesis record data is indeed a KPL-aggregated record. KPL records begin with a magic number (0xF3899AFC). The deaggregation functions in this library perform this check internally and will raise an `InvalidKPLRecordError` if it's not a valid KPL record.
affects: All
gotchaWhen consuming Kinesis records via AWS Lambda events, the `data` field for each Kinesis record in the event payload is Base64-encoded. You must decode this string back into raw bytes (`base64.b64decode()`) before passing it to `deaggregate_records` or `iter_deaggregate_records`.
fix
Always apply `base64.b64decode(record_data_string)` to the Kinesis record data obtained from Lambda events before attempting to deaggregate.
affects: All
Upgrade
Version history
1.2.3latest on PyPI · released Jun 10, 2022
Audit
Dependencies
boto3requiredRequired for interacting with AWS services, including Kinesis (though the library itself doesn't directly send/receive, it expects boto3 to be available for broader AWS workflows).
protobufrequiredRequired for parsing and serializing the KPL's Protocol Buffer message format.
Agent activity
23 hits · last 30 days
node
20
OpenAI (training)
1
Resources
aws-kinesis-agg — pip install aws-kinesis-agg · libregistry