Fast, simple, and robust Cassandra driver for Elixir.
Fast, simple, and robust Cassandra driver for Elixir.
Xandra is a Cassandra driver built natively in Elixir and focused on speed, simplicity, and robustness. This driver works exclusively with the Cassandra Query Language v3 (CQL3) and the native protocol versions 3 and 4.
This library is in its early stages when it comes to features, but we're already successfully using it in production at Forza Football. Currently, the supported features are:
See the documentation for detailed explanation of how the supported features work.
Add the
:xandradependency to your
mix.exsfile:
def deps() do [{:xandra, "~> 0.11"}] end
Then, run
mix deps.getin your shell to fetch the new dependency.
The documentation is available on HexDocs.
Connections or pool of connections can be started with
Xandra.start_link/1:
{:ok, conn} = Xandra.start_link(nodes: ["127.0.0.1:9042"])
This connection can be used to perform all operations against the Cassandra server.
Executing simple queries looks like this:
statement = "INSERT INTO users (name, postcode) VALUES ('Priam', 67100)" {:ok, %Xandra.Void{}} = Xandra.execute(conn, statement, _params = [])
Preparing and executing a query:
with {:ok, prepared}Xandra supports streaming pages:
prepared = Xandra.prepare!(conn, "SELECT * FROM subscriptions WHERE topic = :topic") page_stream = Xandra.stream_pages!(conn, prepared, _params = [], page_size: 1_000)This is going to execute the prepared query every time a new page is needed:
page_stream |> Enum.take(10) |> Enum.each(fn page -> IO.puts("Got a bunch of rows: #{inspect(Enum.to_list(page))}") end)
Scylla support
Xandra supports Scylla (version
2.x) without the need to do anything in particular.Contributing
To run tests, you will need Docker installed on your machine. This repository uses
docker-composeto run multiple Cassandra instances in parallel on different ports to test different features (such as authentication or SSL encryption). To run normal tests, do this from the root of the project:docker-compose up -d mix testThe
-dflags runs the instances as daemons in the background. Give it a minute between starting the services and runningmix testsince Cassandra takes a while to start. To stop the services, rundocker-compose stop.To run tests for Scylla, you'll need a different set of services and a different test task:
docker-compose --file docker-compose.scylladb.yml up -d mix test.scylladbUse
docker-compose --file docker-compose.scylladb.yml stopto stop Scylla when done.By default, tests run for native protocol v3 except for a few specific tests that run on native protocol v4. If you want to test the whole suite on native protocol v4, use:
CASSANDRA_NATIVE_PROTOCOL=v4 mix testLicense
Xandra is released under the ISC license, see the LICENSE file.