2012-10-18 29 views
1

可以在地圖縮減查詢中一起使用sec索引和鍵過濾器。事情是這樣的在RIAK中一起使用二級索引和鍵過濾器mapred

*{"inputs":{ "bucket"  :"ignore_bucket1", 
    "index"  :"secindex_bin", 
    "key"  :"secIndexVal", 
    "key_filters":[["and", 
        [["tokenize", "-", 5], ["greater_than_eq", "20120101"]], 
        [["tokenize", "-", 5], ["less_than_eq", "20120112"]] 
    ]] 
}} 

也是它有效地得到使用秒索引鍵的名單,然後返回鍵運行keyfilter?

回答

1

據我所知,在輸入語句中不可能將這些組合起來,因爲它們表示檢索密鑰的方法非常不同。通過使用二級索引來檢索初始集(避免掃描所有鍵),然後將鍵過濾邏輯實現爲映射階段函數,可以按照您的建議實現這一點。

另一種可能更快的方式來解決它可能可能是創建一個額外的複合二進制二級索引,例如, [secIndexVal] _ [日期]。如果確保這樣可以正確排序,則可以對此執行一次二級索引範圍查詢並獲取您在上面指定的值。

+0

感謝Christian。將日期添加到秒指數 –

0

AFAIK你無法通過HTTP API做到這一點,

基督教提到你可以使用範圍查詢,但你並不需要一個替代指標,你已經可以通過被引用的主鍵索引$key爲索引字段:

[email protected] ~ (master *%) » curl http://nyx:8098/riak/test/20120931 -d "31. Sep 2012" 
[email protected] ~ (master *%) » curl http://nyx:8098/riak/test/20121002 -d "02. Oct 2012" 
[email protected] ~ (master *%) » curl http://nyx:8098/riak/test/20121021 -d "21. Oct 2012 
[email protected] ~ (master *%) » curl http://nyx:8098/riak/test/20121102 -d "The future"  
[email protected] ~ (master *%) » curl -X POST -H "content-type: application/json" \ 
-d @- http://localhost:8098/mapred \ 
<<EOF 
{ "inputs":{ 
     "bucket":"test" 
    , "index":"\$key" 
    , "start":"20121001" 
    , "end":"20121101" 
} 
, "query":[{ 
     "reduce":{ 
      "language":"erlang" 
     , "module":"riak_kv_mapreduce" 
     , "function":"reduce_identity" 
     , "keep":true 
     } 
    }] 
} 
EOF 
# ... 
[["test","20121021"],["test","20121002"]] 

如果你真的想和你的鍵過濾器可以使用Erlang的PB客戶端,做沿着這些線路(你需要riak_kv在你的代碼路徑)的東西:

{ok, Pid} = riakc_pb_socket:start_link("127.0.0.1", 8087), 
Index = {index, <<"test1">>, <<"field_int">>, <<"123">>}, 
{ok, Filter} = riak_kv_mapred_filters:build_filter([[<<"ends_with">>,"1"]]). 
MapReduce = [ 
    { reduce 
    , {qfun, fun(X, F) -> lists:filter(fun({A, B}) -> F(B) end, X) end} 
    , riak_kv_mapred_filters:compose(Filter) 
    , true}], 
riakc_pb_socket:mapred(Pid, Index, MapReduce). 
相關問題