官方文档
http://wiki.basho.com/Secondary-Indexes.html#Indexing-an-Object
在看官方文档时,描述的例子都是用crul描述的,而我是用的riak_erlang_client 只能看api尝试调用了
new/3方法中是提供不了索引的,不过有个update_medadata/2方法 可以根据这个方法插入数据,
我首先写入一个K-V,再立刻读取它,获取的medadata其实是一个dict(),在dict 中插入index,最后写入书库就实现了。
查询的时候用get_index/4即可
过程:
(ejabberd@meta)22> Bucket= <<"user">> .
<<"user">>
(ejabberd@meta)23> Key= <<"langxw">>.
<<"langxw">>
(ejabberd@meta)24> Value= term_to_binary({"jason","29","male"}).
<<131,104,3,107,0,5,106,97,115,111,110,107,0,2,50,57,107,
0,4,109,97,108,101>>
(ejabberd@meta)25> Age="29".
"29"
(ejabberd@meta)26> ObN=riakc_obj:new(Bucket, Key, Value). 根据Bucket,Key,Value构造数据
{riakc_obj,<<"user">>,<<"langxw">>,undefined,[],undefined,
<<131,104,3,107,0,5,106,97,115,111,110,107,0,2,50,57,107,
0,4,109,97,108,...>>}
(ejabberd@meta)28> f(Pid), {ok, Pid} = riakc_pb_socket:start_link("127.0.0.1", 8087).
{ok,<0.582.0>}
(ejabberd@meta)29> riakc_pb_socket:put(Pid,ObN). 把数据写入数据库
ok
(ejabberd@meta)30> {ok, ObR}=riakc_pb_socket:get(Pid,Bucket,Key). 再根据键值读出数据
{ok,{riakc_obj,<<"user">>,<<"langxw">>,
<<107,206,97,96,96,96,204,96,202,5,82,28,202,156,255,126,
250,223,188,122,42,131,41,...>>,
[{{dict,2,16,16,8,80,48,
{[],[],[],[],[],[],[],[],[],[],[],[],...},
{{[],[],[],[],[],[],[],[],[],[],...}}},
<<131,104,3,107,0,5,106,97,115,111,110,107,0,2,50,57,
107,0,4,...>>}],
undefined,undefined}}
(ejabberd@meta)31> Dict = riakc_obj:get_metadata(ObR) 得到metadata数据
(ejabberd@meta)31> .
{dict,2,16,16,8,80,48,
{[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]},
{{[],[],[],[],[],[],[],[],[],[],
[[<<"X-Riak-VTag">>,49,100,75,89,57,82|...]],
[],[],
[[<<"X-Riak-Last-"...>>|{1341,393029,...}]],
[],[]}}}
(ejabberd@meta)32> DictN=di
dict disk_log disk_log_1 disk_log_server
disk_log_sup
(ejabberd@meta)32> DictN=dict:store( <<"index">>, [{"age_ing","29"}], Dict). 构造index
{dict,3,16,16,8,80,48,
{[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]},
{{[],[],[],[],[],[],[],[],[],[],
[[<<"X-Riak-VTag">>,49,100,75,89,57,82|...]],
[[<<"index">>,{"age_ing","29"}]],
[],
[[<<"X-Riak-Last-"...>>|{1341,393029,...}]],
[],[]}}}
(ejabberd@meta)33> ObRN=riakc_obj:update_metadata(ObR,DictN). 构造数据
{riakc_obj,<<"user">>,<<"langxw">>,
<<107,206,97,96,96,96,204,96,202,5,82,28,202,156,255,126,
250,223,188,122,42,131,41,145,49,...>>,
[{{dict,2,16,16,8,80,48,
{[],[],[],[],[],[],[],[],[],[],[],[],[],[],...},
{{[],[],[],[],[],[],[],[],[],[],[[...]],[],...}}},
<<131,104,3,107,0,5,106,97,115,111,110,107,0,2,50,57,
107,0,4,109,97,...>>}],
{dict,3,16,16,8,80,48,
{[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],...},
{{[],[],[],[],[],[],[],[],[],[],
[[<<...>>|...]],
[[...]],
[],...}}},
undefined}
(ejabberd@meta)34> riakc_pb_socket:put(Pid, ObRN).
{error,<<"{precommit_fail,[{unknown_field_type,<<\"age_ing\">>}]}">>}再次写入数据 int写成 ing所以写入失败
(ejabberd@meta)35> f(DictN),DictN=dict:store( <<"index">>, [{"age_int","29"}], Dict).
{dict,3,16,16,8,80,48,
{[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]},
{{[],[],[],[],[],[],[],[],[],[],
[[<<"X-Riak-VTag">>,49,100,75,89,57,82|...]],
[[<<"index">>,{"age_int","29"}]],
[],
[[<<"X-Riak-Last-"...>>|{1341,393029,...}]],
[],[]}}}
(ejabberd@meta)36> f(ObRN),ObRN=riakc_obj:update_metadata(ObR,DictN). 修正后再次写入
{riakc_obj,<<"user">>,<<"langxw">>,
<<107,206,97,96,96,96,204,96,202,5,82,28,202,156,255,126,
250,223,188,122,42,131,41,145,49,...>>,
[{{dict,2,16,16,8,80,48,
{[],[],[],[],[],[],[],[],[],[],[],[],[],[],...},
{{[],[],[],[],[],[],[],[],[],[],[[...]],[],...}}},
<<131,104,3,107,0,5,106,97,115,111,110,107,0,2,50,57,
107,0,4,109,97,...>>}],
{dict,3,16,16,8,80,48,
{[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],...},
{{[],[],[],[],[],[],[],[],[],[],
[[<<...>>|...]],
[[...]],
[],...}}},
undefined}
(ejabberd@meta)37> riakc_pb_socket:put(Pid, ObRN).
ok
(ejabberd@meta)38> riakc_pb
riakc_pb riakc_pb_socket
(ejabberd@meta)38> riakc_pb_socket:get_index(Pid, Bucket, <<"age_int">>, 29). 验证写入是否成功,OK 啦
{ok,[[<<"user">>,<<"langxw">>]]}
(ejabberd@meta)39> Key.
<<"langxw">>
(ejabberd@meta)40>
注意:以上步骤有点繁琐,索引和value可以以此构造不用每次都写入数据库
分享到:
相关推荐
4. **Secondary Indexes**:Riak 支持二级索引,这允许用户通过多种方式查询数据,而不只是通过键值。 **准备与安装** 在开始使用 Riak 之前,你需要下载客户端库,如 Erlang Protocol Buffer 客户端,然后安装 ...
It also brings coverage of multiple new features, including the new generation of client SDKs, security and LDAP integration, secondary indexes, and multi-dimensional scaling. Pro Couchbase Server ...
qlap qlap是一个MySQL基准测试工具,例如 。 用法 Usage of qlap: ... Number of secondary indexes in the table to be created -auto-generate-sql-write-number int Number of rows to be pre-pop
本篇论文详细探讨了速生欧美杨(Populus×euramericana cv.‘74/76’)形成层和次生木质部在活动期内的季节性发育情况。文章主要通过传统的木材解剖学方法对杨树形成层的解剖特征进行了描述,并对次生木质部细胞的...
Phoenix 还提供了强大的 SQL 标准兼容性,支持secondary indexes、Windowed aggregates 和 Connectivity 等高级功能。 在本文中,我们将讨论如何使用 Cost-based Query Optimization 来优化 secondary indexes 的...
认知无线电网络(Cognitive Radio Networks,简称CRNs)允许未授权用户(二级用户Secondary Users,简称SUs)在授权用户(一级用户Primary Users,简称PUs)空闲的频谱间隙(即频谱空洞)中动态接入已授权的频谱。...
以及华为的Server-local indexes和Buddy regions,这些方法都是尝试解决HBase中二级索引的问题。 #### 六、不可变索引 不可变索引是一种较为简单的索引实现方式。它们通常是由客户端管理的,并且可以通过批量加载...
Board of elementary and secondary education. use in code test.
接下来,需要编辑配置文件,例如primary.conf、secondary1.conf、secondary2.conf和arbiter.conf等。这些配置文件用于定义Nosql数据库的各个节点的角色和配置。 5. 创建进程描述文件目录 最后,需要创建进程描述...
Ni-Mn-Sn-Co快凝薄带材料中第二相晶体结构表征及形成机制,余金科,王戊,本文采用XRD衍射、扫描电镜以及透射电镜技术对Ni-Mn-Sn-Co快凝薄带材料中的第二相晶体结构进行了详细表征,并对其形成机制进行探索。...
Our primary aim is to illustrate the power and elegance of mathematical reasoning in science; our secondary aim, to show also that science egenders mathematics. <br>We start with Archimedes work ...
centralaized secondary cotrol of microgrid
In Techniques of Model-Based Control, two leading experts bring together powerful advances in model-based control for chemical-process engineering. Coleman Brosilow and Babu Joseph focus on practical ...
鲸类二次水生生境适应渗透调节相关基因的适应性进化,徐士霞,杨云霞,渗透调节是鲸类祖先从陆地重返高渗的海洋环境中面临的主要挑战,尽管先前研究已经鉴定出鲸类形成了水盐调节的诸多生理机制,但是
The MATLAB codes presented in the book are tested with thousands of runs of MATLAB randomly generated matrices, and the notation in the book follows the MATLAB style to ensure a smooth transition ...
一株无花果内生真菌Fusarium sp.的代谢产物,张弘弛,马养民,目的:研究无花果内生真菌Fusarium sp.的代谢产物。方法:采用反复硅胶柱色谱法、Sephadex LH-20凝胶色谱法等进行分离纯化,并通过理化常�