The RedshiftHelper class is a Python utility for working with Amazon Redshift, a data warehousing service. This class provides methods to simplify the construction of SQL queries and operations on Redshift tables.
To use the RedshiftHelper class, you need to initialize it by providing the necessary connection parameters:
from redshift_connector import RedshiftHelper
redshift = RedshiftHelper(
host='your_redshift_host',
user='your_username',
password='your_password',
database='your_database_name',
port=5439 # Default Redshift port
)You can establish a connection to the Redshift cluster using the connect method:
redshift.connect()The RedshiftHelper class allows you to build SQL queries easily. You can specify the columns to select, add WHERE conditions, and specify GROUP BY columns:
redshift.select(['column1', 'column2']).where('column3', 'value').groupBy('column4')You can execute various types of queries, such as select, count, update, delete, and custom SQL queries using the respective methods:
# Execute a SELECT query and fetch results as dictionaries
results = redshift.get('your_table_name')
# Perform a COUNT(*) operation
count = redshift.count('your_table_name')
# Update records in the table
redshift.update('your_table_name', {'column1': 'new_value'})
# Delete records from the table
redshift.delete('your_table_name')
# Execute a custom SQL query
redshift.raw('your_custom_sql_query')Don't forget to disconnect from the Redshift cluster when you're done:
redshift.disconnect()Here's a simple example of how to use the RedshiftHelper class:
redshift = RedshiftHelper(
host='your_redshift_host',
user='your_username',
password='your_password',
database='your_database_name'
)
redshift.connect()
# Build and execute a SELECT query
results = redshift.select(['column1', 'column2']).where('column3', 'value').get('your_table_name')
print(results)
redshift.disconnect()Ensure you have the redshift_connector library installed to use this class.
pip install redshift_connectorThis project is licensed under the MIT License - see the LICENSE file for details.