riak:通过二级索引轮询存储桶中所有项目的存储桶

时间:2014-07-02 02:05:29

标签: curl riak secondary-indexes

尝试通过their site上的文档执行此操作,但似乎已过时。

拥有一个带有辅助索引的存储桶,并希望curl获取与所有具有特定二级索引值的对象关联的JSON。

看起来旧方法是

curl http://localhost:8098/buckets/{bucket}/index/{index}/{value}

但现在已弃用。

尝试做

curl http://localhost:8098/riak/{bucket}?keys=true&{index}={value}

但那不起作用。有什么想法正确的语法在这里吗?

1 个答案:

答案 0 :(得分:1)

您有向后弃用,不推荐/riak/{bucket}方法,/buckets/{bucket}是当前的URL方案。

请注意,您的查询curl http://localhost:8098/buckets/{bucket}/index/{index}/{value}会返回一个键列表,但不会返回它们的值。

几个例子:

首先生成一些数据:

# for i in {1..1000}; do 
   num=$RANDOM; 
   curl 172.31.0.3:8098/buckets/index_test/keys/key$i -XPUT \
        -H 'content-type: text/plain' \
        -H 'x-riak-index-random_int:'$num \
        -d "{\"value\":$num}"; 
  done

查找随机数为10000-10500的密钥:

# curl 172.31.0.3:8098/buckets/index_test/index/random_int/10000/10500

{"keys":["key334","key93","key51","key232","key427","key177","key504","key813","key472","key618","key405","key558"]}

获取为每个项目编制索引的值:

# curl 172.31.0.3:8098/buckets/index_test/index/random_int/10000/10500\?return_terms=true

{"results":[{"10189":"key334"},{"10089":"key93"},{"10013":"key558"},{"10088":"key405"},{"10057":"key51"},{"10353":"key618"},{"10282":"key472"},{"10194":"key504"},{"10301":"key232"},{"10219":"key813"},{"10311":"key427"},{"10278":"key177"}]}

但要获得实际的JSON,我们需要从KV商店获取价值:

# curl 172.31.0.3:8098/buckets/index_test/keys/key334

{"value":10189}

如果查询不经常运行且节点可以处理负载,可以通过MapReduce完成:

# curl 172.31.0.3:8098/mapred -XPOST -H 'content-type: application/json' \
  -d '{"inputs":{"bucket":"index_test","index":"random_int","start":10000,"end":10500},'\
'"query":[{"map":{"language":"javascript",'\
'"source":"function(Obj){return [Obj.values[0].data]}"}}]}'

["{value:10189}","{value:10013}","{value:10311}","{value:10278}","{value:10057}","{value:10219}","{value:10353}","{value:10089}","{value:10088}","{value:10301}","{value:10282}","{value:10194}"]

# curl 172.31.0.3:8098/mapred -XPOST -H 'content-type: application/json'\
       -d '{"inputs":{"bucket":"index_test","index":"random_int","start":10000,"end":10500},'\
'"query":[{"map":{"language":"javascript",'\
'"source":"function(Obj){return [{\"key\":Obj.key,\"data\":Obj.values[0].data}]}"}}]}'

[{"key":"key558","data":"{value:10013}"},{"key":"key334","data":"{value:10189}"},{"key":"key51","data":"{value:10057}"},{"key":"key427","data":"{value:10311}"},{"key":"key177","data":"{value:10278}"},{"key":"key813","data":"{value:10219}"},{"key":"key93","data":"{value:10089}"},{"key":"key405","data":"{value:10088}"},{"key":"key232","data":"{value:10301}"},{"key":"key618","data":"{value:10353}"},{"key":"key504","data":"{value:10194}"},{"key":"key472","data":"{value:10282}"}]