Skip to content

Commit

Permalink
tested asyncioConnection on experimental driver for Cassandra
Browse files Browse the repository at this point in the history
  • Loading branch information
phenobarbital committed Dec 6, 2023
1 parent 514c33c commit baaa7e6
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 1 deletion.
2 changes: 2 additions & 0 deletions INSTALL
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ mkdir /opt/oracle
# unzip instanclient in folder.
sudo sh -c "echo /opt/oracle/instantclient_19_16 > /etc/ld.so.conf.d/oracle-instantclient.conf"
sudo ldconfig
# For Cassandra:
sudo apt-get install libev4 libev-dev
2 changes: 2 additions & 0 deletions asyncdb/drivers/cassandra.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
from cassandra import ReadTimeout
from cassandra.cluster import Cluster, EXEC_PROFILE_DEFAULT, ExecutionProfile, NoHostAvailable, ResultSet
from cassandra.io.asyncorereactor import AsyncoreConnection
from cassandra.io.asyncioreactor import AsyncioConnection

try:
from cassandra.io.libevreactor import LibevConnection
LIBEV = True
Expand Down
82 changes: 82 additions & 0 deletions examples/cassandra_aio.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import asyncio
from cassandra.cluster import Cluster, EXEC_PROFILE_DEFAULT, ExecutionProfile, NoHostAvailable, ResultSet
from cassandra.io.asyncioreactor import AsyncioConnection
from cassandra.auth import PlainTextAuthProvider
from cassandra.query import (
dict_factory,
ordered_dict_factory,
named_tuple_factory,
ConsistencyLevel,
PreparedStatement,
BatchStatement,
SimpleStatement,
BatchType
)
from cassandra.policies import (
DCAwareRoundRobinPolicy,
DowngradingConsistencyRetryPolicy,
# RetryPolicy
)
try:
from cassandra.io.libevreactor import LibevConnection
LIBEV = True
except ImportError:
LIBEV = False


def main():
params = {
"host": "127.0.0.1",
"port": "9042",
"username": 'cassandra',
"password": 'cassandra'
}
_auth = {
"username": params["username"],
"password": params["password"],
}
policy = DCAwareRoundRobinPolicy()
defaultprofile = ExecutionProfile(
load_balancing_policy=policy,
retry_policy=DowngradingConsistencyRetryPolicy(),
request_timeout=60,
row_factory=dict_factory,
consistency_level=ConsistencyLevel.LOCAL_QUORUM,
serial_consistency_level=ConsistencyLevel.LOCAL_SERIAL,
)
profiles = {
EXEC_PROFILE_DEFAULT: defaultprofile
}
params = {
"port": params["port"],
"compression": True,
"connection_class": AsyncioConnection,
"protocol_version": 4,
"connect_timeout": 60,
"idle_heartbeat_interval": 0
}
auth_provider = PlainTextAuthProvider(**_auth)
_cluster = Cluster(
["127.0.0.1"],
auth_provider=auth_provider,
execution_profiles=profiles,
**params,
)
print(_cluster)
try:
connection = _cluster.connect(keyspace=None)
print('CONNECTION > ', connection)
response = connection.execute("SELECT release_version FROM system.local")
result = [row for row in response]
print(result)
except NoHostAvailable as ex:
raise RuntimeError(
f'Not able to connect to any of the Cassandra contact points: {ex}'
) from ex


if __name__ == "__main__":
try:
main()
finally:
pass
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def readme():
],
author=__author__,
author_email=__author_email__,
packages=find_packages(exclude=["contrib", "docs", "tests", "examples"]),
packages=find_packages(exclude=["bin", "contrib", "docs", "tests", "examples", "libraries"]),
package_data={"asyncdb": ["py.typed"]},
license=__license__,
setup_requires=[
Expand Down

0 comments on commit baaa7e6

Please sign in to comment.