Skip to main content

snippets_py

Integration

New integration from credential
cassandra_client = yepcode.integration.cassandra("credential-slug")
New integration from plain authentication data
from cassandra.cluster import Cluster

cassandra_client = Cluster(['192.168.0.1', '192.168.0.2'])

#The connect() method takes an optional keyspace argument which sets the default keyspace for all queries made through that Session
session = cluster.connect('mykeyspace')
New integration with authentication data
from cassandra.cluster import Cluster, PlainTextAuthProvider

auth_provider = PlainTextAuthProvider(username="username", password="password")
cassandra_client = Cluster(['192.168.0.1', '192.168.0.2'], auth_provider=auth_provider)

#The connect() method takes an optional keyspace argument which sets the default keyspace for all queries made through that Session
session = cluster.connect('mykeyspace')

Executes a Query

Executes a query
rows = cassandra_client.execute('SELECT name, age, email FROM users')

for user_row in rows:
print(user_row.name, user_row.age, user_row.email)

Prepared Statements

Prepared Statements
user_lookup_stmt = cassandra_client.prepare("SELECT * FROM users WHERE user_id=?")

users = []
for user_id in user_ids_to_query:
user = session.execute(user_lookup_stmt, [user_id])
users.append(user)

Execute Concurrent Executions

Delete container
cassandra_client.execute(("CREATE KEYSPACE IF NOT EXISTS examples "
"WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1' }"))
cassandra_client.execute("USE examples")
cassandra_client.execute("CREATE TABLE IF NOT EXISTS tbl_sample_kv (id uuid, value text, PRIMARY KEY (id))")
cassandra_client = session.prepare("INSERT INTO tbl_sample_kv (id, value) VALUES (?, ?)")

for i in range(TOTAL_QUERIES):
cassandra_client.execute_async(prepared_insert, (uuid.uuid4(), str(i)))

Paging

Paging
from cassandra.query import SimpleStatement

query = "SELECT * FROM users"
statement = SimpleStatement(query, fetch_size=10)
results = cassandra_client.execute(statement)

# save the paging_state somewhere and return current results
web_session['paging_state'] = results.paging_state

# resume the pagination sometime later...
statement = SimpleStatement(query, fetch_size=10)
ps = web_session['paging_state']
results = session.execute(statement, paging_state=ps)