A python package for connecting with Cassandra, MongoDB, mySQL.
Project description
dbLinkPro
dbLinkPro
is a versatile Python package designed to simplify database interactions by offering a unified interface for performing CRUD (Create, Read, Update, Delete) operations across Cassandra, MongoDB, and MySQL databases. This package not only supports essential database operations but also enhances the user experience with additional features specific to each database type.
Features
Cassandra-Specific Enhancements
- Docker Integration: Automatically starts a Cassandra Docker container if it's not running. Handles container creation, volume management, and startup checks.
- Connection Timeout Handling: Implements retry logic to handle connection delays, ensuring that Cassandra is fully operational before proceeding.
- Keyspace and Table Management: Create and manage keyspaces and tables with flexible schema definitions. Switch between keyspaces easily.
- Schema Retrieval: Retrieve and validate table schemas, including checking the compatibility of data types for updates and deletions.
- Bulk Data Ingestion: Supports bulk insertion of data from CSV and Excel files into Cassandra tables.
- Custom Query Handling: Construct and execute custom queries for inserting, updating, and deleting records with validation based on schema types.
- Container Lifecycle Management: Start, stop, and remove Cassandra Docker containers programmatically.
Installation
You can install dbLinkPro
using pip:
pip install dbLinkPro
Usage
MySQL
from database_automation import mysql_crud
Create a connection instance
connection = mysql_crud.MySQLConnection(
host='localhost',
user='your_username',
password='your_password',
database='test_db',
port=3306
)
Connect to the database
connection.connect()
Create a new database if it does not exist
connection.create_database()
Table operations
columns = {
'id': 'INT AUTO_INCREMENT PRIMARY KEY',
'name': 'VARCHAR(100)',
'age': 'INT'
}
connection.create_table('person', columns)
record = {
'name': 'Alice',
'age': 28
}
connection.insert_record('person', record)
records = connection.select_record('person')
print(records) # Output: [[1, 'Alice', 28]]
update_values = {'age': 29}
connection.update_record('person', update_values, 'name = "Alice"')
connection.delete_record('person', 'name = "Alice"')
Disconnect from the database
connection.disconnect()
MongoDB
from database_automation import mongo_crud
Create an instance of MongoOperation
mongo = mongo_crud.MongoOperation(
client_url='mongodb://localhost:27017/',
database_name='test_db',
collection_name='test_collection'
)
Create a MongoDB client
client = mongo.create_mongo_client()
Create or access the database
database = mongo.create_database()
Create or access the collection
collection = mongo.create_collection()
CRUD operations
single_record = {'name': 'John Doe', 'age': 35}
mongo.insert_record(single_record, 'test_collection')
multiple_records = [
{'name': 'Jane Smith', 'age': 28},
{'name': 'Emily Davis', 'age': 40}
]
mongo.insert_record(multiple_records, 'test_collection')
mongo.bulk_insert('data.csv', 'test_collection')
mongo.bulk_insert('data.xlsx', 'test_collection')
Cassandra
from database_automation import cassandra_crud
Create an instance of CassandraOperation
cassandra = cassandra_crud.CassandraOperation(
contact_points=['127.0.0.1'],
volume='cassandra_data'
)
Connect to Cassandra
session = cassandra.connect(username='your_username', password='your_password')
Create a keyspace
cassandra.create_keyspace('test_keyspace', strategy='SimpleStrategy', replicas=1)
Use the created keyspace
cassandra.use_keyspace('test_keyspace')
Define table schema
schema = {
'id': 'int',
'name': 'text',
'age': 'int'
}
Create a table
cassandra.create_table('test_table', schema)
Insert a record
record = {'id': 1, 'name': 'John Doe', 'age': 30}
cassandra.insert_record('test_table', record)
Bulk insert from a CSV file
cassandra.bulk_insert('data.csv', 'test_table')
Fetch Records
rows = cassandra.fetch_records('test_table')
for row in rows:
print(row)
Update a record
update_values = {'name': 'Jane Doe', 'age': 29}
cassandra.update_record('test_table', 'id', 1, update_values)
Delete a record
cassandra.delete_record('test_table', 'id', 1)
Close the connection
cassandra.close()
Contributing
I welcome contributions to dbLinkPro. If you'd like to contribute, please..
- Fork the repository on GitHub.
- Create a new branch for your changes.
- Make your changes and commit them with descriptive messages.
- Push your changes to your fork.
- Submit a pull request with a detailed explanation of your changes.
License
dbLinkPro is licensed under the MIT License.
Author/Maintainer
Sneh Pillai
Contact Information
For support or questions, please contact: snehpillai02@gmail.com
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 dbLinkPro-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8c167661da6a392ed9085f566a5a96f7fdb89f80753d99f22997854d1471cba1 |
|
MD5 | e0249bbcb1a01a6503554b2c95e99e44 |
|
BLAKE2b-256 | 049e9fb53ccbcd55d1430a7b3cef4dca7f1eba73885b7fd3b461b18713ec0e8a |