NHyDBrun4
R
u
v
Version of 1
lane
NLOverview

Python and NumPy

NHyDB is a Python package with a native core in Rust. For everyday work there are single operations, for large amounts of data a NumPy interface that passes whole columns in one call.

Single operations

One record, one cell, one query per call: put, get, put_cell, get_value, sql and so on. With with, the database is committed and closed at the end. Examples are on the pages First steps and Records and cells.

Whole columns with NumPy

For many records you pass one NumPy array per field. Checking, converting and writing run in the Rust core, without a Python loop. In the same way NHyDB reads a range of records back as one array per field.

An example

Write 100,000 measurements in one call, read a section and fetch a query result as an array:

import numpy as np
import nhydb

with nhydb.create("measurements.nhydb") as db:
    db.create_realm(1, "measurements")
    db.create_shape(1, 1, "measurement", [{"v": 1, "name": "minute", "type": "u32"},
                                          {"v": 2, "name": "temp", "type": "f64"}])
    db.create_classic_layout(1, 1, "measurements", shape_id=1)

    n = 100_000
    minute = np.arange(n, dtype=np.uint32)
    temp = 20 + np.sin(minute / 60.0)
    db.put_numpy_batch(1, 1, {"minute": minute, "temp": temp}, commit=True)   # u = 1 …

    part = db.read_numpy(1, 99_999, 3, ["minute", "temp"])        # u = 99999 … 100001
    print(part["minute"], part["_present"])
    r = db.sql_numpy("SELECT minute FROM measurements WHERE temp > ? "
                     "ORDER BY minute LIMIT 3", [20.99])
    print(r["minute"])

Output:

[99998 99999     0] [ True  True False]
[86 87 88]

read_numpy reads three records starting at u = 99,999. The third one does not exist: _present is False there, and the value 0 is only a placeholder.

Query results as arrays

NHySQL and QueryEngine results can be fetched directly as NumPy arrays, one column per field. This way, analyses pass straight to NumPy and the tools built on it.

Threads and processes

Exactly one object writes to each database; other processes or threads read with their own read-only objects. Long calls such as queries, batches and commits release the Python lock (GIL) while working in the Rust core, so other threads keep running.

Single operations for everyday work, whole columns for large amounts of data.

# Python and NumPy NHyDB is a Python package with a native core in Rust. For everyday work there are single operations, for large amounts of data a NumPy interface that passes whole columns in one call. ## Single operations One record, one cell, one query per call: `put`, `get`, `put_cell`, `get_value`, `sql` and so on. With `with`, the database is committed and closed at the end. Examples are on the pages [First steps](/en/getting-started/) and [Records and cells](/en/records-and-cells/). ## Whole columns with NumPy For many records you pass one NumPy array per field. Checking, converting and writing run in the Rust core, without a Python loop. In the same way NHyDB reads a range of records back as one array per field. ## An example Write 100,000 measurements in one call, read a section and fetch a query result as an array: ```pythonimport numpy as npimport nhydb with nhydb.create("measurements.nhydb") as db: db.create_realm(1, "measurements") db.create_shape(1, 1, "measurement", [{"v": 1, "name": "minute", "type": "u32"}, {"v": 2, "name": "temp", "type": "f64"}]) db.create_classic_layout(1, 1, "measurements", shape_id=1) n = 100_000 minute = np.arange(n, dtype=np.uint32) temp = 20 + np.sin(minute / 60.0) db.put_numpy_batch(1, 1, {"minute": minute, "temp": temp}, commit=True) # u = 1 … part = db.read_numpy(1, 99_999, 3, ["minute", "temp"]) # u = 99999 … 100001 print(part["minute"], part["_present"]) r = db.sql_numpy("SELECT minute FROM measurements WHERE temp > ? " "ORDER BY minute LIMIT 3", [20.99]) print(r["minute"])``` Output: ```text[99998 99999 0] [ True True False][86 87 88]``` `read_numpy` reads three records starting at u = 99,999. The third one does not exist: `_present` is `False` there, and the value 0 is only a placeholder. ## Query results as arrays NHySQL and QueryEngine results can be fetched directly as NumPy arrays, one column per field. This way, analyses pass straight to NumPy and the tools built on it. ## Threads and processes Exactly one object writes to each database; other processes or threads read with their own read-only objects. Long calls such as queries, batches and commits release the Python lock (GIL) while working in the Rust core, so other threads keep running. > Single operations for everyday work, whole columns for large amounts of data.