二级索引,只不过CloudantDB中它叫view。
Demo:
CloudantClient client = new CloudantClient(account,username,pwd);//account,username,pwd
Database db = client.database("your_db_name in cloudantDB", false);
//create a View
Map<String,Object> view_location = new HashMap<String, Object>();
// view_location.put("map", "function(doc) { \\n\\t\\n\\tif(doc.properties.packet.meta.event == 'track' && doc.geometry.coordinates && doc.geometry.coordinates[1] !==0 && doc.geometry.coordinates[0] !==0){\\n\\t\\t\\n\\t\\temit(\\n\\t\\t\\t\\t[doc.properties.vehicleId,new Date(doc.properties.packet.payload.received_at).getTime()],\\n\\\t\\\t\\\t\\t{\\n\\t\\t\\t\\t\\torigin_Lat:doc.geometry.coordinates[1],\\n\\t\\t\\t\\t\\torigin_Long:doc.geometry.coordinates[0],\\n\\t\\t\\t\\t\\tmatched_Lat:doc.properties.packet.payload.loc[1],\\n\\t\\t\\t\\t\\tmatched_Long:doc.properties.packet.payload.loc[0],\\n\\t\\t\\t\\t\\trecorded_at:doc.properties.packet.payload.recorded_at\\n\\t\\t\\t\\t}\\n\\t\\t);\\n\\t\\n\\t}\\n\\n");
view_location.put("map", "function(key, value, rereduce){return sum(values)}");
view1.put("reduce", "function(key, value, rereduce){return sum(values)}");
Map<String,Object> views = new HashMap<String, Object>();
views.put("vehicle_list", view_location);
Map<String,Object> view_ddoc = new HashMap<String, Object>();
view_ddoc.put("_id", "_design/v1");
view_ddoc.put("views", views);
db.save(view_ddoc);
在创建map和reduce函数的”值“时,如果因为太复杂,而报”Bad Request“的异常,可以先写一个简单的map function和reduce function,然后到CloudantDB web console中去修改,修改好save就行了。