Community

Building a Java EE app on IBM Bluemix Using Watson and Cloudant

Share this post:

Hey Y’all!

Jeff here again and something I am really excited about is Watson is now available for anyone to use in Bluemix!

Today we are going to be building an example app using Java, Cloudant, and Watson.

Ok let’s talk through what this app is going to do before we build it.

Meet Ivy (hello!)

She’s a talent manager at a growing tech startup.

She’s looking for a new hire that would be a good fit on her team but the company is so popular that she has a huge inventory of resumes to sort through. She’s looking for tools to help her, and tools beyond just simple tag filters.

With Watson services she can also start to solve for a problem like, “I’m looking for another developer like “[insert cool employee].”

So in this case, the application can issue queries such as,

“Find me a Developer like Craig Smith”. Then search through all possible candidate and return a ranked list of candidates sorted by highest-to-lowest percentage of personality resemblance. From here, searches can be refined by including technical skills. “Find me a Developer like Craig Smith, and knows Java, C and Python”.

Make sense??

Ok let’s jump right in.

First thing you need to do is clone the github project with some starter code.

git clone https://github.com/jsloyer/talent-manager.git

Or download a zip file if you don’t have git installed https://github.com/jsloyer/talent-manager/archive/master.zip

Next we need to complete a couple pre-req steps.

Ok once we have all that setup we can start creating our app.

First we need to sign into Bluemix, so open your browser and head on over to http://bluemix.net. We need to login.  Up at the top lets go ahead and click on Catalog.

catalog

Scroll down a little and click on Liberty

liberty

On the right hand side we need to give our app a name.  Please note that this name must be unique.  Also remember this name as we will need it later…

Click create.

new-app

Bluemix will start deploying our app and Bluemix will start our app.

So next we need to add Cloudant and Watson to our app!

So do you that make sure you are in the dashboard and have your app open like the screencap below.  We will then click the “Add A Service” button.

add-service

First let’s add Watson.  Scroll down until you find the “User Modeling” Service.  Go ahead and click that.

user-modeling

Again, all we have to do is click “Create” on the right hand side.

watson-create

Bluemix is going to ask us if we want to restage our app, we should click the “OK” button.  What Bluemix is asking us here is since we made a change to our app, the app needs to be restarted to bind in Watson.

restage

Next, let’s click “Add A Service” again.

addserviceagain

This time scroll down to the bottom and choose “Cloudant”.

cloudant

Then click “Create” on the right hand side.

cloudantcreate

It is going to ask us to restage our app again, go ahead and click “OK”.

cloudantrestage

Now we have all our services added.  Let’s go ahead and start getting some data imported.

On the dashboard for the app let’s go ahead and click on Cloudant.

cloudantclick

Next, go ahead and click on “Launch”.  What this is going to do is Single Sign us onto our Cloudant account for us.

cloudantlaunch

So let’s pause and explain what Cloudant is.  Cloudant is a No-SQL database that is based on CouchDB.  The big difference between a relationship database and No-SQL database is illustrated below.  In the relationship database you have to have multiple tables to represent the data and have to use SQL and JOIN statements to get data from both tables.  In a No-SQL database which is commonly referred to as a document store database you stores JSON documents/data (as illustrated on the left below).

Figure5a

So back to the demo….

We need to copy some existing data in instead of trying to create it ourselves.  In CouchDB this is called a replication.  It’s just basically making a bunch of API calls for us and copying data.

So let’s go ahead and click  “Add New Database” in the top left.
addnewdb

Let’s choose a name for our database, lets use “talent-manager”.  You can choose something else but remember it for later…

dbname

On the left click replication.

replication

Here are 4 really important steps…

  1. Click Remote Database
  2. Enter in the database URL exactley, “https://jsloyer.cloudant.com/talent-manager” without the quotes.
  3. Under My databases type in “talent-manager” without the quotes.
  4. Click Replicate

replicate

It is going to ask us for a password.

password

We can get this password from Bluemix.  So let’s switch back to our Bluemix tab in the browser. Then click the “Overview” link on the left.

overviewbluemix

To get the password for Cloudant click “Show Credentials” on the Cloudant tile.

credentialsshow

Double click the long gorpy password as show in the screenshot below.  Right click to copy or Control+C on Windows or Command+C on Mac to copy.

password

Let’s switch back to our Cloudant tab.  We paste the paste in (step 1), and then click “Continue Replication” (step 2).

enterpassword

The copy has started, let’s go ahead and click “Databases” on the top left.

datagbases

If the replication was successfull it should show 117 documents for the talent-manager DB, is the number is lower, refresh the page, the copy hasn’t finished yet.

copydone
Ok now onto the code, let’s launch Eclipse.

In Eclipse, right click on the project’s area on the left.

import

Under General click existing project into workspace and click next

existingproject

Next we want to find our project we downloaded from github.  We need to browse to the directory where it is.  Once you find the talent-manager folder, go one more level down into that into a folder called personafusion (as shown below). Click finish.

finishimport

Next, we need to make a couple code edits…

In src/com.ibm.personafusion/Config.java.

Line 10 currently reads like below.

public static final String CLOUDANT_NAME = "";

It needs to be changed to what is below.  This is our Cloudant database name we created.  If you used something other than “talent-manager here” type that in.

public static final String CLOUDANT_NAME = "talent-manager";

Go ahead and save that file and close it.

Next open src/com.ibm.personafusion/CloudantClient.java

We need to update our constructor from what is below.

To this.
If you notice we also added 3 global variables as well.

Next we need to update the Watson code as well.  This is located in src/com.ibm.personafusion/services/WatonUserModeler.java

Before:

After:

Ok, so the app is basically done, we need to build our war file now, on the left side in Eclipse, open build.xml.  On the right hand side right click build [default] and then “Run As” and then “Ant Build”.  This will generate our WAR file for us that we will deploy.

antbuild

In the bottom it should say “BUILD SUCESSFULL”.

buildfinished

The next and last step involves deploy your app to bluemix.  This step requires the Cloud Foundry CLI to be installed (if you haven’t done this yet scroll back up the pre-req’s section above).

If you are on Windows open up the command prompt, if you are on a Mac or Linux open up the terminal.

Type the following.
cf login -a https://api.ng.bluemix.net

It will then ask for your username and password that you registered with for Bluemix.There are two important files in this directory.

  1. manifest.yml
  2. webStarterApp.war

We need to edit the manifest.yml file and then we can deploy the app.
Open the file in your favorite text editor or you can use Eclipse as well. The contents of the file are below.

There are two important lines in here, host and name. These values need to be unique and match the app we deployed earlier. In my case the app name is talent-manager-awesome, so my file would then become what is below.

This value comes from Bluemix, lets open the Bluemix dashboard in our web browser again.  In the screenshot below we can just copy the name of the app and paste this into the manifest.yml file.  So don’t copy my apps name exactly as it won’t work, it needs to be your app’s unique name.

appname

Now the final step we need to cd (change directory) in our command prompt or terminal to where the manifest.yml and webStarterApp.war are located.  For me its located at /Users/jsloyer/Downloads/talent-manager-master/personafusion.

cd /Users/jsloyer/Downloads/talent-manager-master/personafusion
cf push

The cf push command pushes our app to Bluemix, a bunch of text will fly bay saying its deploying and eventually it will say the app is starting and then it is up and running.  Bluemix will give you a URL you can access your app at.

apprunning

Voila!!!!  We created a Java web app using Cloudant and Watson on IBM Bluemix!

Source Code:
Partially complete code – https://ibm.biz/talent-manager-complete
Complete code – https://ibm.biz/talent-manager-complete (Don’t cheat and start with this one!)

Feedback is welcome, please contact me on Twitter @jsloyer – http://twitter.com/jsloyer

For a video walkthrough of this demo please visit http://ibm.biz/talent-manager-demo or watch the video below!

 


This app was developed over the course of 48 hours at an internal hackathon. The developers and designers that worked on this are the following.

  • Eva XIAOHUI LUO
  • MICHAEL J. YOUNG
  • SEAN J. WELLECK
  • BRIAN T. HAN
  • MICHAEL POPLAVSKI
  • ALAN XIA
  • Jeff Sloyer

IBM Cloud Containers Service - Core Dev Lead

More stories
September 20, 2018

Deploying to IBM Cloud Private 2.1.0.3 with IBM Cloud Developer Tools CLI

IBM Cloud Developer Tools CLI version 2.1.4 adds deployment support for IBM Cloud Private 2.1.0.3. This version of IBM Cloud Private uses a more secure Helm for Kubernetes deployments and simplifies the cluster configuration for the client compared to prior IBM Cloud Private releases.

Continue reading

September 19, 2018

Serverless Functions vs. Virtual Machines: A Total Cost of Ownership Comparison

Explore relevant costs, performance, and availability issues for a Total Cost of Ownership comparison of virtual machine and serverless functions.

Continue reading

September 19, 2018

Tutorial: Apply End-to-End Security to Cloud Applications

A new tutorial will show you how to use IBM Cloud services to secure your cloud application. Capture and review security-related events, encrypt storage, integrate authentication, and more.

Continue reading