A simple CRUD library for Apache Cassandra
Project description
CassandraCRUD Library
CassandraCRUD is a Python library that provides a simple and flexible interface for interacting with Apache Cassandra databases. It wraps the DataStax Cassandra driver and offers additional utility functions for common database operations.
Features
- Easy connection management
- Flexible query execution (synchronous and asynchronous)
- Batch operations support
- Customizable consistency levels
- Built-in CRUD operations
- Table management utilities
- Configurable through environment variables or direct parameters
Installation
To install CassandraCRUD, you can use pip:
pip install cassandracrud
Usage
Here's a basic example of how to use CassandraCRUD:
from cassandracrud import CassandraCRUD
# Initialize the CassandraCRUD instance
crud = CassandraCRUD(
contact_points=["localhost"],
keyspace="my_keyspace",
username="user",
password="pass"
)
# Connect to the database
crud.connect()
# Execute a query
result = crud.execute("SELECT * FROM my_table")
# Create a new record
crud.create("my_table", {"id": 1, "name": "John Doe"})
# Close the connection
crud.close()
Configuration
You can configure CassandraCRUD either by passing parameters to the constructor or by setting environment variables:
CASSANDRA_PROD_CONTACT_POINTS: Comma-separated list of contact pointsCASSANDRA_PROD_KEYSPACE: Keyspace nameCASSANDRA_PROD_USERNAME: Username for authenticationCASSANDRA_PROD_PASSWORD: Password for authentication
If both environment variables and constructor parameters are provided, the constructor parameters take precedence.
Main Methods
connect(): Establishes a connection to the Cassandra clusterexecute(query, params=None): Executes a CQL queryexecute_async(query, params=None): Executes a CQL query asynchronouslyprepare(query): Prepares a CQL statementexecute_batch(statements): Executes a batch of CQL statementscreate(table, data): Inserts a new record into a tableread(table, conditions=None): Retrieves records from a tableupdate(table, data, conditions): Updates records in a tabledelete(table, conditions): Deletes records from a table
Utility Methods
table_exists(table_name): Checks if a table existscreate_table(table_name, column_definitions): Creates a new tabledrop_table(table_name): Drops a tableget_table_schema(table_name): Retrieves the schema of a tableget_metrics(): Retrieves cluster metricsset_consistency_level(consistency_level): Sets the consistency level for queries
Advanced Usage
Asynchronous Queries
future = crud.execute_async("SELECT * FROM my_table")
# Do other work...
result = future.result()
Batch Operations
statements = [
("INSERT INTO my_table (id, name) VALUES (%s, %s)", (1, "John")),
("INSERT INTO my_table (id, name) VALUES (%s, %s)", (2, "Jane"))
]
crud.execute_batch(statements)
Changing Consistency Level
from cassandra import ConsistencyLevel
crud.set_consistency_level(ConsistencyLevel.ALL)
Contributing
Contributions to CassandraCRUD are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License.
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cassandracrud-0.1.0.tar.gz.
File metadata
- Download URL: cassandracrud-0.1.0.tar.gz
- Upload date:
- Size: 6.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
34474e30ad2d1c9520e8a2de7ebd7dced9e1d2887e6c483900f79d64b8a27d4e
|
|
| MD5 |
6b3ae3c097be640da4da727581843e50
|
|
| BLAKE2b-256 |
5f907aed8dbb4c43bb9144ba0123b5838a3e9229b813548f4fbf1d6fa965a62d
|
File details
Details for the file cassandracrud-0.1.0-py3-none-any.whl.
File metadata
- Download URL: cassandracrud-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
64958c72d6c3a8eba19e11b4c978e2491c3b82e60fe7940116c8754190b98d80
|
|
| MD5 |
8b6d9519cb661bf845c10188f85b4e91
|
|
| BLAKE2b-256 |
ec2b42dd212dfff5b0b447879a78571d731cf817346a654262a2029be65e8809
|