Blog

Filter posts by Category Or Tag of the Blog section!

ScyllaDB with a basic example in python

Sunday, 01 August 2021

ScyllaDB is a distributed NoSQL database that is designed to provide high performance and low latency. It is based on the Apache Cassandra project but is designed to be faster and more scalable. ScyllaDB is written in C++ and uses a shared-nothing architecture, which means that each node in the cluster is independent and does not share resources with other nodes. This allows for better performance and scalability since each node can be optimized for its specific workload. 

 

ScyllaDB supports the CQL (Cassandra Query Language) API and provides features such as automatic sharding, automatic replication, and built-in support for secondary indexes. It also supports features such as transactions and materialized views. ScyllaDB is commonly used in applications that require high throughputs and low latency, such as real-time analytics and ad-serving platforms.


Install the cassandra-driver  package and execute the following code:
 

from cassandra.cluster import Cluster



# Connect to the ScyllaDB cluster

cluster = Cluster(['localhost'])

session = cluster.connect()



# Create a keyspace

session.execute("CREATE KEYSPACE IF NOT EXISTS mykeyspace WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'}")



# Create a table

session.execute("CREATE TABLE IF NOT EXISTS mykeyspace.mytable (id uuid PRIMARY KEY, name text)")



# Insert some data

session.execute("INSERT INTO mykeyspace.mytable (id, name) VALUES (uuid(), 'John Doe')")

session.execute("INSERT INTO mykeyspace.mytable (id, name) VALUES (uuid(), 'Jane Doe')")



# Retrieve data

rows = session.execute("SELECT * FROM mykeyspace.mytable")

for row in rows:

    print(row.id, row.name)



# Close the session and cluster

session.shutdown()

cluster.shutdown()

 

This example connects to a ScyllaDB cluster running on localhost, creates a keyspace named mykeyspace, creates a table named mytable, inserts some data into the table, retrieves the data, and then closes the session and cluster.

 

comments powered by Disqus