Skip to content

Primeight

Primeight


primeight is a Python package designed to make the powerful NoSQL Cassandra database system easily available to everyone. We had two major objectives in mind: encapsulate its partition system with a very easy interface, and enforce Cassandra best practices by defining queries for tables

Additionally we added other proxies to convenient operations such as creation, management, and drop of keyspaces, tables and/or materialized views.

primeight came to life from the necessity to create a standard at Meight Engineering when interacting with Cassandra. We recognised that when dealing with IOT Time Series data there are typically 3 dimensions to partition it: by time, space, and/or identifier.

primeight tries to make the most of Cassandra core ideas, like Query-driven modelling and partitions to optimise for speed, while making available one of the most powerful database technologies to the Python community!

Info

primeight is still in beta, as well as this documentation. If you find anything that you believe is incorrect please feel free to open an issue or open a pull request.

Installation

pip install primeight

Example

This example demonstrates the most important tasks. We will be using the following devices.yaml configuration file.

version: '1.0.0'

name: 'devices'
keyspace: 'meight'

columns:
  device_id:
    type: uuid
  ts:
    type: timestamp
generated_columns:
  day: ts

query:
  base:
    required:
      id: device_id
    optional:
      - day
    order:
      day: desc

Load table configuration

from primeight.parser import YamlParser

config = YamlParser.parse('devices.yaml')

Create connection

from primeight import CassandraManager

manager = CassandraManager(['127.0.0.1'])
manager.connect()

Create keyspace

from primeight import CassandraKeyspace

keyspace = CassandraKeyspace(config, cassandra_manager=manager)
keyspace.create()

Create table

from primeight import CassandraTable

table = CassandraTable(config, cassandra_manager=manager)
table.create()

Insert data

table.insert({'device_id': '659b6222-19fb-416e-9aa5-9df8cf679247', 'ts': 1592491059984}).execute()

Query data

rows = \
    table \
    .query('base') \
    .id('659b6222-19fb-416e-9aa5-9df8cf679247') \
    .execute()

Recap

from primeight.parser import YamlParser
from primeight import CassandraManager, CassandraKeyspace, CassandraTable


# Load configuration.
config = YamlParser.parse('devices.yaml')

# Connect to Cassandra cluster.
manager = CassandraManager(['127.0.0.1'])
manager.connect()

# Create Keyspace and table.
keyspace = CassandraKeyspace(config, cassandra_manager=manager)
table = CassandraTable(config, cassandra_manager=manager)
keyspace.create().execute()
table.create().execute()

# Insert line into table.
table.insert({'device_id': '659b6222-19fb-416e-9aa5-9df8cf679247', 'ts': 1592491059984}).execute()

# Query table.
rows = \
    table \
    .query('base') \
    .id('659b6222-19fb-416e-9aa5-9df8cf679247') \
    .execute()
n_rows = len(rows)
print(f"Queried {len(rows)} rows")

License

This project is licensed under the terms of the Apache License 2.0 license.