查询视图的文档是https://github.com/cloudant/java-cloudant#query-on-a-view
我使用 groovysh(使用 java api)针对 cloudant 教育帐户的 animaldb 快速运行了一个示例。
您可以通过使用浏览器点击此 URL 来查看示例查询的原始结果:https ://education.cloudant.com/animaldb/_design/views101/_view/diet_count?reduce=true&group=true&key=%22omnivore%22
{"rows":[
{"key":"omnivore","value":3}
]}
groovy 程序(注意前两行使用 Grape 只是为了拉入 cloudant java 库):
groovy:000> import groovy.grape.Grape
groovy:000> Grape.grab(group:'com.cloudant', module:'cloudant-client', version:'1.0.1')
groovy:000> import com.cloudant.client.api.CloudantClient
groovy:000> client = new CloudantClient('education', 'education')
groovy:000> db = client.database('animaldb', false)
groovy:000> omnivores = db.view("views101/diet_count").key('omnivore').queryForInt()
===> 3
这是等效的java代码:
CloudantClient client = new CloudantClient('education', 'education');
Database db = client.database('animaldb', false);
int omnivores = db.view("views101/diet_count").key('omnivore').queryForInt();