TNS
VOXPOP
Will JavaScript type annotations kill TypeScript?
The creators of Svelte and Turbo 8 both dropped TS recently saying that "it's not worth it".
Yes: If JavaScript gets type annotations then there's no reason for TypeScript to exist.
0%
No: TypeScript remains the best language for structuring large enterprise applications.
0%
TBD: The existing user base and its corpensource owner means that TypeScript isn’t likely to reach EOL without a putting up a fight.
0%
I hope they both die. I mean, if you really need strong types in the browser then you could leverage WASM and use a real programming language.
0%
I don’t know and I don’t care.
0%
Data

Getting Started with NoSQL and Java

A look at the NoSQL database landscape, as well a walk-through of a simple Java example that interacts with a NoSQL database.
Jul 27th, 2022 7:07am by
Featued image for: Getting Started with NoSQL and Java
Feature image via Pixabay.

For developers working with Java, connecting to the right database can provide significant benefits to their organization, while also making their jobs easier in the long term. This is especially true for databases that combine the flexibility and agility of NoSQL with the familiarity of SQL.

In this article, I’ll briefly dive into the NoSQL database landscape, as well as walk you through the structure of a simple Java example that interacts with a NoSQL database.

The Current State of NoSQL Database Technologies

Jagadesh Munta
Jagadesh is a principal software engineer at Couchbase. He has more than 20 years of experience in the software development life cycle, QA, DevOps and architecture. Prior to joining Couchbase, Jagadesh held technical roles at Oracle and Sun Microsystems.

NoSQL databases store data as JSON documents rather than relational tables with columns and rows. Some NoSQL databases provide programming interfaces in both SQL and native document APIs. Types of NoSQL databases include document databases, key-value stores, wide-column databases and graph databases.

NoSQL databases are known to store and process vast amounts of data efficiently and have become a foundational technology for many modern businesses. Many NoSQL databases are available in the cloud as a fully managed Database as a Service (DBaaS) or for on-premises installations. SDKs in various programming languages and APIs are used by developers to interact with such databases.

Before we get into the code samples, it would be helpful to look at the sample data and how it’s organized within the database, often referred to as the data model.

Data Model 

At the highest level of data organization, this database contains one or more buckets. Each bucket can contain one or more scopes, and each scope can contain one or more collections. Within collections are JSON documents.

This hierarchy is similar to the relational database where databases have schemas, tables and rows, etc. This hierarchical data container model of this document database maps very well to the relational model: bucket = database, scope = schema, collection = table, document = row.

This mapping allows you to access data either through the document data model in collections or the relational model using SQL.

The sample in this example is called “Travel Sample,” a data set for an airline travel information system.

The document (JSON) data model for the Travel Sample data set is shown below.

The major entities are airline, airport, route, linking airline and airport and hotel as a separate entity.

The diagram below shows the relationship between the various documents in the Airline Travel system. It shows the primary key, ID and type fields that each document contains, with additional representative fields in each type of document.

Now we are ready to look at some basic data access operations on this sample dataset.

Code Examples 

Now that the basic concepts are covered, let us see how to connect to the database, establish a context for a bucket and collection to work on, perform simple key-value operations and queries, and finally modify some data in the database.

Connect to the Database Cluster

A connection string typically consists of a host URL (IP name/addresses), followed by a username and password. Using this connection string, you can get a connection object to the database cluster. In this example, we are using localhost (127.0.0.1) as the database host. You may want to replace the DB name, username and password that are appropriate to your database cluster. A connection to the database cluster is represented by a cluster object.

Set Context to the Appropriate Collection

Let us now establish a context for a very specific dataset: a bucket named “travel-sample” that already contains the Travel Sample data set collection. The code below sets our current context to the default collection within the “travel-sample.”

Basic Key-Value Operation (Get a Document)

The key value (KV) or data service offers the simplest way to retrieve or mutate data where the key is known. The get method below retrieves specific data (value) associated with a key. In this case, the key is “airline_10.”

Query Rows Using SQL

Here is a snippet of code performing a SQL query to retrieve hotel names in the city of Malibu from the Travel Sample data set.

Query Using Named or Positional Parameters

Query methods can have named or positional parameters.

Below is a named parameter example:

The code proceeds to access the travel-sample database (specifically the name, city and state buckets). The queryOptions() method allows the customization of various SQL query options.


Positional parameters allow the order of the method parameters to be replaced with placeholders.

Using Subdocument Lookup Operations

Subdocuments are parts of the document that you can atomically and efficiently update and retrieve.

In the code below, the lookupIn operation queries the “airport_1254” document for a certain path (here, it’s the geo.alt path). This code allows us to retrieve the document path using the subdoc get operation: (get("geo.alt")).

Using Subdocument Mutate Operations

Mutation operations modify one or more paths in the document. In the code below, the mutateIn operation is used to modify the airline_10 by using a full doc-level upsert, which will create the value of an existing path with parameters (country, Canada).

Using the Upsert Function

Upsert is used to insert a new record or update an existing one. If the document doesn’t exist, it will be created. Upsert is a combination of insert and update.

The .put method allows the user to insert a mapping into a map. If an existing key is passed, the new value replaces the previous value.


The sample data set and code examples used above are from the distributed NoSQL cloud database Couchbase. Speaking of Databases as a Service, check out Couchbase Capella to see how modern enterprises are able to deliver flexibility across various use cases with built-in multimodel and mobile synchronization capabilities, and drive millisecond data response at scale.

These and many more examples can be found and run from Couchbase Playground. To connect with other like-minded developers in the community for more inspiration, check out the Couchbase Forums. For those just getting started with Java, another great resource to consider is the free online Java developer certification course offered by Couchbase Academy.

Group Created with Sketch.
TNS owner Insight Partners is an investor in: Pragma.
THE NEW STACK UPDATE A newsletter digest of the week’s most important stories & analyses.