1

I am new to IBM Watson. Can some one guide me on how to invoke Alchemy in Java using Bluemix? Guide me about all the Maven dependence too.

Radiodef
  • 37,180
  • 14
  • 90
  • 125
Rakshith R Pai
  • 202
  • 1
  • 5
  • 13

1 Answers1

2

It's pretty simple

Step 1: create in bluemix a project using the boilerplate "Java DB Web Starter". Ensure your application name is unique and check if you have enough free memory first (512 MB)

Step 2: on the app's Overview page, click Add Git Repo and Pipeline, or in the Bluemix Classic Experience, click ADD GIT. It will generate a GIT URL for your project code. The generated project code already uses maven and when you commit new code, Bluemix will deploy it automatically.

Step 3: clone the GIT repository using eclipse and open the project pom.xml, and add the "java-sdk" and "commons-io" dependencies like this

    <dependency>
        <groupId>com.ibm.watson.developer_cloud</groupId>
        <artifactId>java-sdk</artifactId>
        <version>2.8.0</version>
    </dependency>

    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
    </dependency>

This will trigger maven in your eclipse project and it will start to download something around 30 MB of libraries. If you haven't done this before, go get some coffee. It will take something like 5 minutes to download and resolve all the dependencies.

Step 4: Add to your Bluemix Project (using the Bluemix web UI) an Alchemy component. This will add the Alchemy API credentials to your Bluemix App VCAP_SERVICES system variable.

Step 5: In eclipse, add a code like this

package qi.watson;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.FileUtils;

import com.ibm.watson.developer_cloud.alchemy.v1.AlchemyLanguage;
import com.ibm.watson.developer_cloud.alchemy.v1.model.DocumentSentiment;
import com.ibm.watson.developer_cloud.util.CredentialUtils;

public class AlchemyAPI {

    private AlchemyLanguage al = new AlchemyLanguage();

    public AlchemyAPI() throws IOException{
        String env = System.getenv("VCAP_SERVICES");
        if (env == null){
            env = FileUtils.readFileToString(new File("/home/leoks/git/qi/qi.json"));
        }

        CredentialUtils.setServices(env);

        al.setApiKey(CredentialUtils.getAPIKey("alchemy_api"));
    }

    public static void main(String[] args) throws IOException, InterruptedException {
        AlchemyAPI api = new AlchemyAPI();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(AlchemyLanguage.TEXT, "All work and no play makes Jack a dull boy");
        DocumentSentiment sentiment = api.al.getSentiment(params);
        System.out.println(sentiment.getSentiment().getScore());
        System.out.println(sentiment.getSentiment().getType());
    }
}

Step 6: In the Bluemix Web UI, click on your app and find in the left menu the section "Environment Variables". Copy the contents from VCAP_SERVICES and paste in a local text file inside your eclipse project called /home/leoks/git/qi/qi.json for example (of course, you can change that)

Notice that Alchemy API may take some minutes to activate and you can only have 1 Alchemy API module in Bluemix.

For more information about the Watson API Java Wrapper, check this link -- https://github.com/watson-developer-cloud/java-sdk

Leo
  • 751
  • 4
  • 29
  • Hi,can you be specific about the method on how I can push application to bluemix.Which method is simpler? is it using eclipse plugin or CF push.Wil I be able to run the application on eclipse using localHost?How can I test my application in eclipse? – Rakshith R Pai Mar 08 '16 at 12:04
  • I had some bad time using the eclipse plugin, however, I've heard it has improved significantly. CF is more reliable but less convenient. I push changes into Bluemix nowadays creating a project from a boilerplate (java DB starter for example), then add a GIT repository for it (there's a button on the top right corner to do that, and the generated project already comes with Maven support), clone the repository from inside eclipse and then commit the changes. Everytime you commit the changes into GIT, Bluemix triggers a build process and re-stage your application by default. – Leo Mar 08 '16 at 12:43
  • In order to test your application locally in eclipse, you can use the CredentialsUtils class (which comes bundled in the java-sdk dependency). It reads the credentials from the VCAP_SERVICES system variable, but you can easily add a fall-back to read the same info from a local file. Once you have it set, you can call all the Watson APIs from outside Bluemix, in your local eclipse. – Leo Mar 08 '16 at 12:48
  • I have had a hard time figuring out all the dependence using bluemix and then adding git repository.that is why i decided to move to eclipse hoping for a fix. http://stackoverflow.com/questions/35859856/cannot-find-symbol-in-alchemy-api-error Have a look into this if u can find out any error?Not sure abt the pom.xml file. – Rakshith R Pai Mar 08 '16 at 13:03
  • Figuring out the dependencies manually is a tough job. That's why it uses maven, so it solves all of them for you. You have to learn a little bit of Maven to proceed. Please accept the answer if you think it's fair. – Leo Mar 08 '16 at 13:14