Introduction to Oracle NoSQL and Helidon

Oracle NoSQL is a highly scalable, distributed key-value database built for fast and reliable performance, particularly suited for modern, high-demand applications. Helidon, on the other hand, is a lightweight, reactive framework for building microservices in Java. In this tutorial, we will explore how to build a RESTful application using Oracle NoSQL as the backend database and Helidon to handle the REST endpoints.

Prerequisites

Before we begin, make sure you have the following installed:

  • Java Development Kit (JDK) version 11 or later
  • Maven build tool
  • Oracle NoSQL database installed and configured

Setting Up the Project

First, let’s create a new Maven project. Open your terminal and run the following command:

bash
mvn archetype:generate -DarchetypeGroupId=io.helidon.archetypes -DarchetypeArtifactId=helidon-quickstart-se -DarchetypeVersion=2.3.1 -DinteractiveMode=false -DgroupId=com.example -DartifactId=helidon-nosql-app -Dpackage=com.example.nosqlapp

This will generate a new Helidon project with a standard directory structure.

Next, we need to add the Oracle NoSQL dependency to our project’s pom.xml file:

xml
<dependency>
<groupId>com.oracle.nosql</groupId>
<artifactId>nosqldb-cloudsim</artifactId>
<version>22.1.5</version>
</dependency>

Make sure to replace the version with the latest available version.

Configuring Oracle NoSQL

We need to configure Oracle NoSQL connection settings in our application. Create a new configuration file named application.yaml in the src/main/resources directory:

yaml
nosql:
endpoint: "https://nosql.us-ashburn-1.oci.oraclecloud.com"
region: "us-ashburn-1"
compartment: "your-compartment-id"
auth:
iam:
tenantId: "your-tenant-id"
userId: "your-user-id"
fingerprint: "your-fingerprint"
privateKeyFile: "path-to-your-private-key-file"

Replace the placeholder values with your actual Oracle Cloud Infrastructure (OCI) credentials and configuration.

Creating REST Endpoints

Now, let’s create our REST endpoints using Helidon’s WebServer module. Create a new Java class named UserResource in the src/main/java/com/example/nosqlapp directory:

java

package com.example.nosqlapp;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;

@Path(“/users”)
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class UserResource {

@GET
public List<User> getAllUsers() {
// Retrieve and return all users from the database
}

@GET
@Path(“/{id}”)
public Response getUserById(@PathParam(“id”) String id) {
// Retrieve user by ID from the database
}

@POST
public Response createUser(User user) {
// Create a new user in the database
}

@PUT
@Path(“/{id}”)
public Response updateUser(@PathParam(“id”) String id, User user) {
// Update an existing user in the database
}

@DELETE
@Path(“/{id}”)
public Response deleteUser(@PathParam(“id”) String id) {
// Delete user from the database
}
}

In the above code, we define endpoints for retrieving all users, retrieving a user by ID, creating a new user, updating an existing user, and deleting a user.

Integrating with Oracle NoSQL

Now, let’s integrate our REST endpoints with Oracle NoSQL. We’ll use the Oracle NoSQL Java SDK to interact with the database. Add the following dependency to your pom.xml file:

xml
<dependency>
<groupId>com.oracle.nosql</groupId>
<artifactId>nosqldb-driver</artifactId>
<version>4.3.9</version>
</dependency>

Next, we’ll inject an instance of NoSQLClient into our UserResource class and use it to perform CRUD operations on the database.

java
import com.oracle.nosql.driver.NoSQLHandleConfig;
import com.oracle.nosql.driver.NoSQLHandleFactory;
import com.oracle.nosql.driver.values.MapValue;
import com.oracle.nosql.driver.values.StringValue;
import java.util.List;public class UserResource {

private final NoSQLHandleConfig config = NoSQLHandleConfig.builder()
.endpoint(System.getProperty(“nosql.endpoint”))
.region(System.getProperty(“nosql.region”))
.compartment(System.getProperty(“nosql.compartment”))
.authConfig(NoSQLHandleConfig.AuthConfig.builder()
.iamConfig(NoSQLHandleConfig.AuthConfig.IAMConfig.builder()
.tenantId(System.getProperty(“nosql.auth.iam.tenantId”))
.userId(System.getProperty(“nosql.auth.iam.userId”))
.fingerprint(System.getProperty(“nosql.auth.iam.fingerprint”))
.privateKeyFile(System.getProperty(“nosql.auth.iam.privateKeyFile”))
.build())
.build())
.build();

private final NoSQLClient client = NoSQLHandleFactory.createNoSQLHandle(config);

@GET
public List<User> getAllUsers() {
// Retrieve and return all users from the database
}

// Other endpoint methods
}

In the above code, we configure the NoSQLHandleConfig with the connection properties specified in the application.yaml file and create a new instance of NoSQLClient to interact with the Oracle NoSQL database.

Implementing CRUD Operations

Now, let’s implement the methods in UserResource to perform CRUD operations using Oracle NoSQL.

java
import com.oracle.nosql.driver.ops.GetRequest;
import com.oracle.nosql.driver.ops.GetResult;
import com.oracle.nosql.driver.ops.PutRequest;
import com.oracle.nosql.driver.ops.PutResult;
import com.oracle.nosql.driver.ops.DeleteRequest;
import com.oracle.nosql.driver.ops.DeleteResult;
import java.util.List;public class UserResource {

// Constructor and injection of NoSQLClient

@GET
public List<User> getAllUsers() {
// Retrieve and return all users from the database
return client.table(“users”)
.read()
.all()
.execute()
.asPojo(User.class);
}

@GET
@Path(“/{id}”)
public Response getUserById(@PathParam(“id”) String id) {
GetRequest getRequest = GetRequest.builder()
.tableName(“users”)
.key(MapValue.of(“id”, StringValue.fromString(id)))
.build();

GetResult getResult = client.get(getRequest);
if (getResult.isSuccessful()) {
User user = getResult.getValue().asPojo(User.class);
return Response.ok(user).build();
} else {
return Response.status(Response.Status.NOT_FOUND).build();
}
}

@POST
public Response createUser(User user) {
PutRequest putRequest = PutRequest.builder()
.tableName(“users”)
.value(MapValue.ofPojo(user))
.build();

PutResult putResult = client.put(putRequest);
if (putResult.isSuccessful()) {
return Response.status(Response.Status.CREATED).build();
} else {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
}

@PUT
@Path(“/{id}”)
public Response updateUser(@PathParam(“id”) String id, User user) {
// Update an existing user in the database
}

@DELETE
@Path(“/{id}”)
public Response deleteUser(@PathParam(“id”) String id) {
// Delete user from the database
}
}

Conclusion

In this article, we’ve explored how to build a RESTful application using Oracle NoSQL Database and Helidon. We started by setting up our development environment, creating a Helidon project, adding Oracle NoSQL dependency, and implementing REST endpoints to perform CRUD operations on data stored in Oracle NoSQL Database. With the combination of these technologies, developers can build high-performance and scalable applications with ease.

This is just the beginning, and there’s much more to explore in terms of advanced features, security, and performance tuning. However, with the foundation laid out in this article, you should now have a good understanding of how to integrate Oracle NoSQL Database with Helidon to build modern Java applications.