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
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • Creating Scalable OpenAI GPT Applications in Java
  • Apache Cassandra Horizontal Scalability for Java Applications [Book]
  • Introducing Graph Concepts in Java With Eclipse JNoSQL
  • Jakarta NoSQL 1.0: A Way To Bring Java and NoSQL Together

Trending

  • What Is Plagiarism? How to Avoid It and Cite Sources
  • Microservices for Machine Learning
  • Microservice Madness: Debunking Myths and Exposing Pitfalls
  • The QA Paradox: To Save Artificial Intelligence, We Must Stop Blindly Trusting Data—And Start Trusting Human Judgment
  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 user avatar
Otavio Santana
DZone Core CORE ·
Nov. 15, 18 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
12.5K Views

Join the DZone community and get the full member experience.

Join For Free

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]
  • Introducing Graph Concepts in Java With Eclipse JNoSQL
  • Jakarta NoSQL 1.0: A Way To Bring Java and NoSQL Together

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: