CouchDB filtered replication

One of the greatest features of CouchDB is its replication which allows for great distributed computing. It reminds me when in 1999 I met Erlang language for the first time (Working for a Telco). Erlang is made for distributed computing and so CouchDB which of course is built in Erlang.

I have to say I have successfully tested this in upcoming version 1.1.1 ( built from a branch) Do not try this in 1.1.0.

The example below is based on the document that I have been discussing in the three part tutorial about building a Document Management System (DMS) with CouchDB.

Filtered or selective replication is a two step process:
  1. First create a filter named for example "clientFilter" in a new document called "replicateFilter". This sample filter will reject any client not matching the clientId parameter (step 2 explains what this parameter is about). Any deleted documents will be deleted from the target as well.
    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    curl -H 'Content-Type: application/json' -X PUT http: //127 .0.0.1:5984 /dms4/_design/replicateFilter -d \
    '{
       "filters" :{
         "clientFilter" :" function (doc, req) {
           if (doc._deleted) {
             return true ;
           }
      
           if (!doc.clientId) {
             return false ;
           }
      
           if (!req.query.clientId) {
             throw(\"Please provide a query parameter clientId.\");
           }
      
           if (doc.clientId == req.query.clientId) {
             return true ;
           }
           return false ;
         }"
       }
    }'
  2. Create a replication document called "by_clientId". This example passes clientId=1 as a parameter to the filter we created in step number 1 ("replicateFilter/clientFilter"). You figured we will end up replicating documents for that client.
    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    curl -H 'Content-Type: application/json' -X POST http: //127 .0.0.1:5984 /_replicator -d \
    '{
       "_id" : "by_clientId" ,
       "source" : "dms4" ,
       "create_target" : true ,
       "continuous" : true ,
       "filter" : "replicateFilter/clientFilter" ,
       "query_params" :{ "clientId" :1}
    }'

Deleting a replication document is how you turn off that replication. This is not any different than deleting any other document:
?
1
2
3
4
nestor:~ nestor$ curl -X GET http: //127 .0.0.1:5984 /_replicator/by_clientId
{ "_id" : "by_clientId" , "_rev" : "5-e177ca7f79d9ba6f91b803a2cb2abc1e" , "source" : "dms4" , "target" : "http://couchdb.nestorurquiza.com:5984/dms4" , "create_target" : true , "continuous" : true , "filter" : "replicateFilter/clientFilter" , "query_params" :{ "clientId" :1}, "_replication_state" : "triggered" , "_replication_state_time" : "2011-10-20T13:09:56-04:00" , "_replication_id" : "d8dc09e97f4948de0294260dda19fc6f" }
nestor:~ nestor$ curl -X DELETE http: //127 .0.0.1:5984 /_replicator/by_clientId ?rev=5-e177ca7f79d9ba6f91b803a2cb2abc1e
{ "ok" : true , "id" : "by_clientId" , "rev" : "6-0d20d90cbed22837eb3233e2bd8dfb2c" }

The same applies for getting a list of the current defined "selective replicators". You can use a temporary view like I show here or create a permanent view to list all the replicators:
?
1
2
3
4
5
$ curl -X POST http: //127 .0.0.1:5984 /_replicator/_temp_view -H "Content-Type: application/json" -d '{
   "map" : " function (doc) {
             emit(null, doc);
           }"
}'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值