This guide will teach you the basics of using the SurrealDB SDK for Java to interact with a SurrealDB instance. We will cover topics such as installing the SDK, connecting to an in-memory database, and performing basic queries. For demonstration purposes, the presented queries follow the example of a library database.
ImportantYou can find a working example project implementing the queries described in the guide in our surrealdb-examples repository
This guide assumes the following:
The SurrealDB Java SDK is available on the Maven Central repository. You can install it using Gradle or Maven. Copy the relevant code snippet into your build.gradle or pom.xml file to add the SDK as a dependency.
The first step towards sending queries is instantiating the driver. In the following example we use a try-with-resources block to ensure the driver is properly closed after use. After instantiating the driver, we will connect to an in-memory database, and select our namespace and database.
In order to represent database records within your application, you need to define POJO classes that match the tables in your database. Additionally, model classes require a public no-argument constructor to be used with the SDK.
For this example, we will create a Book class representing book records in our database.
Model classes are extremely useful as they can be used to represent more than just tables, such as complex query responses, relations, and more. Additionally, the fields in model classes can contain nested objects, arrays, and other SurrealDB data types.
You can learn more about supported SurrealDB data types on the data types page.
The first step towards querying data is to populate the database with records. This can be achieved using the create method, which allows you to pass model class instances to create new records.
In the following example, we instantiate a new Book object and pass it to the create method. This method will return a list of created Book records, each with a unique record id.
Since create allows us to create multiple records at once, it returns a list of books. In this case, we only created one book, so we use .get(0) to retrieve it.
You might also want to create records with a specific ID. In this case, you can pass the RecordId as the second argument to the create method. This approach will return the Book instance directly instead of a list.
// Create a book record with a custom id Book created = driver.create(Book.class, new RecordId("book", "aeon"), book);
Now we have records in our database, we can query them using the select method. This method allows you to retrieve either all records from a table, or select a single record by its ID.
While the select method is useful for retrieving all records or a single record by ID, you may want to perform filtering, ordering, and other operations on your select queries. In these situations you can use the query method to execute custom SurrealQL queries. We will cover this in the Complex queries section.
Records can be updated using the update and upsert methods. These methods allow you to update all records of a table, or a single record by its ID.
In the following example we update the availability status of a book record. We pass the id of the record we want to update, the type of update we want to perform, and the updated record. The UpType.MERGE parameter tells the SDK to merge the updated record with the existing record, keeping any fields that are not present in the updated record.
After the update is applied, a new Book instance is returned with the updated fields.
You can pass a table name instead of a record ID to update all records in a table. This will return an iterator of updated records.
As you can see, we can instantiate an empty Book object and set only the fields we want to update. This allows us to conveniently update only specific fields.
Existing records can be deleted using the delete method. You can either pass a table name to delete all records in a table, or a record ID to delete a specific record.
The Java SDK makes it easy to define relations between records using the relate method. This method takes two RecordId instances, an edge name, and an optional value model.
This next example demonstrates how to relate a book to a publisher.
Much like other methods, you can also pass a class reference as a first argument to control the return and value type. This class must be a subclass of Relation, or simply Relation itself. This class holds the id, in, and out of a relation.
While the previously mentioned methods are useful for basic operations, you may want to perform more complex queries. This can be achieved using the query method, which allows you to execute custom SurrealQL queries.
You can pass a query string to the query method and it will return a Response object containing the query results.
You can optionally pass parameters to the query by using the queryBind method, which takes a map of values to bind to the query.
While we can represent object data using model classes, we can also use the Value class to represent SurrealDB data types directly.
In all previous examples we can omit the model class reference arguments to make methods return a Value instance instead of a model class instance. This class provides useful functions to check and convert data types, as well as access nested objects and arrays.
Now that you have learned the basics of the SurrealDB SDK for Java, you can learn more about the API in the API documentation or explore the data types supported by the SDK.