how to connect a Java app ( java code ) to cloudant?

 ̄綄美尐妖づ 提交于 2019-12-11 02:17:38

问题


I've java code in eclipse and I've done all the set up required between eclipse and IBM bluemix cloudant service I am not sure how to update my code to enable cloudant in eclipse can someone please help ?


回答1:


you need to add a piece of code in CloudantClient.java file under source directory of your project. Please add these lines in CloudantClient class:

String VCAP_SERVICES = System.getenv("VCAP_SERVICES");
                JSONObject vcap;
                vcap = (JSONObject) JSONObject.parse(VCAP_SERVICES);
                cloudant = (JSONArray) vcap.get("cloudantNoSQULDB");
                cloudantInstance = (JSONObject) cloudant.get(0);
                cloudantCredentials = (JSONObject) cloudantInstance.get("credentials");

you can also put this piece of code in a try catch loop as well.

try {
                String VCAP_SERVICES = System.getenv("VCAP_SERVICES");
                JSONObject vcap;
                vcap = (JSONObject) JSONObject.parse(VCAP_SERVICES);
                cloudant = (JSONArray) vcap.get("cloudantNoSQULDB");
                cloudantInstance = (JSONObject) cloudant.get(0);
                cloudantCredentials = (JSONObject) cloudantInstance.get("credentials");
                } 
            catch (IOException e) {
                e.printStackTrace();
            }

I hope it works!




回答2:


You may use the Bluemix config parser library to automatically parse the VCAP_SERVICES env variable (https://github.com/icha024/bluemix-config-parser)

It simplifies the messy code into...

String username = BluemixConfigStore.getConfig().getCloudantNoSQLDB()
                    .getCredentials().getUsername();
String password = BluemixConfigStore.getConfig().getCloudantNoSQLDB()
                    .getCredentials().getPassword();    

Then you can create a Cloudant client from it as usual:

CloudantClient cloudantClient = ClientBuilder.account(username)
                                  .username(username)
                                  .password(password)
                                  .build();



回答3:


You need to use VCAP_SERVICES env variable used in bluemix like below:

private JSONArray cloudant;
private JSONObject cloudantInstance;
private JSONObject cloudantCredentials;
public CloudantClient()
{
    this.httpClient = null;

    try {
        String VCAP_SERVICES = System.getenv("VCAP_SERVICES");
        JSONObject vcap;
        vcap = (JSONObject) JSONObject.parse(VCAP_SERVICES);
        cloudant = (JSONArray) vcap.get("cloudantNoSQLDB");
        cloudantInstance = (JSONObject) cloudant.get(0);
        cloudantCredentials = (JSONObject) cloudantInstance.get("credentials");
    } catch (IOException e) {
        e.printStackTrace();
    }
    this.port = Config.CLOUDANT_PORT;
    this.host = (String) cloudantCredentials.get("host");
    this.username = (String) cloudantCredentials.get("username");
    this.password = (String) cloudantCredentials.get("password");
    this.name = Config.CLOUDANT_NAME;
    this.dbc = this.createDBConnector(); 
}


来源:https://stackoverflow.com/questions/27719335/how-to-connect-a-java-app-java-code-to-cloudant

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!