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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
  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.

Otavio Santana user avatar by
Otavio Santana
CORE ·
Nov. 15, 18 · Tutorial
Like (3)
Save
Tweet
Share
11.62K 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.

Popular on DZone

  • Pair Testing in Software Development
  • Implementing Infinite Scroll in jOOQ
  • The Key Assumption of Modern Work Culture
  • The Changing Face of ETL

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: