官方文档
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可以以此构造不用每次都写入数据库
分享到:
相关推荐
Secondary Indexing in Phoenix
Challenges Facing Secondary Education in the Third World Countries,Magreth Columbani Komba,,The aim of this paper is to explain the main challenges facing secondary education in the third world ...
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
速生杨形成层和次生木质部的季节性发育,席恩华,赵广杰,本文运用传统的木材解剖学方法研究了活动期内速生欧美杨107形成层的解剖特征和次生木质部的细胞的堆积过程,同时探讨了季节性形成
classes of customers, namely primary and secondary users. Primary users have long term contracts while secondary users are admitted and priced according to current availability of excess spectrum. ...
基于模仿机制的认知无线电频谱动态接入,傅洛伊,陈海波,在认知无线电技术网络中,动态频谱分配是一项重要技术。本文主要关注二级用户如何通过协作或非协作方式使用授权频谱用户的空闲频
Board of elementary and secondary education. use in code test.
Secondary Data Research in a Digital Age.ppt
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 ...
Outage Performance of Relay-Assisted Primary and Secondary Transmissions in Cognitive Relay Networks
An Efficient Bulk Loading Approach of Secondary index in Distributed Log-Structed Data Stores
Influence of surface charging on secondary-electron yield of MgO film
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 ...
认知无线网络的联合波束成形和用户选择,孙治水,沈涛,在主网络和次网络共享相同频谱的情况下,本文通过子空间理论分析,提出了一种用于次网络下行链路的联合波束成形和用户选择算法,
centralaized secondary cotrol of microgrid
In the secondary control, restorations for both voltage and frequency in the droop- controlled inverter-based islanded MG are addressed. A distributed finite-time con- trol approach is used in the ...
Numerical Study of Secondary Cooling Technology in Continuous Casting