我正在尝试从本地连接到cloudantDB。 这是我从IBM示例中获得的代码,它正在使用已部署的在线版本。
try {
System.out.println("Connecting to Cloudant : " + user);
//This works
CloudantClient client = ClientBuilder.account(user)
.username(user)
.password(password)
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build();
System.out.println("Connected to Cloudant : " + user);
//This doesn't works and fire the errror Show the server version
System.out.println("Server Version: " + client.serverVersion());
//This doesn't works and fire the error to. Show databases
System.out.println("Dbs: " + client.getAllDbs());
return client;
} catch (Exception e) {
throw new RuntimeException("Unable to connect", e);
}
从localhost我有这个错误:
[ERROR ] Failed to get cookie from server, response code 503, cookie authentication will not be attempted again
[err] java.lang.RuntimeException: Unable to connect to repository
[err] at it.sinesy.nosql.CloudantClientMgr.createClient(CloudantClientMgr.java:110)
[err] at it.sinesy.nosql.CloudantClientMgr.initClient(CloudantClientMgr.java:45)
[err] at it.sinesy.nosql.CloudantClientMgr.getDB(CloudantClientMgr.java:117)
[err] at it.sinesy.nosql.ResourceServlet.getDB(ResourceServlet.java:307)
[err] at it.sinesy.nosql.ResourceServlet.get(ResourceServlet.java:110)
[err] at sun.reflect.GeneratedMethodAccessor751.invoke(Unknown Source)
[err] at java.lang.reflect.Method.invoke(Method.java:498)
[err] at com.ibm.ws.jaxrs20.server.LibertyJaxRsServerFactoryBean.performInvocation(LibertyJaxRsServerFactoryBean.java:646)
[err] at [internal classes]
[err] at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
[err] at com.ibm.websphere.jaxrs.server.IBMRestServlet.service(IBMRestServlet.java:96)
[err] at [internal classes]
[err] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
[err] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
[err] at java.lang.Thread.run(Thread.java:748)
[err] Caused by: com.cloudant.client.org.lightcouch.CouchDbException: 503 Service Unavailable: service_unavailable: Service unavailable
[err] at com.cloudant.client.org.lightcouch.CouchDbClient.execute(CouchDbClient.java:526)
[err] at com.cloudant.client.org.lightcouch.CouchDbClient.executeToInputStream(CouchDbClient.java:574)
[err] at com.cloudant.client.org.lightcouch.CouchDbClient.get(CouchDbClient.java:320)
[err] at com.cloudant.client.org.lightcouch.CouchDbClient.serverVersion(CouchDbClient.java:240)
[err] at com.cloudant.client.api.CloudantClient.serverVersion(CloudantClient.java:272)
[err] at it.sinesy.nosql.CloudantClientMgr.createClient(CloudantClientMgr.java:103)
[err] ... 45 more
我不是代理人...... 我做错了什么?
答案 0 :(得分:3)
来自cloudant控制台"生成api密钥"我收到了帐号/密码 和数据库名称
如果您使用的是API密钥,则帐户名称与用户名不同。在您的示例中,他们都使用user
ClientBuilder.account(account_name) // needs to be the part before .cloudant.com
.username(api_key_user) // needs to be the API key
其次,API密钥是按数据库进行的,因此无权执行getAllDbs()
。
FWIW .build()
实际上没有连接到Cloudant,它只构建一个CloudantClient
对象,直到需要时才会发出HTTP请求。
答案 1 :(得分:0)
解决。 用户和密码必须从Cloudant服务创建 - >服务信誉 - >新证书。
我使用了从云端创建的错误凭证 - >数据库 - >权限 - >生成API密钥。