DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Creating Scalable OpenAI GPT Applications in Java
  • Apache Cassandra Horizontal Scalability for Java Applications [Book]
  • Prototype for a Java Database Application With REST and Security
  • Introducing Graph Concepts in Java With Eclipse JNoSQL

Trending

  • Applying Microservices Lessons to Multi-Agent AI Systems
  • Why Kotlin or Java Is the Wrong Question For Enterprises
  • When RabbitMQ Accepts a Message but Does Not Route It to a Queue
  • Beyond the Wall of Charts: How Advanced Observability Displaced the Telemetry Dashboard

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. Databases
  4. Scalability and Performance: It's Time to Relax With CouchDB and Java

Scalability and Performance: It's Time to Relax With CouchDB and Java

Scalability and performance are some of the most desirable traits for developers.

By 
Otavio Santana
DZone Core CORE ·
Nov. 15, 18 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
12.9K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Performance 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:

  1. Install Docker: https://www.docker.com/
  2. 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.

Database Java (programming language) Scalability

Opinions expressed by DZone contributors are their own.

Related

  • Creating Scalable OpenAI GPT Applications in Java
  • Apache Cassandra Horizontal Scalability for Java Applications [Book]
  • Prototype for a Java Database Application With REST and Security
  • Introducing Graph Concepts in Java With Eclipse JNoSQL

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook