The Oracle NoSQL SDK for Java provides interfaces, documentation, and examples to help develop Java applications that connect to the Oracle NoSQL Database Cloud Service, Oracle NoSQL Database or to the Oracle NoSQL Cloud Simulator (which runs on a local machine). In order to run the Oracle NoSQL Cloud Simulator, a separate download is necessary from the Oracle NoSQL OTN download page. The Oracle NoSQL Database Cloud Service and Cloud Simulator are referred to as the "cloud service" while the Oracle NoSQL Database is referred to as "on-premise."
The API for all environments is the same, with the exception of some environment-specific classes and methods, mostly related to authentication and authorization. The API documentation clearly notes environment-specific information.
Java versions 8 and higher are supported.
The Oracle NoSQL SDK for Java can be included in a project in 2 ways:
This dependency can be used to include the SDK and its dependencies in your project. The version changes with each release.
<dependency>
<groupId>com.oracle.nosql.sdk</groupId>
<artifactId>nosqldriver</artifactId>
<version>5.4.15</version>
</dependency>
You can download the Oracle NoSQL SDK for Java as an archive from GitHub. The archive contains the runtime library and its dependencies, examples, and API documentation.
See Oracle NoSQL SDK for Java javadoc for the latest API documentation.
General documentation about the Oracle NoSQL Database and the Oracle NoSQL Database Cloud Service can be found in these locations:
See CHANGELOG for changes in each release.
There are 3 environments, or services that can be used by the Oracle NoSQL SDK for Java:
The next sections describe how to connect to each and what information is required.
There are 3 ways to authorize an application using the Oracle NoSQL Database Cloud Service:
You will need an Oracle Cloud account and credentials to use this SDK. With this information, you'll set up a client configuration to tell your application how to find the cloud service, and how to properly authenticate. See Authentication to connect to Oracle NoSQL Database for details of credentials you will need to configure an application. This only needs to be done once for any user.
You should have the following information in hand:
You can supply your user credentials in 2 ways:
See the Quickstart example below for details on using a User Principal
Instance Principal is an IAM service feature that enables instances to be authorized actors (or principals) to perform actions on service resources. Each compute instance has its own identity, and it authenticates using the certificates that are added to it. See Calling Services from an instance for prerequisite steps to set up Instance Principal.
See the Quickstart example below for code details for using an Instance Principal.
Resource Principal is an IAM service feature that enables the resources to be authorized actors (or principals) to perform actions on service resources. You may use Resource Principal when calling Oracle NoSQL Database Cloud Service from other Oracle Cloud service resource such as Functions. See Accessing Other Oracle Cloud Infrastructure Resources from Running Functions for how to set up Resource Principal.
See the Quickstart example below for code details for using a Resource Principal.
The on-premises configuration requires a running instance of Oracle NoSQL Database. In addition a running proxy service is required. See Oracle NoSQL Database Downloads for downloads, and see Information about the proxy for proxy configuration information.
On-premise authorization requires use of StoreAccessTokenProvider See the Quickstart example below for code details for connecting on-premise.
When you develop an application, you may wish to start with Oracle NoSQL Database Cloud Simulator.
The Cloud Simulator simulates the cloud service and lets you write and test applications locally without accessing the Oracle NoSQL Database Cloud Service. You may run the Cloud Simulator on localhost.
See the Quickstart example below for code details for connecting to the Cloud Simulator. Authorization for the Cloud Simulator is a simple no-op class implemented directly in the Quickstart example.
Addional logging can be enabled using a java properties file. For full details, see "Logging in the SDK" at https://oracle.github.io/nosql-java-sdk/oracle/nosql/driver/package-summary.html
The following is a quick start tutorial to run a simple program in all supported environments. It requires access to the Oracle NoSQL Database Cloud Service, a running on-premises Oracle NoSQL Database instance, or a running Oracle NoSQL Cloud Simulator instance. As a standalone program it will run most easily using a download version of the Oracle NoSQL SDK for Java.
$ javac -cp <path-to-nosqldriver.jar> Quickstart.java
$ java -cp .:<path-to-nosqldriver.jar> Quickstart -service cloud -endpoint us-ashburn-1
Using a non-secure on-premises service on endpoint http://localhost:8090
$ java -cp .:<path-to-nosqldriver.jar> Quickstart -service onprem -endpoint http://localhost:8090
Using a Cloud Simulator instance on endpoint http://localhost:8080
$ java -cp .:<path-to-nosqldriver.jar> Quickstart -service cloudsim -endpoint http://localhost:8080
There is code in the example for using Instance Principal and Resource Principal authorization for the cloud service but it is not enabled via a command line option. There is an additional, optional argument to the command line that allows specification of a compartment to use, where the compartment is an OCID. This is required if using Instance Principal or Resource Principal authorization.
/*-
* Copyright (c) 2019, 2024 Oracle and/or its affiliates. All rights reserved.
*
* Licensed under the Universal Permissive License v 1.0 as shown at
* https://oss.oracle.com/licenses/upl/
*/
import java.io.IOException;
import java.util.ArrayList;
import oracle.nosql.driver.AuthorizationProvider;
import oracle.nosql.driver.NoSQLHandle;
import oracle.nosql.driver.NoSQLHandleConfig;
import oracle.nosql.driver.NoSQLHandleFactory;
import oracle.nosql.driver.iam.SignatureProvider;
import oracle.nosql.driver.kv.StoreAccessTokenProvider;
import oracle.nosql.driver.ops.GetRequest;
import oracle.nosql.driver.ops.GetResult;
import oracle.nosql.driver.ops.QueryRequest;
import oracle.nosql.driver.ops.QueryResult;
import oracle.nosql.driver.ops.PutRequest;
import oracle.nosql.driver.ops.PutResult;
import oracle.nosql.driver.ops.QueryIterableResult;
import oracle.nosql.driver.ops.Request;
import oracle.nosql.driver.ops.TableLimits;
import oracle.nosql.driver.ops.TableRequest;
import oracle.nosql.driver.values.MapValue;
/**
* A simple quickstart program to demonstrate Oracle NoSQL Database.
* It does these things:
* - create a table
* - put a row
* - get a row
* - run a query using iterable/iterator
* - run a query using partial results
* - drop the table
*
* See the examples for more interesting operations. This quickstart is
* intended to illustrate connecting to a service and performing a few
* operations.
*
* This program can be run against:
* 1. the cloud service
* 2. the on-premises proxy and Oracle NoSQL Database instance, secure or
* not secure.
* 3. the cloud simulator (CloudSim)
*
* To run:
* java -cp .:../lib/nosqldriver.jar Quickstart \
* -service <cloud|onprem|cloudsim> -endpoint <endpoint-or-region> \
* [-compartment <ocid>]
*
* The endpoint and arguments vary with the environment.
*
* This quick start does not directly support a secure on-premise
* environment. See the examples for details on that environment.
*/
public class Quickstart {
private String endpoint;
private String service;
/* required for Instance/Resource Principal auth and OKE workload identity */
private String compartment = null; // an OCID
/* alternative cloud authorization mechanisms */
private final static boolean useUserPrincipal = true;
private final static boolean useInstancePrincipal = false;
private final static boolean useResourcePrincipal = false;
private final static boolean useSessionToken = false;
private final static boolean useOkeWorkloadIdentity = false;
private Quickstart(String[] args) {
/*
* parse arguments
*/
for (int i = 0; i < args.length; i++) {
if (args[i].equals("-service")) {
service = args[++i];
} else if (args[i].equals("-endpoint")) {
endpoint = args[++i];
} else if (args[i].equals("-compartment")) {
compartment = args[++i];
} else {
System.err.println("Unknown argument: " + args[i]);
usage();
}
}
if (service == null || endpoint == null) {
System.err.println("-service and -endpoint are required");
usage();
}
}
private static void usage() {
System.err.println(
"Usage: java -cp <path-to-nosqldriver.jar> Quickstart \\ \n" +
" -service <cloud|onprem|cloudsim> -endpoint <endpoint-or-region>" +
" \\ \n[-compartment <ocid>]");
System.exit(1);
}
private NoSQLHandle getHandle() {
NoSQLHandleConfig config = new NoSQLHandleConfig(endpoint);
if (compartment != null) {
config.setDefaultCompartment(compartment);
}
/*
* By default the handle will log to the console at level INFO.
* The default logger can be configured using a logging properties
* file specified on the command line, e.g.:
* -Djava.util.logging.config.file=logging.properties
* If a user-provided Logger is desired, create and set it here:
* Logger logger = Logger.getLogger("...");
* config.setLogger(logger);
* NOTE: the referenced classes must be imported above
*/
config.setRequestTimeout(5000);
configureAuth(config);
NoSQLHandle handle = NoSQLHandleFactory.createNoSQLHandle(config);
System.out.println("Acquired handle for service " + service +
" at endpoint " + endpoint);
return handle;
}
/*
* This method contains all service-specific code in this program
*/
private void configureAuth(NoSQLHandleConfig config) {
if (service.equals("cloud")) {
try {
SignatureProvider authProvider = null;
if (useUserPrincipal) {
/*
* Use User Principal authorization using a config
* file in $HOME/.oci/config
*/
authProvider = new SignatureProvider();
/*
* Credentials can be provided directly by editing the
* appropriate information into the parameters below
authProvider = new SignatureProvider(tenantId, // OCID
userId, // OCID
fingerprint, // String
privateKeyFile, // File
passphrase); // char[]
*/
} else if (useSessionToken) {
/*
* There are additional constructors for Session Token,
* see the javadoc
*/
authProvider = SignatureProvider.createWithSessionToken();
} else {
if (compartment == null) {
throw new IllegalArgumentException(
"Compartment is required for Instance/Resource " +
"Principal authorization");
}
/*
* There are additional constructors for both Instance and
* Resource Principal, see the javadoc
*/
if (useInstancePrincipal) {
authProvider =
SignatureProvider.createWithInstancePrincipal();
} else if (useResourcePrincipal) {
authProvider =
SignatureProvider.createWithResourcePrincipal();
} else if (useOkeWorkloadIdentity) {
authProvider =
SignatureProvider.createWithOkeWorkloadIdentity();
} else {
throw new IllegalArgumentException(
"Authorization method is required");
}
}
config.setAuthorizationProvider(authProvider);
} catch (IOException ioe) {
System.err.println("Unable to configure authentication: " +
ioe);
System.exit(1);
}
} else if (service.equals("onprem")) {
config.setAuthorizationProvider(new StoreAccessTokenProvider());
} else if (service.equals("cloudsim")) {
/* cloud simulator */
config.setAuthorizationProvider(new AuthorizationProvider() {
@Override
public String getAuthorizationString(Request request) {
return "Bearer cloudsim";
}
@Override
public void close() {
}
});
} else {
System.err.println("Unknown service: " + service);
usage();
}
}
public static void main(String[] args) {
final String tableName = "JavaQuickstart";
/*
* The Quickstart instance configures and acquires a handle
*/
Quickstart qs = new Quickstart(args);
/*
* Configure and get a NoSQLHandle. All service specific configuration
* is handled here
*/
try (NoSQLHandle handle = qs.getHandle()) {
/*
* Create a simple table with an integer key, string name and
* JSON data
*/
final String createTableStatement =
"create table if not exists " + tableName +
"(id integer, name string, data json, primary key(id))";
TableRequest tableRequest = new TableRequest()
.setStatement(createTableStatement)
.setTableLimits(new TableLimits(10, 10, 10));
/* this call will succeed or throw an exception */
handle.doTableRequest(tableRequest,
20000, /* wait up to 20 sec */
1000); /* poll once per second */
System.out.println("Created table " + tableName + " ...");
/*
* Construct a row to put
*/
MapValue value = new MapValue()
.put("id", 123)
.put("name", "joe")
.putFromJson("data", "{\"a\": 1, \"b\": 2}", null);
PutRequest putRequest = new PutRequest()
.setValue(value)
.setTableName(tableName);
PutResult putRes = handle.put(putRequest);
System.out.println("Put row, result " + putRes);
/*
* Get a row using the primary key
*/
MapValue key = new MapValue().put("id", 123);
GetRequest getRequest = new GetRequest()
.setKey(key)
.setTableName(tableName);
GetResult getRes = handle.get(getRequest);
System.out.println("Got row, result " + getRes);
/*
* Perform a query using iterable and iterator
*
* To ensure the query resources are closed properly, use
* try-with-resources statement.
*/
try (
QueryRequest queryRequest = new QueryRequest()
.setStatement("select * from " + tableName);
QueryIterableResult results =
handle.queryIterable(queryRequest)) {
System.out.println("Query results:");
for (MapValue res : results) {
System.out.println("\t" + res);
}
}
/*
* Perform a query using partial results
*/
try (
QueryRequest queryRequest = new QueryRequest()
.setStatement("select * from " + tableName) ) {
/*
* Because a query can return partial results execution must occur
* in a loop, accumulating or processing results
*/
ArrayList<MapValue> results = new ArrayList<MapValue>();
do {
QueryResult queryResult = handle.query(queryRequest);
results.addAll(queryResult.getResults());
} while (!queryRequest.isDone());
System.out.println("Query results again:");
for (MapValue res : results) {
System.out.println("\t" + res);
}
}
/*
* Drop the table
*/
tableRequest = new TableRequest()
.setStatement("drop table if exists " + tableName);
handle.doTableRequest(tableRequest,
20000,
1000);
System.out.println("Dropped table " + tableName + ", done...");
}
}
}
Several example programs are provided in the examples directory to illustrate the API. They can be found in the release download from GitHub or directly in GitHub NoSQL Examples. These examples can be run against the Oracle NoSQL Database, the NoSQL Database Cloud Service or an instance of the Oracle NoSQL Cloud Simulator. The code that differentiates among the configurations is in the file Common.java and can be examined to understand the differences.
Examples can be run directly from a clone of the GitHub Repository. Once the clone has been built (mvn compile) examples can be run in this manner
Run BasicTableExample using a cloud simulator instance on endpoint localhost:8080
$ mvn -pl examples exec:java -Dexec.mainClass=BasicTableExample \
-Dexec.args="http://localhost:8080"
Run BasicTableExample using an on-premises instance on endpoint localhost:8090
$ mvn -pl examples exec:java -Dexec.mainClass=BasicTableExample \
-Dexec.args="http://localhost:8090 -useKVProxy"
Run BasicTableExample using the cloud service on region us-ashburn-1
$ mvn -pl examples exec:java -Dexec.mainClass=BasicTableExample \
-Dexec.args="us-ashburn-1"
Compile Examples:
$ cd examples
$ javac -cp ../lib/nosqldriver.jar *.java
This requires Oracle Cloud credentials. Credentials can be provided directly in API or in a configuration file. The default configuration in examples/Common.java uses a configuration file in ~/.oci/config with the following contents:
[DEFAULT]
tenancy=<User OCID>
user=<Tenancy OCID>
fingerprint=<Public key fingerprint>
key_file=<PEM private key file>
pass_phrase=<Private key passphrase>
Run the example using an Oracle Cloud region endpoint.
$ java -cp .:../lib/nosqldriver.jar BasicTableExample <region>
e.g.
$ java -cp .:../lib/nosqldriver.jar BasicTableExample us-ashburn-1
The region argument will change depending on which region you use.
Running against the on-premises Oracle NoSQL Database on-premises requires a running instance of the database and running proxy service. See above.
Run against a not-secure proxy and store, with the proxy running on port 80:
$ java -cp .:../lib/nosqldriver.jar BasicTableExample http://localhost:80 -useKVProxy
When using a secure proxy and store the proxy will generally run on port 443 and requires SSL configuration. In addition the store requires a valid user and password which must have been created via administrative procedures.
Assumptions for this command:
Run the command:
$ java -Djavax.net.ssl.trustStorePassword=123456 \
-Djavax.net.ssl.trustStore=driver.trust -cp .:../lib/nosqldriver.jar \
BasicTableExample https://localhost:443 -useKVProxy -user driver \
-password Driver.User@01
Run against the Oracle NoSQL Cloud Simulator using its default endpoint of localhost:8080, assuming that the Cloud Simulator has been started. If started on a different host or port adjust the endpoint accordingly.
$ java -cp .:../lib/nosqldriver.jar BasicTableExample localhost:8080
See the LICENSE file.
The THIRD_PARTY_LICENSES file contains third party notices and licenses.
When requesting help please be sure to include as much detail as possible, including version of the SDK and simple, standalone example code as needed.
See CONTRIBUTING for details.
See SECURITY for details.