CouchDB and Ektorp usage in Java API

CouchDB is a NoSQL (not only SQL) database system. It is not a traditional database rather than it is a schema free, non-relational and table-less database system. All records are stored here as document. Ektorp is a persistence tool used by CouchDB. In this document I have explained

 

  1. How Ektorp is used within Java API

  2. How Ektorp get CouchDb connection

  3. How Ektorp create view and apply query

  1. Create a file couchdb.properties and add to the classpath. The content would be as follows:

    couchdb.name=opensrp

    host=localhost

    port=5984

    maxConnections=20

    connectionTimeout=1000

    socketTimeout=10000

    autoUpdateViewOnChange=true

  2. Include the file to the context file and add connection information to the bean class

    -----------------------------------------------------------------------

    <util:properties id="opensrp" location="classpath:/couchdb.properties"/>

    -----------------------------------------------------------------------

    <bean id="opensrpDatabaseConnector" class="org.ektorp.impl.StdCouchDbConnector">

                  <constructor-arg value="${couchdb.name}"/>

                  <constructor-arg ref="couchDbInstance"/>

          </bean>

          <bean id="couchDbInstance" class="org.ektorp.impl.StdCouchDbInstance">

                   <constructor-arg>

                                <bean class="org.ektorp.spring.HttpClientFactoryBean" />

                   </constructor-arg>

          </bean>

          -------------------------------------------------------------------------------------------------------

     3. Create entity class as like as the following example:

         @TypeDiscriminator("doc.type === 'EligibleCouple'")

         public class EligibleCouple extends CouchDbDocument {

                 @JsonProperty

                private String caseId;

                 @JsonProperty

                private String anmIdentifier;

                 @JsonProperty

                private String firstName;

                 @JsonProperty

                private String lastName;

                @JsonProperty

               private String dateOfBirth;

             public EligibleCouple() {

              }

             public EligibleCouple withANMIdentifier(String anmIdentifier) {

                  this.anmIdentifier = anmIdentifier;

                 return this;

               }

            public EligibleCouple withFirstName(String firstName) {

                  this.firstName = firstName;

                 return this;

               }

            public EligibleCouple withLastName(String lastName) {

                  this.lastName = lastName;

                 return this;

               }

             public EligibleCouple withDateOfBirth(String dateOfBirth) {

                  this.dateOfBirth = dateOfBirth;

                 return this;

               }


            }

 

     4. Create a Repository class using the following structure

          @Repository

          public class AllEligibleCouples extends CouchDbRepositorySupport<EligibleCouple> {

         public static final String OPENSRP_DATABASE_CONNECTOR = "opensrpDatabaseConnector";

         @Autowired

         public AllEligibleCouples(@Qualifier(OPENSRP_DATABASE_CONNECTOR) CouchDbConnector db) {

                 super(EligibleCouple.class, db);

         }

        @GenerateView

        public EligibleCouple findByCaseId(String caseId) {

                  List<EligibleCouple> couples = queryView("by_caseId", caseId);

                 if (couples == null || couples.isEmpty()) {

                    return null;

               }

              return couples.get(0);

        }

       @View(name = "all_ecs", map = "function(doc) { if (doc.type === 'EligibleCouple') { emit(doc.anmIdentifier); } }")

        public List<EligibleCouple> all(String anmIdentifier) {

                return db.queryView(createQuery("all_ecs").key(anmIdentifier).includeDocs(true), EligibleCouple.class);

        }

       There two type of view can be created. One is by @GenerateView annotation and another is by @View annotation

 

     5. Call repository class  within a service class

        @Service

        public class ECService {

        private AllEligibleCouples allEligibleCouples;

        @Autowired

       public ECService(AllEligibleCouples allEligibleCouples) {

          this.allEligibleCouples = allEligibleCouples;

        }

       public EligibleCouple getECByCaseId(String caseId)

       {

         EligibleCouple eligibleCouple = allEligibleCouples.findByCaseId(caseId);

        return eligibleCouple;

       }

      public List<EligibleCouple> getAllECsByANMId(String caseId)

      {

        List<EligibleCouple> ecs = allEligibleCouples.all(anmIdentifier);

        return ecs;

      }

    }

  Now use the service class as your need.

   Cheers !

Related pages