Skip to Content

Benefits of CouchDB

Currently there is a new initiative to persist data in the community called “NoSQL”. All these databases have in common that they are not using the well known relational databases and SQL as the query language. There are many open source projects in this area like Apache Hadoop and Apache Cassandra. In this article I would like to explain how to integrate another NoSQL database, called CouchDB, with the SAP NetWeaver Composition Environment.

CouchDB is built on a REST approach, meaning that all data is persisted in resources in JSON format. So, every entry is a JSON document persisted in a database in CouchDB. The access to the data works via HTTP requests following the REST architecture principle using GET, PUT, etc.

But CouchDB is not only providing the persistency, it also provides the functionality to display special views of the data. This is done with another principle related to NoSQL databases. The principle is called map/reduce. The map/reduce mechanism means that there is a map function that works with each data entry and extracts some data. In case of the CouchDB a map function is called for every JSON document. But running a map function for each and every document is only half of the story. The results of the map function calls have to be integrated again and this is done by the reduce function. The reduce function will aggregate the results into a consistent view of the data.

More details of CouchDB can be found here: http://books.couchdb.org/relax/ and on the official homepage: http://couchdb.apache.org/. In the appendix there are even links to install a CouchDB. For this blog I was using the Windows installer at http://books.couchdb.org/relax/appendix/installing-on-windows

Putting some data into CouchDB

After the installation of the CouchDB it is the question how to put some data into it. This is done with a very simple Java application.

In principle accessing CouchDB is done with HTTP requests using the built-in Java functionality for that. The connection is configured to use the HTTP PUT method to indicate that a new data entry has to be put to the database.

The following code snippet shows the basic concept.

 

 private static void createItems(String database) {
  try {
   for(int j = 0; j<10; j++) {
    URL url = new URL(dbURL + database + "/item" + j);
    HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
    OutputStreamWriter out = getWriter(httpCon);
    JSONObject o = getContent(j);
    out.write(o.toString(1));
    out.flush();
    closeConnection(httpCon);
    out.close();
   }
  } catch (MalformedURLException e) {
   e.printStackTrace();
  } catch (ProtocolException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  } catch (JSONException e) {
  }
 }
 private static OutputStreamWriter getWriter(HttpURLConnection httpCon)
   throws ProtocolException, IOException {
  httpCon.setDoOutput(true);
  httpCon.setRequestMethod("PUT");
  OutputStreamWriter out = new OutputStreamWriter(
      httpCon.getOutputStream());
  return out;
 }
 private static JSONObject getContent(int i) {
  JSONObject o = new JSONObject();
  try {
   o.put("_id", "item" + i);
   Random r = new Random(i);
   o.put("sales", r.nextInt(100));
   o.put("country", getCountry(i));
  } catch (JSONException e) {
   e.printStackTrace();
  }
  return o;
 }

 

In the example the JSON library from http://www.json.org/java/ was used to avoid manual handling of strings.

Invoke CouchDB from Web Dynpro Component

It would be definitely possible to implement a standalone application to access the CouchDB and read the data, but in this blog I would like to explain how CouchDB can be used in SAP NetWeaver Composition Environment. So, I would like to use the Web Dynpro framework as a UI technology to display some data.

CouchDB app

The usual functionality of Web Dynpro to bind the UI elements to Context nodes is used. But the context nodes have an implementation behind them, so that the data is not persisted by the Web Dynpro framework itself. The handling of data is done by some Java coding.
The Java coding is done in a similar way to the standalone application by using the HTTP library. This time it is not the built-in Java library, it is the HTTP client library of the SAP JEE server. So, the Web Dynpro DC needs a dependency to the HTTP client library. After setting the dependency the library to do HTTP calls is available.

A typical method looks like this:

 

   String t = "";
   try {
    String host = "http://10.18.111.253";
    int port = Integer.parseInt("8080");
    HostConfiguration hostConfig = new HostConfiguration(host, port);
    HttpClient client = new HttpClient();
    client.setHostConfiguration(hostConfig);
    GET httpGet = new GET("http://10.18.111.253:8080/sales/_design/countries/_view/countries?group=true");
    HttpClientParameters params = new HttpClientParameters();
    try {
     client.executeMethod(httpGet);
           t = httpGet.getResponseBodyAsString();
    } catch(Exception e2) {
     e2.printStackTrace();
    } finally {
           httpGet.releaseConnection();
       }
   } catch (Exception e) {
    e.printStackTrace();
   }
   try {
    JSONObject o = new JSONObject(t);
    JSONArray a = o.optJSONArray("rows");
    t = String.valueOf(a.length());
   } catch (JSONException e) {
    e.printStackTrace();
   }
   return t;

 

Again the JSON library is used to handle the JSON format without parsing strings.

Using map/reduce functions

But unfortunately in the moment it is only possible to show pureJSON documents stored in the database. This is not so bad, but from a database we also expect that we can get some specific data or have special view of the data. This is done by the map/reduce functionality in CouchDB. The functions can be implemented in Javascript. The interpreter for this language is included in the installation.

In the example the entries contain revenue information for specific countries. Now, the UI should display how much revenue was created in a specific country. For this reason a map function for every document is required:

 

function(doc) {
if ( doc.sales && doc.country ) {
  emit(doc.country, doc.sales);
 }
};

 

So, the parameter doc represents the JSON document. The if-statements checks, if the document is a valid entry in the database for revenue information. The emit-function is called with the attribute country as a key field and the attribute sales as a value. Only one result is returned that is a key/value pair. Internally in CouchDB all the results with the same key are grouped together and handled together.

How the key/value pairs are handled together becomes obvious by looking at the reduce function:

 

function(keys, values, rereduce) {
  return sum(values);
};

 

The function is called for each set of keys and the corresponding values. Therefore it is very simple to sum the revenue for a specific country, because CouchDB itself is grouping the keys and invoking the reduce function.
Other map/reduce functions are even part of the example. They show the real power of the map/reduce mechanism to provide different views of the data.

Summary

The blog explains how CouchDB can be used from SAP NetWeaver Composition Environment. It only gives a very simple example, but the basic concepts of CouchDB are explained: REST approach, JSON persistency of data, and map/reduce mechanism.

Additionally it is shown how these concepts can be used in CE, especially in Web Dynpro.

Resources:

Setup of the test application:

  1. Install CouchDB from the location mentioned above. Port was changed from 5984 to 8080.
  2. Put the resources from the archive to C:
    JSON sources for map/reduce: http://www.sdn.sap.com/irj/scn/index?rid=/library/uuid/b0b7bd9b-f98f-2d10-b7b3-acf47ea636e1
  3. Run the standalone application to create some documents in the server and upload the functions from C:
    Create Resources: http://www.sdn.sap.com/irj/scn/index?rid=/library/uuid/50221e86-f98f-2d10-e7b5-9ece57e1a8b8
  4. Import the product in NWDS and deploy the Web Dynpro application
    CouchDB application structure: http://www.sdn.sap.com/irj/scn/index?rid=/library/uuid/10640246-f98f-2d10-edb1-c9d0923c4592
    CouchDB application: http://www.sdn.sap.com/irj/scn/index?rid=/library/uuid/507baa5b-b190-2d10-08a5-fd6ae98055fb
To report this post you need to login first.

3 Comments

You must be Logged on to comment or reply to a post.

  1. Martin Voros
    Hi,

    nice post. Thanks to REST based interface it’s easy to integrate CouchDB to ABAP stack as well. I wrote a simple class which uses class CL_HTTP_CLIENT to provide a simple interface to CouchDB. It simply works and it didn’t take more than 2 hours.

    I am just bit worried about enterprises. It’s hard to convenience customer to deploy unsupported DB into landscape.

    Cheers

    (0) 
  2. Mark Teichmann
    CouchDB contains some of the best features of Lotus Notes (document oriented database, replication mechanism, multi-layered security concept etc.) while not being stamped as an email application 🙂

    And it is free software. I hope there will be more samples using CouchDB around in the future. Currently you can even get a free database instance at http://couch.io .

    (0) 
  3. Matthias Steiner
    Hi Ulf,

    I really liked your blog. In fact, the entire NoSQL movement will most probably gain a lot more momentum, so it’s great to see a simple primer on the topic.

    BTW, didn’t you want to blog about your Bangalore trip? 😉

    Best regards,

    Matthias

    (0) 

Leave a Reply