NHyDBrun4
R
u
v
Version of 1
lane
NLOverview

First steps

NHyDB and dNHyDB are Python packages with a native core in Rust. Prebuilt packages are available for Linux, macOS and Windows – no Rust compiler is needed.

Install

python -m pip install nhydb
python -m pip install numpy        # optional, for the NumPy interface

For dNHyDB accordingly pip install dnhydb.

Your first NHyDB database

Create two cities, close the database, open it again and read – once directly by address, once with SQL:

import nhydb

# Create: realm 1, a record shape with two typed fields, a CLASSIC layout
with nhydb.create("cities.nhydb") as db:
    db.create_realm(1, "main")
    db.create_shape(1, 1, "city", [{"v": 1, "name": "name", "type": "utf8"},
                                   {"v": 2, "name": "population", "type": "varuint"}])
    db.create_classic_layout(1, 1, "cities", shape_id=1)
    db.put_dense_batch(1, 1, [["Berlin", 3_685_000], ["Hamburg", 1_862_000]], shape_id=1)
# Leaving the block commits and closes the database.

with nhydb.open("cities.nhydb") as db:
    print(db.get(1, 2))
    print(db.sql("SELECT name FROM cities WHERE population > 2000000")["rows"])

Output:

{'_realm': 1, '_u': 2, '_lane': 0, '_lane_name': 'NL', '_shape': 1, 'name': 'Hamburg', 'population': 1862000}
[{'name': 'Berlin'}]

db.get(1, 2) reads the record at realm 1, u = 2 – Hamburg, the second record. Its address exists before anything is searched.

Three files belong together

Next to cities.nhydb, NHyDB creates the encrypted layout plan (.layout.enc) and its key (.layout.key). These three files belong together: back them up together, move them together – and protect the key well; without it the database will not open.

Your first dNHyDB database

dNHyDB is the lean sister: one realm, one file. Two records and a free cell:

import dnhydb

# Create: a typed BASE record layout, two records and one free cell
with dnhydb.create("cities.dnhydb") as db:
    db.define_base([("city", "utf8"), ("population", "u64")])
    db.put_record(1, ["Berlin", 3_685_000])
    db.put_record(2, ["Hamburg", 1_862_000])
    db.put_cell(2, 1_000, "port city")
# Leaving the block commits and closes the database.

db = dnhydb.open_reader("cities.dnhydb")
print(db.get_record(2))
print(db.get_cell(2, 1_000))
n = dnhydb.n_of(2, 1)
print(n, dnhydb.address_of(n))
db.close()

Output:

{'city': 'Hamburg', 'population': 1862000, 'v1000': 'port city'}
port city
7 (2, 1, 0)

Using it from Rust

The core of NHyDB is a pure Rust library (nhydb-core) and can also be embedded directly in Rust programs without Python – which is exactly what NhyFlow does. There is no package on crates.io for this yet; you include the source code from the GitHub repository. So far, the documented interface is the Python interface.

Would you like to use NHyDB the way NhyFlow does – as a pure Rust database in your own program? Then feel free to email achim.meissner@nhydb.de, and we can talk about your project.

Where to go next

# First steps NHyDB and dNHyDB are Python packages with a native core in Rust. Prebuilt packages are available for Linux, macOS and Windows – no Rust compiler is needed. ## Install ```bashpython -m pip install nhydbpython -m pip install numpy # optional, for the NumPy interface``` For dNHyDB accordingly `pip install dnhydb`. ## Your first NHyDB database Create two cities, close the database, open it again and read – once directly by address, once with SQL: ```pythonimport nhydb # Create: realm 1, a record shape with two typed fields, a CLASSIC layoutwith nhydb.create("cities.nhydb") as db: db.create_realm(1, "main") db.create_shape(1, 1, "city", [{"v": 1, "name": "name", "type": "utf8"}, {"v": 2, "name": "population", "type": "varuint"}]) db.create_classic_layout(1, 1, "cities", shape_id=1) db.put_dense_batch(1, 1, [["Berlin", 3_685_000], ["Hamburg", 1_862_000]], shape_id=1)# Leaving the block commits and closes the database. with nhydb.open("cities.nhydb") as db: print(db.get(1, 2)) print(db.sql("SELECT name FROM cities WHERE population > 2000000")["rows"])``` Output: ```text{'_realm': 1, '_u': 2, '_lane': 0, '_lane_name': 'NL', '_shape': 1, 'name': 'Hamburg', 'population': 1862000}[{'name': 'Berlin'}]``` `db.get(1, 2)` reads the record at realm 1, u = 2 – Hamburg, the second record. Its address exists before anything is searched. ## Three files belong together Next to `cities.nhydb`, NHyDB creates the encrypted layout plan (`.layout.enc`) and its key (`.layout.key`). These three files belong together: back them up together, move them together – and protect the key well; without it the database will not open. ## Your first dNHyDB database dNHyDB is the lean sister: one realm, one file. Two records and a free cell: ```pythonimport dnhydb # Create: a typed BASE record layout, two records and one free cellwith dnhydb.create("cities.dnhydb") as db: db.define_base([("city", "utf8"), ("population", "u64")]) db.put_record(1, ["Berlin", 3_685_000]) db.put_record(2, ["Hamburg", 1_862_000]) db.put_cell(2, 1_000, "port city")# Leaving the block commits and closes the database. db = dnhydb.open_reader("cities.dnhydb")print(db.get_record(2))print(db.get_cell(2, 1_000))n = dnhydb.n_of(2, 1)print(n, dnhydb.address_of(n))db.close()``` Output: ```text{'city': 'Hamburg', 'population': 1862000, 'v1000': 'port city'}port city7 (2, 1, 0)``` ## Using it from Rust The core of NHyDB is a pure Rust library (`nhydb-core`) and can also be embedded directly in Rust programs without Python – which is exactly what NhyFlow does. There is no package on crates.io for this yet; you include the source code from the GitHub repository. So far, the documented interface is the Python interface. Would you like to use NHyDB the way NhyFlow does – as a pure Rust database in your own program? Then feel free to email [achim.meissner@nhydb.de](mailto:achim.meissner@nhydb.de), and we can talk about your project. ## Where to go next - The [basic concepts](/en/basic-concepts/) explain address, realm, layout and commit in plain words.- The [Python Cookbook](https://github.com/nhydb-run4/NHyDB/blob/main/docs/cookbook/PYTHON_COOKBOOK.md) covers the complete interface with tested examples.- [22 Jupyter notebooks in English](https://github.com/nhydb-run4/NHyDB/blob/main/notebooks/en/README_v1.md) to try it out, step by step.