Skip to content

Instantly share code, notes, and snippets.

@wang-zhijun
Last active August 24, 2016 00:42
Show Gist options
  • Select an option

  • Save wang-zhijun/ba74eb446bfcb355f02c to your computer and use it in GitHub Desktop.

Select an option

Save wang-zhijun/ba74eb446bfcb355f02c to your computer and use it in GitHub Desktop.
riak-erlang-clientの使い方

Riakにコネクションを貼る

iex(1)> {:ok, pid} = :riakc_pb_socket.start_link('*.*.*.*', 8087)
{:ok, #PID<0.191.0>}
iex(2)> :riakc_pb_socket.ping(pid)
:pong

Riakオブジェクトを作成して,Riakに保存する

iex(3)> object = :riakc_obj.new("riak-bucket-test", "key1", "value1")
{:riakc_obj, "riak-bucket-test", "key1", :undefined, [], :undefined, "value1"}
iex(4)> :riakc_pb_socket.put(pid, object)
:ok

Riakに保存したオブジェクトを取得

iex(5)> {:ok, obj} = :riakc_pb_socket.get(pid, "riak-bucket-test", "key1")
{:ok,
 {:riakc_obj, "riak-bucket-test", "key1",
  <<107, 206, 97, 96, 96, 96, 204, 96, 202, 5, 82, 28, 147, 3, 54, 4, 134, 237, 176, 254, 144, 193, 148, 200, 152, 199, 202, 176, 118, 69, 234, 5, 190, 44, 0>>,
  [{{:dict, 2, 16, 16, 8, 80, 48,
     {[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []},
     {{[], [], [], [], [], [], [], [], [], [],
       [["X-Riak-VTag", 49, 122, 97, 67, 57, 48, 74, 57, 78, 113, 86, 66, 77,
         80, 77, 82, 98, 66, 115, 106, 112, 108]], [], [],
       [["X-Riak-Last-Modified" | {1458, 646189, 246697}]], [], []}}},
    "value1"}], :undefined, :undefined}}

このobjはsiblingがないことを仮定して,関連のvalueを取ってくる,ソース

iex(6)> :riakc_obj.get_value(obj)
"value1"

:riakc_obj.get_value/は実は:riakc_obj.get_contentsを呼び出して,一回パターンマッチングしただけ

iex(14)> contents = :riakc_obj.get_contents(obj)
[{{:dict, 2, 16, 16, 8, 80, 48,
   {[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []},
   {{[], [], [], [], [], [], [], [], [], [],
     [["X-Riak-VTag", 49, 122, 97, 67, 57, 48, 74, 57, 78, 113, 86, 66, 77, 80,
       77, 82, 98, 66, 115, 106, 112, 108]], [], [],
     [["X-Riak-Last-Modified" | {1458, 646189, 246697}]], [], []}}}, "value1"}]
iex(15)> [{_md, v}] = contents
[{{:dict, 2, 16, 16, 8, 80, 48,
   {[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []},
   {{[], [], [], [], [], [], [], [], [], [],
     [["X-Riak-VTag", 49, 122, 97, 67, 57, 48, 74, 57, 78, 113, 86, 66, 77, 80,
       77, 82, 98, 66, 115, 106, 112, 108]], [], [],
     [["X-Riak-Last-Modified" | {1458, 646189, 246697}]], [], []}}}, "value1"}]
iex(16)> v
"value1"

このobjはsiblingがないことを仮定して,関連のmetadataを取ってくる,ソース, そしてリストに変換して,中身を確認する

iex(11)> d = :riakc_obj.get_metadata(obj)
{:dict, 2, 16, 16, 8, 80, 48,
 {[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []},
 {{[], [], [], [], [], [], [], [], [], [],
   [["X-Riak-VTag", 49, 122, 97, 67, 57, 48, 74, 57, 78, 113, 86, 66, 77, 80,
     77, 82, 98, 66, 115, 106, 112, 108]], [], [],
   [["X-Riak-Last-Modified" | {1458, 646189, 246697}]], [], []}}}
iex(12)> :dict.to_list(d)
[{"X-Riak-VTag", '1zaC90J9NqVBMPMRbBsjpl'},
 {"X-Riak-Last-Modified", {1458, 646189, 246697}}]

さっき作成したバケットriak-bucket-testの属性をチェックした場合,例えばallow_mult属性

iex(1)> {:ok, pid} = :riakc_pb_socket.start_link('*.*.*.*', 8087)
{:ok, #PID<0.237.0>}
iex(5)> {:ok, props} = :riakc_pb_socket.get_bucket(pid, "riak-bucket-test")
{:ok,
 [n_val: 3, old_vclock: 86400, young_vclock: 20, big_vclock: 50,
  small_vclock: 50, allow_mult: true, last_write_wins: false,
  basic_quorum: false, notfound_ok: true, precommit: [], postcommit: [],
  chash_keyfun: {:riak_core_util, :chash_std_keyfun},
  linkfun: {:modfun, :riak_kv_wm_link_walker, :mapreduce_linkfun}, pr: 0,
  r: :quorum, w: :quorum, pw: 0, dw: :quorum, rw: :quorum]}
iex(6)> props[:allow_mult]
true

:riakc_pb_socket.set_bucket関数を使って,作成したバケットriak-bucket-testの属性allow_multfalseに変更

iex(7)> :riakc_pb_socket.set_bucket(pid, "riak-bucket-test", [{:allow_mult,  false}])
:ok
iex(8)> {:ok, props} = :riakc_pb_socket.get_bucket(pid, "riak-bucket-test")
{:ok,
 [n_val: 3, old_vclock: 86400, young_vclock: 20, big_vclock: 50,
  small_vclock: 50, allow_mult: false, last_write_wins: false,
  basic_quorum: false, notfound_ok: true, precommit: [], postcommit: [],
  chash_keyfun: {:riak_core_util, :chash_std_keyfun},
  linkfun: {:modfun, :riak_kv_wm_link_walker, :mapreduce_linkfun}, pr: 0,
  r: :quorum, w: :quorum, pw: 0, dw: :quorum, rw: :quorum]}
iex(9)> props[:allow_mult]                                                       
false

usersバケットを作成して,Secondary Indexを使ってみる.

  1. Content-typetext/plainにしたオブジェクトを作成

  2. metadataを取得

  3. 新しいmetadataに更新

  4. オブジェクトを更新

iex(2)> obj = :riakc_obj.new({"default", "users"}, "john_smith", "...user data...", "text/plain")
{:riakc_obj, {"default", "users"}, "john_smith", :undefined, [],
 {:dict, 1, 16, 16, 8, 80, 48,
  {[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []},
  {{[], [], [], [], [], [], [], [], [], [],
    [["content-type", 116, 101, 120, 116, 47, 112, 108, 97, 105, 110]], [], [],
    [], [], []}}}, "...user data..."}
iex(3)> md1 = :riakc_obj.get_update_metadata(obj)
{:dict, 1, 16, 16, 8, 80, 48,
 {[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []},
 {{[], [], [], [], [], [], [], [], [], [],
   [["content-type", 116, 101, 120, 116, 47, 112, 108, 97, 105, 110]], [], [],
   [], [], []}}}
iex(4)> md2 = :riakc_obj.set_secondary_index(md1, [{{:binary_index, 'twitter'}, ["jsmith123"]}, {{:binary_index, 'email'}, ["jsmith@basho.com"]}])
{:dict, 2, 16, 16, 8, 80, 48,
 {[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []},
 {{[], [], [], [], [], [], [], [], [], [],
   [["content-type", 116, 101, 120, 116, 47, 112, 108, 97, 105, 110]],
   [["index", {"email_bin", "jsmith@basho.com"}, {"twitter_bin", "jsmith123"}]],
   [], [], [], []}}}
iex(5)> obj2 = :riakc_obj.update_metadata(obj, md2)
{:riakc_obj, {"default", "users"}, "john_smith", :undefined, [],
 {:dict, 2, 16, 16, 8, 80, 48,
  {[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []},
  {{[], [], [], [], [], [], [], [], [], [],
    [["content-type", 116, 101, 120, 116, 47, 112, 108, 97, 105, 110]],
    [["index", {"email_bin", "jsmith@basho.com"},
      {"twitter_bin", "jsmith123"}]], [], [], [], []}}}, "...user data..."}
iex(6)> :riakc_pb_socket.put(pid, obj2)                                                
:ok
iex(7)> {:ok, results} = :riakc_pb_socket.get_index(pid, "users", {:binary_index, 'twitter'}, "jsmith123")
{:ok, {:index_results_v1, ["john_smith"], :undefined, :undefined}}

Second Indexを追加したらもう一回metadataの中に確認する

iex(1)> {:ok, pid} = :riakc_pb_socket.start_link('*.*.*.*', 8087)                                                 [26/342]
{:ok, #PID<0.314.0>}
iex(2)> {:ok, obj} = :riakc_pb_socket.get(pid, "users", "john_smith")
{:ok,
 {:riakc_obj, "users", "john_smith",
  <<107, 206, 97, 96, 96, 96, 204, 96, 202, 5, 82, 28, 147, 3, 54, 4, 134, 237, 176, 254, 144, 193, 148, 200, 152, 199, 202, 96,
177, 39, 245, 2, 95, 22, 0>>,
  [{{:dict, 4, 16, 16, 8, 80, 48,
     {[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []},
     {{[], [], [], [], [], [], [], [], [], [],
       [["content-type", 116, 101, 120, 116, 47, 112, 108, 97, 105, 110],
        ["X-Riak-VTag", 49, 121, 85, 51, 118, 98, 98, 73, 71, 66, 113, 50, 111,
         105, 103, 50, 103, 116, 107, 67, 57, 86]],
       [["index", {"email_bin", "jsmith@basho.com"},
         {"twitter_bin", "jsmith123"}]], [],
       [["X-Riak-Last-Modified" | {1458, 651192, 145990}]], [], []}}},
    "...user data..."}], :undefined, :undefined}}

iex(3)> :riakc_obj.get_value(obj)
"...user data..."

iex(35)> :riakc_obj.key(obj)
"john_smith"

iex(37)> :riakc_obj.value_count(obj) # siblingの数を返す
1

iex(4)> :riakc_obj.get_contents(obj)
[{{:dict, 4, 16, 16, 8, 80, 48,
   {[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []},
   {{[], [], [], [], [], [], [], [], [], [],
     [["content-type", 116, 101, 120, 116, 47, 112, 108, 97, 105, 110],
      ["X-Riak-VTag", 49, 121, 85, 51, 118, 98, 98, 73, 71, 66, 113, 50, 111,
       105, 103, 50, 103, 116, 107, 67, 57, 86]],
     [["index", {"email_bin", "jsmith@basho.com"},
       {"twitter_bin", "jsmith123"}]], [],
     [["X-Riak-Last-Modified" | {1458, 651192, 145990}]], [], []}}},
  "...user data..."}]
  
iex(5)> [{md, v}] = :riakc_obj.get_contents(obj)
[{{:dict, 4, 16, 16, 8, 80, 48,
   {[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []},
   {{[], [], [], [], [], [], [], [], [], [],
     [["content-type", 116, 101, 120, 116, 47, 112, 108, 97, 105, 110],
      ["X-Riak-VTag", 49, 121, 85, 51, 118, 98, 98, 73, 71, 66, 113, 50, 111,
       105, 103, 50, 103, 116, 107, 67, 57, 86]],
     [["index", {"email_bin", "jsmith@basho.com"},
       {"twitter_bin", "jsmith123"}]], [],
     [["X-Riak-Last-Modified" | {1458, 651192, 145990}]], [], []}}},
  "...user data..."}]
  
iex(6)> v
"...user data..."

iex(29)> :dict.fetch_keys(md)
["X-Riak-VTag", "content-type", "index", "X-Riak-Last-Modified"]

iex(39)> :riakc_obj.md_ctype(md) # metadataの中のcontent typeを返す
'text/plain'

iex(40)> :riakc_obj.get_secondary_indexes(md) # indexを返す
[{{:binary_index, 'twitter'}, ["jsmith123"]},
 {{:binary_index, 'email'}, ["jsmith@basho.com"]}]

iex(7)> d = :riakc_obj.get_metadata(obj)
{:dict, 4, 16, 16, 8, 80, 48,
 {[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []},
 {{[], [], [], [], [], [], [], [], [], [],
   [["content-type", 116, 101, 120, 116, 47, 112, 108, 97, 105, 110],
    ["X-Riak-VTag", 49, 121, 85, 51, 118, 98, 98, 73, 71, 66, 113, 50, 111, 105,
     103, 50, 103, 116, 107, 67, 57, 86]],
   [["index", {"email_bin", "jsmith@basho.com"}, {"twitter_bin", "jsmith123"}]],
   [], [["X-Riak-Last-Modified" | {1458, 651192, 145990}]], [], []}}}
   
iex(9)> :dict.to_list(d)
[{"X-Riak-VTag", '1yU3vbbIGBq2oig2gtkC9V'}, {"content-type", 'text/plain'},
 {"index", [{"email_bin", "jsmith@basho.com"}, {"twitter_bin", "jsmith123"}]},
 {"X-Riak-Last-Modified", {1458, 651192, 145990}}]
 
 iex(26)> :dict.fetch_keys(d)
["X-Riak-VTag", "content-type", "index", "X-Riak-Last-Modified"]

(riak-cs@10.15.0.241)13> [dict:fetch_keys(M)||{M, V} <- Contents].
[[<<"X-Riak-VTag">>,<<"X-Riak-Last-Modified">>,
  <<"X-Riak-Meta">>]]

バケットmapsの中のキーを取得

iex(2)> :riakc_pb_socket.list_keys(pid, "maps")                                                                           
{:ok, ["map_key"]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment