CloudantDB创建view(In fact,it is the secondary Indexes)

二级索引,只不过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就行了。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值