Scalability and Performance: It's Time to Relax With CouchDB and Java
Scalability and performance are some of the most desirable traits for developers.
Join the DZone community and get the full member experience.
Join For FreePerformance and scalability are the most desirable traits that developers talk about in regards to persistence technology. Apache CouchDB makes this dream a reality. Apache CouchDB is an open-source database software that focuses on ease of use and having a scalable architecture. CouchDB is a document-oriented NoSQL database architecture and is implemented in the concurrency-oriented language Erlang; it uses JSON to store data, JavaScript as its query language using MapReduce, and HTTP for an API. This article will cover about this NoSQL database and integrate it with Jakarta EE.
Installing CouchDB Using Docker
To install CouchDB using Docker, we need to follow these steps:
- Install Docker: https://www.docker.com/
- Run the docker command
docker run --name couchdb_instance -p 5984:5984 -d couchdb
Setting the Application Dependencies
The next step is to configure a smooth Java SE application with CDI and Eclipse JNoSQL with CouchDB. This demo uses a Maven project that needs to set the dependencies beyond CDI 2.0 implementation. It needs the Eclipse JNosQL dependencies as shown below:
<dependency>
<groupId>org.jnosql.artemis</groupId>
<artifactId>artemis-document</artifactId>
<version>0.0.7</version>
</dependency>
<dependency>
<groupId>org.jnosql.diana</groupId>
<artifactId>couchdb-driver</artifactId>
<version>0.0.7</version>
</dependency>
It is equally important to do a DocumentCollectionManager
, which is eligible to CDI setting an instance as a producer.
@ApplicationScoped
public class DocumentCollectionManagerProducer {
private static final String HEROES = "heroes";
@Inject
@ConfigurationUnit(name = "document")
private DocumentCollectionManagerFactory<DocumentCollectionManager> entityManager;
@Produces
public DocumentCollectionManager getManager() {
return entityManager.get(HEROES);
}
}
Using the ConfigurationUnilt
annotation, we will read from a jnosl.json, then serialize it as a manager factory. Eclipse JNoSQL can support several extension files, such as XML, YAML, and JSON.
[
{
"description": "The couchdb document configuration",
"name": "document",
"provider": "org.jnosql.diana.couchdb.document.CouchDBDocumentConfiguration",
"settings": {
"couchdb.host": "localhost"
}
}
]
At this sample, we will use a Hero entity that has as attributes their name, real name, age, and powers.
@Entity
public class Hero implements Serializable {
@Id
private String id;
@Column
private String name;
@Column
private String realName;
@Column
private Integer age;
@Column
private Set<String> powers;
}
The model and the configuration are ready, and it is time to create the class that will run this demo.
public class App {
public static void main(String[] args) {
try (SeContainer container = SeContainerInitializer.newInstance().initialize()) {
Hero ironMan = Hero.builder().withRealName("Tony Stark").withName("iron_man")
.withAge(34).withPowers(Collections.singleton("rich")).build();
DocumentTemplate template = container.select(DocumentTemplate.class).get();
template.insert(ironMan);
DocumentQuery query = select().from("Hero").where("_id").eq("iron_man").build();
List<Hero> heroes = template.select(query);
System.out.println(heroes);
}
}
private App() {
}
}
To make that integration easier, there is the repository that handles the implementation of the methods that exist in the repository interface and new methods once its method query convention.
public interface HeroRepository extends Repository<Hero, String> {
Optional<Hero> findByName(String name);
List<Hero> findByAgeGreaterThan(Integer age);
List<Hero> findByAgeLessThan(Integer age);
}
public class App3 {
public static void main(String[] args) {
try (SeContainer container = SeContainerInitializer.newInstance().initialize()) {
Hero ironMan = Hero.builder().withRealName("Tony Stark").withName("iron_man")
.withAge(34).withPowers(Collections.singleton("rich")).build();
HeroRepository repository = container.select(HeroRepository.class, DatabaseQualifier.ofDocument()).get();
repository.save(ironMan);
System.out.println(repository.findByName("iron_man"));
System.out.println(repository.findByAgeGreaterThan(30));
System.out.println(repository.findByAgeLessThan(40));
}
}
private App3() {
}
}
Also, since previous the version, it has support to query as text. This language will work to any document database who supports Document API from Eclipse JNoSQL.
public class App2 {
public static void main(String[] args) {
try (SeContainer container = SeContainerInitializer.newInstance().initialize()) {
Hero ironMan = Hero.builder().withRealName("Tony Stark").withName("iron_man")
.withAge(34).withPowers(Collections.singleton("rich")).build();
DocumentTemplate template = container.select(DocumentTemplate.class).get();
template.update(ironMan);
PreparedStatement prepare = template.prepare("select * from Hero where realName =@name");
List<Hero> heroes = prepare.bind("name", "Tony Stark").getResultList();
System.out.println(heroes);
System.out.println(template.query("select * from Hero where _id = 'iron_man'"));
}
}
private App2() {
}
}
On the simplicity topic, there is an integrated GUI to manipulate CouchDB, the Fauxton. Fauxton is a native web-based interface built into CouchDB. It provides a basic interface to the majority of the functionality, including the ability to create, update, delete, and view documents and design documents. It provides access to the configuration parameters and an interface for initiating replication.
To access it, go to http://IP:5984/_utils/ in the browser where IP is the IP of the database. E.G.: http://127.0.0.1:5984/_utils/
You can check out a CouchDB code sample here.
Opinions expressed by DZone contributors are their own.
Comments