Skip to content

Commit

Permalink
fix: set the consistency level in connection (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rukesh-Kapuluru authored Jun 13, 2022
1 parent 74f074c commit 9132fe3
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pip-delete-this-directory.txt

# Env
venv
.venv/*
.env
.envrc
env/
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ from cassandra.policies import DCAwareRoundRobinPolicy
from cassandra.cqlengine import columns
from cassandra.cqlengine.management import sync_table
from cassandra.cqlengine.models import Model
from cassandra import ConsistencyLevel

load_balancing_policy = DCAwareRoundRobinPolicy(local_dc='AWS_VPC_AP_SOUTHEAST_2')

Expand All @@ -83,7 +84,8 @@ conn = CassandraConnectionManager(
cluster_ips=["1.1.1.1", "2.2.2.2"],
port=9042,
load_balancing_policy=load_balancing_policy,
)
),
consistency_level=ConsistencyLevel.LOCAL_QUORUM
)

conn = CassandraConnectionManager(
Expand All @@ -93,7 +95,8 @@ conn = CassandraConnectionManager(
load_balancing_policy=load_balancing_policy,
secrets_manager=CassandraSecretsManager(
username_var="MY_CUSTOM_USERNAME_ENV_VAR"),
)
),
consistency_level=ConsistencyLevel.LOCAL_ONE
)

# For running Cassandra model operations
Expand Down
6 changes: 3 additions & 3 deletions hip_data_tools/apache/cassandra.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,15 @@ class CassandraConnectionManager:
for connecting to a cluster
"""

def __init__(self, settings: CassandraConnectionSettings):
def __init__(self, settings: CassandraConnectionSettings, consistency_level: ConsistencyLevel = ConsistencyLevel.LOCAL_ONE):
self._settings = settings
self.cluster = None
self.session = None
self._auth = PlainTextAuthProvider(
username=self._settings.secrets_manager.username,
password=self._settings.secrets_manager.password,
)
self.consistency_level = ConsistencyLevel.LOCAL_QUORUM
self.consistency_level = consistency_level

def get_cluster(self) -> Cluster:
"""
Expand All @@ -236,7 +236,7 @@ def get_session(self, keyspace) -> Session:
"""
if self.session is None:
self.session = self.get_cluster().connect(keyspace)

self.session.default_consistency_level = self.consistency_level
return self.session

def setup_connection(self, default_keyspace) -> None:
Expand Down
3 changes: 2 additions & 1 deletion hip_data_tools/etl/s3_to_cassandra.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from attr import dataclass
from cassandra.datastax.graph import Result
from cassandra import ConsistencyLevel

from hip_data_tools.apache.cassandra import CassandraUtil, CassandraConnectionManager, \
CassandraConnectionSettings
Expand Down Expand Up @@ -42,7 +43,7 @@ def _get_cassandra_util(self):
return CassandraUtil(
keyspace=self.__settings.destination_keyspace,
conn=CassandraConnectionManager(
settings=self.__settings.destination_connection_settings),
settings=self.__settings.destination_connection_settings,consistency_level=ConsistencyLevel.LOCAL_QUORUM),
)

def _get_s3_util(self):
Expand Down

0 comments on commit 9132fe3

Please sign in to comment.