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

Add Comment
24 Comments

Leave a Reply

Your email address will not be published.Required fields are marked *


Eric Swanson

Jeff – are you going to update this to show the source edits directly in the cloud and the build using cloud-based Git CI integration?

Reply

Jeff Sloyer

Hey Eric,

Sorry for the delay on this, I am working on a continuation of this using the built in git tooling (devops services).

Reply

Jeff Sloyer

The instructions on how to use the auto-deploy and git hosting through IBM DevOps Services (jazzhub.com) is now live.

https://developer.ibm.com/bluemix/2014/11/10/deploying-watson-cloudant-app-devops-services/

Reply

Anil

Hi Jeff Sloyer,
i had followed all the steps that you mentioned in this article .
I came across some things- till final step it working fine.after opening “talent-manager-jeff.mybluemix.net ” only i got the problem ,i too got same interface but it didn’t displayed any images or persons below the comparison thing for compare .Where I have to feed that data ?
And One more thing In CloudantDb Have u taken any predefined datasets in database .

Thanks and Regards
Anil Kumar

Reply

    Imran

    Hi Anil,
    Were you able to resolve the issue where images were not showing once the code the deployed successfully.

    Reply

Name Rich

In Eclipse, what type of project is this?
new -> project -> Dynamic Web Project ??

I am trying to write a Bluemix app which uses Liberty for Java with a Cloudant service.
I just want to write a simple app that displays an html page (like index.html). The app connects to a cloudant DB and reads/writes data.

Thank You

Reply

    Jeff Sloyer

    It is actually a generic Java app. You can import it into Eclipse as an existing eclipse project.

    Reply

Daniel_Dugan

Jeff,
Thank you for your help at Interconnect. I was able to rapidly scale out an existing project with Bluemix. I could not have done it without you.
-Daniel from Miracle.

Reply

    Jeff Sloyer

    Thanks for the kind feedback Daniel!

    Reply

tipo

Jeff,
This is really cool layout. Has anybody else encountered what I have before? If so, how did you get around it?

I encountered a Linux (ubuntu) package called cf which gave me much grief when I tried to install clound foundry (cf). I had to delete the Linux package. I cannot get the .war built – got stock in the Eclipse build part – with a build fail:
…../BlueMix/talent-manager/personafusion/src/com/ibm/personafusion/CloudantClient.java:1: error: class, interface, or enum expected
[javac] private JSONArray cloudant;
….BlueMix/talent-manager/personafusion/src/com/ibm/personafusion/CloudantClient.java:2: error: class, interface, or enum expected
[javac] private JSONObject cloudantInstance;
[javac] ^
…..BlueMix/talent-manager/personafusion/src/com/ibm/personafusion/CloudantClient.java:3: error: class, interface, or enum expected
[javac] private JSONObject cloudantCredentials;
[javac] ^
….BlueMix/talent-manager/personafusion/src/com/ibm/personafusion/CloudantClient.java:5: error: ‘{‘ expected
[javac] public class CloudantClient()
[javac] ^

BUILD FAILED
….BlueMix/talent-manager/personafusion/build.xml:43: Compile failed; see the compiler error output for details.

Total time: 1 second

Anybody encounter this before? How did you get around it?
Cheers!

Reply

Michel Reyrolle

Hi

I compiled the application under Eclipse and now i am try to deploy the application using cf push. I added the two required services (user modeling (now renamed personality insights, cloudantNoSQLDB) as specified in the demo script above.

The binding to cloudantNoSQLDB fails after successful upload of application war files. Below is my manifest.yml file.

declared-services:
cloudantNoSQLDB:
label: cloudantNoSQLDB
plan: Shared
personality-insights:
label: personality_insights
plan: “IBM Watson Personality Insights Monthly Plan”
applications:
– disk_quota: 1024M
host: MR-talent-manager-awesome
name: MR-talent-manager-awesome
path: webStarterApp.war
instances: 1
memory: 512M
services:
– cloudantNoSQLDB
– personality_insights

any idea??

Reply

    Jeff Sloyer

    The name of the personality insights plan has changed from “IBM Watson Personality Insights Monthly Plan” to standard. Sorry about that.

    Reply

      Kihumba

      I have the same problem as Michel Reyrolle above and I cant get it resolved even after making the change you have recommended.Below is my manifest.yml file.


      declared-services:
      talent-manager-db:
      label: cloudantNoSQLDB
      plan: Shared
      personality-insights-talent-manager:
      label: personality_insights
      plan: “standard”
      applications:
      – disk_quota: 1024M
      host: talent-manager-kn
      name: talent-manager-kn
      path: ./webStarterApp.war
      instances: 1
      buildpack: https://github.com/cloudfoundry/java-buildpack.git
      memory: 512M
      services:
      – talent-manager-db
      – personality-insights-talent-manager

      Reply

John

Had the same problem but found that the services don’t really match the services I have, probably the same for you.

cf services

will list the services that are in your instance – then you have to put those in the services section and try again..

Reply

Shiling Huang

In the starter code you offered for download, you added some .jar files in deb-jar directory. What are the correct places or steps to download them from?

Reply

André LAGUERRE

Hi,

thank’s for this tutorial and the Demo video, but I tried to reproduce this case but I don’t find the service “User Modeling” in the Watson Services ?

Is there another solution ?

Reply

    André LAGUERRE

    Resolved… It’s explained in post before…

    “User Modeling” is now renamed “Personality Insights”

    Reply

PREMSAICEEDELLA

hello sir i had just followed the same process in your tutorial and successfully builtin and also deployed successfully but the application is not working it just showing webpage as before deploying into it please let me know how to do.
thankyou!

Reply

Einar Karlsen

I got it to work. 3 things in the manifest needs to be changed:

1) The standard plan for personality insights is called “tiered”. You can get from the Bluemix VCAPs file.

2) The path for the war file must be changed to ./output/webStarterApp.war. That’s where the war is really located.

3) The name of the services at the top and the bottom of the manifest must be changed to the names used by the app.
As already mentioned in another comment, you can get the true names by invoking ‘cf services’ or alternatively from the VCAP file.

Here’s my manifest:

declared-services:
Cloudant NoSQL DB-im:
label: cloudantNoSQLDB
plan: Shared
Personality Insights-7r:
label: personality_insights
plan: tiered
applications:
– disk_quota: 1024M
host: EinarsTalentManager
name: EinarsTalentManager
path: ./output/webStarterApp.war
instances: 1
buildpack: https://github.com/cloudfoundry/java-buildpack.git
memory: 512M
services:
– Cloudant NoSQL DB-im
– Personality Insights-7r

Reply

Ammar

Hello sir, I get this issue when i try to upload the mainfest file via command line and it says:

Using route talent-manager-kea.mybluemix.net
Uploading talent-manager-kea…
Uploading app files from: C:UsersammarAppDataLocalTempunzipped-app583602255
Uploading 3.8M, 145 files
Done uploading
OK
FAILED
Could not find service talent-manager-kea to bind to talent-manager-kea

Pls help I don’t know what to do anymore.

Reply

LarsBesselmann

Instead of editing the yml file and adding a randomly created service name it is much easier to rename the service name for db and watson to the ones expected by the yml file meaning
talent-manager-db and personality-insights-talent-manager.
This can be easily done within the Bluemix dashboard via the menu for the related service.

Reply

Sok Dina

Hi Jeff Sloyer, it is very nice project, I have tested it. Everything is successful except it doesn’t load data when I test.

https://talented-professors-mu.mybluemix.net/#/
or
https://talent-professors-masaryk-university.mybluemix.net/#/

could you please advice me something?

Reply
More How-tos Stories

Deploying a React web app with IBM Container Services

The IBM NodeJS team has built a starter for developers to quickly create and deploy a MERN stack in a Kubernetes container. You may ask, “What is a MERN stack?” MERN stands for MongoDB, Express, React and NodeJS. Our MERN starter is a working application with a React frontend that makes HTTP requests to an Express/Node.js backend, where sessions are persisted using MongoDB.

Continue reading

Object Storage v1 service being shut down

We’d like to inform you that the Object Store v1 service on Bluemix is being shutdown. The v3 driver will still be available and accessible for use.

Continue reading

Rapidly developing applications (part 3): implementing your microservices project

This is the third post in a series on microservices application development. The series provides a context for defining a cloud-based pilot project that best fits current needs and prepares for a longer-term cloud adoption decision.

Continue reading