Current section

Files

Jump to
arcadic documentation tutorials getting_started.livemd
Raw

documentation/tutorials/getting_started.livemd

# Getting started with Arcadic

<!--
SPDX-FileCopyrightText: 2026 BaseLabs Holdings LLC

SPDX-License-Identifier: MIT
-->

Use this tutorial to connect an Elixir application to ArcadeDB, write and
read a record, and run a transaction. It also walks through HTTP and Bolt
streaming, a schema migration, and database administration.

[Open in Livebook](https://livebook.dev/run?url=https%3A%2F%2Fraw.githubusercontent.com%2Fbaselabs%2Farcadic%2Fmain%2Fdocumentation%2Ftutorials%2Fgetting_started.livemd)

## Setup

Start an ArcadeDB instance with HTTP and Bolt:

```
docker run -d --name arcadic-quickstart \
  -p 2480:2480 -p 7687:7687 \
  -e JAVA_OPTS="-Darcadedb.server.rootPassword=playwithdata \
                -Darcadedb.server.plugins=Bolt:com.arcadedb.bolt.BoltProtocolPlugin" \
  arcadedata/arcadedb:26.11.1
```

Two ports are published: **2480** (the HTTP command API, the default transport)
and **7687** (Bolt, used by the streaming section below). The
`-Darcadedb.server.plugins=Bolt:...` option enables ArcadeDB's Bolt protocol,
which is off by default; without it the streaming section can't connect.

> **Heads-up on port 7687.** That is also Neo4j's default Bolt port. If you run a
> local Neo4j, it likely already owns 7687 and the streaming section would end up
> talking to *it* instead of ArcadeDB, giving a confusing handshake or auth error.
> Map ArcadeDB's Bolt to a free host port instead (e.g. `-p 7690:7687`) and set
> `ARCADIC_BOLT_PORT=7690` before launching Livebook.

> **Re-running cells.** Setup creates the database only if it is absent.
> The data cells use `MERGE` so you can run them again. To start fresh,
> run **Cleanup** and then run all cells from top to bottom.

If you use a different server or credentials, set `ARCADIC_HTTP_URL`,
`ARCADIC_BOLT_HOST`, `ARCADIC_BOLT_PORT`, or `ARCADIC_PASSWORD` before
launching Livebook.

## Install

```elixir
Mix.install([
  {:arcadic, "~> 3.0"},
  {:db_connection, ">= 2.10.3 and < 3.0.0-0"},
  {:kino, "~> 0.14"}
])
```

## Connect

`Arcadic.connect/3` builds a connection handle. This cell creates a
disposable `arcadic_quickstart` database if it is absent.

```elixir
http_url = System.get_env("ARCADIC_HTTP_URL", "http://localhost:2480")
password = System.get_env("ARCADIC_PASSWORD", "playwithdata")
database = "arcadic_quickstart"

admin = Arcadic.connect(http_url, database, auth: {"root", password})
{:ok, true} = Arcadic.Server.ready?(admin)

unless match?({:ok, true}, Arcadic.Server.database_exists?(admin, database)) do
  :ok = Arcadic.Server.create_database(admin, database)
end

conn = Arcadic.connect(http_url, database, auth: {"root", password})
```

## Query and command (parameters only)

Bind dynamic values with `$name` placeholders in Cypher. Use `command/4`
for writes and `query/4` for reads. Here `MERGE` matches existing records
when you run the cell again.

```elixir
{:ok, _} =
  Arcadic.command(
    conn,
    "MERGE (p:Person {name: $name}) ON CREATE SET p.role = $role RETURN p.name AS name",
    %{"name" => "Alice", "role" => "engineer"}
  )

{:ok, rows} = Arcadic.query(conn, "MATCH (p:Person) RETURN p.name AS name, p.role AS role", %{})
rows
```

## Transactions

`Arcadic.transaction/3` opens an ArcadeDB session, runs the function with a
session-scoped connection, and commits on normal return. An exception
rolls back and reraises. `Arcadic.rollback/2` aborts and yields
`{:error, reason}`.

```elixir
{:ok, total} =
  Arcadic.transaction(conn, fn tx ->
    Arcadic.command!(tx, "MERGE (p:Person {name: $name})", %{"name" => "Bob"})
    [%{"c" => c}] = Arcadic.query!(tx, "MATCH (p:Person) RETURN count(p) AS c", %{})
    c
  end)

total
```

```elixir
# An intentional rollback discards everything the transaction did.
result =
  Arcadic.transaction(conn, fn tx ->
    Arcadic.command!(tx, "CREATE (p:Person {name: $name})", %{"name" => "Ghost"})
    Arcadic.rollback(tx, :changed_my_mind)
  end)

{:ok, [%{"c" => ghosts}]} =
  Arcadic.query(conn, "MATCH (p:Person {name: $name}) RETURN count(p) AS c", %{"name" => "Ghost"})

{result, ghosts}
```

## Other query languages

Cypher is the default. Pass `language: "sql"` to run this SQL statement.

```elixir
Arcadic.query!(conn, "SELECT name, role FROM Person ORDER BY name", %{}, language: "sql")
```

## Inspecting the plan (EXPLAIN)

`Arcadic.explain!/4` prepends `EXPLAIN` and returns the query plan **without running
the statement**, handy for checking how ArcadeDB will resolve a query (by type scan,
bucket, or index) before you run it for real. It returns a `%{plan, plan_tree, rows}`
map; `.plan` is the human-readable summary (`plan_tree` holds the raw, transport-defined
tree and `rows` is empty for EXPLAIN).

> `Arcadic.profile!/4` executes the statement to measure its runtime.
> Profiling a write changes data.

```elixir
Arcadic.explain!(conn, "SELECT FROM Person", %{}, language: "sql").plan
```

## Streaming large results over HTTP

`Arcadic.query_stream/4` returns a lazy stream. This SQL statement uses
`@rid` keyset paging over HTTP. The example requests up to five rows per
page. See [Stream query results](../how-to/stream-results.md) for paging rules.

```elixir
{:ok, page_stream} =
  Arcadic.query_stream(conn, "SELECT FROM Person", %{}, language: "sql", chunk_size: 5)

page_stream |> Enum.to_list() |> Kino.DataTable.new()
```

## Streaming large results over Bolt

For large result sets, `Arcadic.query_stream/4` returns a lazy `Stream` of rows
over Bolt, paged with `PULL` so the whole result never has to sit in memory at
once. `Arcadic.Transport.Bolt.setup/1` builds the transport options in one call.

```elixir
bolt_host = System.get_env("ARCADIC_BOLT_HOST", "localhost")
bolt_port = String.to_integer(System.get_env("ARCADIC_BOLT_PORT", "7687"))

{:ok, transport_options} =
  Arcadic.Transport.Bolt.setup(
    hostname: bolt_host,
    port: bolt_port,
    username: "root",
    password: password
  )

bolt_conn =
  Arcadic.connect(http_url, database,
    auth: {"root", password},
    transport: Arcadic.Transport.Bolt,
    transport_options: transport_options
  )

{:ok, stream} =
  Arcadic.query_stream(bolt_conn, "UNWIND range(1, 25) AS i RETURN i AS n, i * i AS square", %{},
    chunk_size: 5
  )

stream |> Enum.to_list() |> Kino.DataTable.new()
```

For encrypted Bolt, see [Connections and transports](../topics/client/connections.md#bolt).

## Migrations

Define and apply a migration to create the `Widget` type.
`Arcadic.Migrator` records the applied version and skips it on the next
run. See [Run and recover migrations](../how-to/migrate-schema.md) for the
recovery procedure.

```elixir
defmodule Quickstart.Migrations.V1 do
  @behaviour Arcadic.Migration
  @impl true
  def version, do: 1
  @impl true
  def up(c) do
    Arcadic.command!(c, "CREATE VERTEX TYPE Widget IF NOT EXISTS", %{}, language: "sql")
    :ok
  end
  @impl true
  def down(c), do: Arcadic.command!(c, "DROP TYPE Widget IF EXISTS", %{}, language: "sql") && :ok
end

defmodule Quickstart.Migrations do
  use Arcadic.MigrationRegistry
  migrations([Quickstart.Migrations.V1])
end

{:ok, applied} = Arcadic.Migrator.migrate(conn, Quickstart.Migrations)
{:ok, status} = Arcadic.Migrator.status(conn, Quickstart.Migrations)
{applied, status}
```

## Schema introspection

`Arcadic.Schema` reads the database catalog, which now includes `Widget`.

```elixir
Arcadic.Schema.types!(conn)
```

## Server admin

`Arcadic.Server` covers server-level operations. Database identifiers are
allowlist-validated before they reach the wire.

```elixir
{:ok, databases} = Arcadic.Server.list_databases(admin)
{:ok, exists?} = Arcadic.Server.database_exists?(admin, database)
{databases, exists?}
```

## Beyond this notebook

Two more areas `Arcadic` covers that don't fit a quickstart cell:

- **`Arcadic.Vector`** builds and queries ArcadeDB's `LSM_VECTOR`-backed indexes for
  similarity search, e.g.
  `Arcadic.Vector.create_dense_index!(conn, "Person", "embedding", 384)`, then
  `Arcadic.Vector.neighbors!/5` for a k-NN lookup.
- **`Arcadic.Import`/`Arcadic.Export`** wrap ArcadeDB's bulk `IMPORT DATABASE`/`EXPORT
  DATABASE` commands, e.g.
  `Arcadic.Export.database!(conn, "backup", with: [format: "jsonl"])` writes a bare,
  allowlisted export name server-side (never a path).

## Cleanup

Drop the throwaway database when you are done (or before a fresh top-to-bottom
run).

```elixir
Arcadic.Server.drop_database(admin, database)
```