Sunday, May 12, 2013

Riak Links and Link Walking with F# and CorrugatedIron

Continuing my journey through the book Seven Databases in Seven Weeks, I explore links and link walking in this blog post. Riak has the ability to establish one-way relationship between entries via Links, providing some of the capabilities of a graph database (Riak documentation calls it a lightweight graph database). Riak documentation hints that links should be kept low, on the order of dozens, not thousands. Using the Twitter example from Riak Handbook, you can probably easily find out who Don Syme follows and who does those people follows, etc. But it would be difficult to find all the people that follows Don Syme using Riak's link capability.


Adding Links

Here's how you would add links with CorrugatedIron library:

type Cage = { room : int }

(*
Linking cage 1 to polly via contains, equivalent to doing the following
curl -X PUT http://localhost:8098/riak/cages/1 \
-H "Content-Type: application/json" \
-H "Link: </riak/animals/polly>; riaktag=\"contains\"" \
-d '{"room" : 101}'
*)
client.Put(
  let cage = new RiakObject("cages","1",{room=101})
  cage.LinkTo("animals","polly","contains")
  cage)

(*
Putting ace in cage 2 and setting cage 2 next to cage 1.
Equivalent to the following sample code:
curl -X PUT http://localhost:8091/riak/cages/2 \
-H "Content-Type: application/json" \
-H "Link:</riak/animals/ace>;riaktag=\"contains\",</riak/cages/1>;riaktag=\"next_to\"" \
-d '{"room" : 101}'
*)
// Adding more than one link  
client.Put(
  let cage = new RiakObject("cages","2",{room=101})
  [("animals","ace","contains");("cages","1","next_to")]
  |> List.iter(fun (bucket,key,tag) -> cage.LinkTo(bucket,key,tag))
  cage)  


Link Walking

With CorrugatedIron, I can use the API to perform link walking and as an extra bonus, I don't have to worry about extracting data from the multipart/mixed mime types. CorrugateIron library takes care of all of that for us.

(*
Link walking, equivalent to 
curl http://riakhost1:8098/riak/cages/1/_,_,_
or in the new version:
curl http://riakhost1:8098/buckets/cages/keys/1/_,_,_
*)
 
let results = client.WalkLinks(new RiakObject("cages","1"),
                               [|new RiakLink(null,null,null)|])
         
// Dump results, which returns a list of RiakObject(s)
results.Value

// Since in this particular case, we have only one result, we can do the following
// and get back polly
results.Value.[0].GetObject<Animal>()

Here is the output result for results.Value:

// results.Value
val it : IList<RiakObject> =
  seq
    [CorrugatedIron.Models.RiakObject
       {BinIndexes = dict [];
        Bucket = "animals";
        CharSet = null;
        ContentEncoding = null;
        ContentType = "application/json";
        HasChanged = false;
        IntIndexes = dict [];
        Key = "polly";
        LastModified = 1359758822u;
        LastModifiedUsec = 523420u;
        Links = seq [];
        Siblings = seq [];
        UserMetaData = dict [];
        VTag = "2BTveSKTYDNOZNCiOxyryw";
        VTags = seq ["2BTveSKTYDNOZNCiOxyryw"];
        Value = [|123uy; 32uy; 34uy; 110uy; 105uy; 99uy; 107uy; 110uy; 97uy;
                  109uy; 101uy; 34uy; 32uy; 58uy; 32uy; 34uy; 83uy; 119uy;
                  101uy; 101uy; 116uy; 32uy; 80uy; 111uy; 108uy; 108uy; 121uy;
                  32uy; 80uy; 117uy; 114uy; 101uy; 98uy; 114uy; 101uy; 100uy;
                  34uy; 32uy; 44uy; 32uy; 34uy; 98uy; 114uy; 101uy; 101uy;
                  100uy; 34uy; 32uy; 58uy; 32uy; 34uy; 80uy; 117uy; 114uy;
                  101uy; 98uy; 114uy; 101uy; 100uy; 34uy; 32uy; 125uy|];
        VectorClock = [|107uy; 206uy; 97uy; 96uy; 96uy; 96uy; 204uy; 96uy;
                        202uy; 5uy; 82uy; 28uy; 169uy; 111uy; 239uy; 241uy;
                        7uy; 114uy; 230uy; 184uy; 103uy; 48uy; 37uy; 50uy;
                        230uy; 177uy; 50uy; 60uy; 59uy; 216uy; 112uy; 138uy;
                        47uy; 11uy; 0uy|];}]
      
// results.Value.[0].GetObject<Animal>()
val it : Animal = {nickname = "Sweet Polly Purebred";
                   breed = "Purebred";}     

There seems to be some limitations with link walking using CorrugatedIron library. There is a WalkLinks() method as part RiakClient, but the input is expecting RiakLink object which has bucket,key, tag as arguments. The link spec is expecting bucket, tag, and keep flag as arguments. I do notice that in the Riak documentation that Link Walking is not available as part of the Protocol Buffers Client (PBC) API, so I'm guessing the WalkLinks() method in CorrugatedIron is either using HTTP protocol or a modified usage of MapReduce. Since link walking is a special case of MapReduce querying, it may not matter much that CorrugatedIron has some limitations on link walking. One other issue with CorrugatedIron and link walking is that when I add multiple links with the same tag and try to link walking with CorrugatedIron, I do not get back all the links, I only get one of the links. In order to follow the examples in the book Seven Databases in Seven Weeks, I can fall back to using ASP.NET MVC Rest API.

Link walking using CorrugatedIron library:

// Link walking, equivalent to 
// curl http://localhost:8098/riak/cages/2/animals,_,_
client.WalkLinks(new RiakObject("cages","2"),
                 [|new RiakLink("animals",null,null)|])
     
// curl http://localhost:8098/riak/cages/2/_,next_to,0/animals,_,_
client.WalkLinks(new RiakObject("cages","2"),
                 [|new RiakLink(null,null,"next_to");
                   new RiakLink("animals",null,null);|])

// I can't seem to specify the keep flag with CorrugatedIron, which keeps
// intermediate results as you walk beyond primary links
// curl http://localhost:8091/riak/cages/2/_,next_to,1/_,_,_     

The book Seven Databases in Seven Weeks is still using the old format for HTTP Link Walking. The new format is as follows:

GET /riak/bucket/key/[bucket],[tag],[keep]            # Old format
GET /buckets/bucket/keys/key/[bucket],[tag],[keep]    # New format

Link walking using REST API:


let riakurl = "http://myriakhost1:8098"
let restClient = new HttpClient()

type LinkWalkSpec = 
    { bucket: string; tag: string;  keep: string; }

    member x.Link = (sprintf "%s,%s,%s" x.bucket x.tag x.keep)

let linkWalker url bucket key (links:LinkWalkSpec list) =
    let baseurl = sprintf "%s/buckets/%s/keys/%s" url bucket key
    let rec buildLinkWalkUrl (linklist:LinkWalkSpec list) baseUrl =
      match linklist with
      | [] -> baseUrl
      | [x] -> sprintf "%s/%s" baseUrl (x.Link)
      | h::t -> let newUrl = sprintf "%s/%s" baseUrl (h.Link)
                buildLinkWalkUrl t newUrl

    buildLinkWalkUrl links baseurl
    |> restClient.GetStringAsync

// Equiv to : curl http://localhost:8091/riak/cages/2/_,next_to,1/_,_,_  
   
[{bucket="_";tag="next_to";keep="1"};{bucket="_";tag="_";keep="_"}] 
|> linkWalker riakurl "cages" "2" 

Link walking results from using REST API:

val it : Task<string> =
  System.Threading.Tasks.Task`1[System.String]
    {AsyncState = null;
     CreationOptions = None;
     Exception = null;
     Id = 1;
     IsCanceled = false;
     IsCompleted = false;
     IsFaulted = false;
     Result = "
--CveXyss6PAqBxOOWxeWBCf6eXii
Content-Type: multipart/mixed; boundary=AvYXKrJYDlkeNxh1bQyqDvBAuBF

--AvYXKrJYDlkeNxh1bQyqDvBAuBF
X-Riak-Vclock: a85hYGBgzGDKBVIcqW/v8Qdy5rhnMCUy5rEy9Oi+P8WXBQA=
Location: /buckets/cages/keys/1
Content-Type: application/json
Link: </buckets/animals/keys/polly>; riaktag="contains", </buckets/cages>; rel="up"
Etag: 6gyXgkIgzvwBGRRotqHK3b
Last-Modified: Fri, 26 Apr 2013 16:55:40 GMT

{"room":101}
--AvYXKrJYDlkeNxh1bQyqDvBAuBF--

--CveXyss6PAqBxOOWxeWBCf6eXii
Content-Type: multipart/mixed; boundary=SaWqmmho48dzhMDmJy3BVcCWrzu

--SaWqmmho48dzhMDmJy3BVcCWrzu
X-Riak-Vclock: a85hYGBgzGDKBVIcqW/v8Qdy5rhnMCUy5rEyPDvYcIovCwA=
Location: /buckets/animals/keys/polly
Content-Type: application/json; charset=utf-8
Link: </buckets/animals>; rel="up"
Etag: 2BTveSKTYDNOZNCiOxyryw
Last-Modified: Fri, 01 Feb 2013 22:47:02 GMT

{ "nickname" : "Sweet Polly Purebred" , "breed" : "Purebred" }
--SaWqmmho48dzhMDmJy3BVcCWrzu--

--CveXyss6PAqBxOOWxeWBCf6eXii--
";
     Status = RanToCompletion;}      

22 comments:

Anonymous said...

Hi John,

This is another nice post. Well done. I have a just a couple of comments:

I can't seem to specify the keep flag with CorrugatedIron, which keeps intermediate results as you walk beyond primary links.

The reason for this is because CI uses the PBC interface wherever possible and the PBC interface does not support the "keep" parameter. You have to walk the links yourself. The HTTP interface has some extra magic in it to allow for that.

new RiakLink(null,null,null)

This can be replaced with RiakLink.AllLinks to make this a little more intuitive (I hope!).

Thank you again for your posts!
OJ

ganga said...

I simply wanted to write down a quick word to say thanks to you for those wonderful tips and hints you are showing on this site.r-programming Training in marathahalli

r-programming Training in rajajinagar

r-programming Training in bangalore

Rprogramming Training in velachery

Rprogramming online Training

Rprogramming Training

creative web solution said...

In This Summers get the best designer umbrellas for you or for your family members we allso deals in wedding umbrellas and in advertising umbrellas For more info visit links given bellow
UMBRELLA WHOLESALERS IN DELHI
FANCY UMBRELLA DEALERS
CORPORATE UMBRELLA MANUFACTURER
BEST CUSTOMIZED UMBRELLA
FOLDING UMBRELLA DISTRIBUTORS
DESIGNER UMBRELLA
GOLF UMBRELLA DEALERS/MANUFACTURERS
TOP MENS UMBRELLA
LADIES UMBRELLA DEALERS
WEDDING UMBRELLA DEALERS
BEST QUALITY UMBRELLA
BIG UMBRELLA
Top Umbrella Manufacturers in India
Umbrella Manufacturers in Mumbai
Umbrella Manufacturers in Delhi
Garden Umbrella Dealers
Garden Umbrella Manufacturers
PROMOTIONAL UMBRELLA DEALERS IN DELHI/MUMBAI
PROMOTIONAL UMBRELLA MANUFACTURERS IN DELHI / MUMBAI
ADVERTISING UMBRELLA MANUFACTURERS

creative web solution said...

In This Summers get the best designer umbrellas for you or for your family members we allso deals in wedding umbrellas and in advertising umbrellas For more info visit links given bellow
UMBRELLA WHOLESALERS IN DELHI
FANCY UMBRELLA DEALERS
CORPORATE UMBRELLA MANUFACTURER
BEST CUSTOMIZED UMBRELLA
FOLDING UMBRELLA DISTRIBUTORS
DESIGNER UMBRELLA
GOLF UMBRELLA DEALERS/MANUFACTURERS
TOP MENS UMBRELLA
LADIES UMBRELLA DEALERS
WEDDING UMBRELLA DEALERS
BEST QUALITY UMBRELLA
BIG UMBRELLA
Top Umbrella Manufacturers in India
Umbrella Manufacturers in Mumbai
Umbrella Manufacturers in Delhi
Garden Umbrella Dealers
Garden Umbrella Manufacturers
PROMOTIONAL UMBRELLA DEALERS IN DELHI/MUMBAI
PROMOTIONAL UMBRELLA MANUFACTURERS IN DELHI / MUMBAI
ADVERTISING UMBRELLA MANUFACTURERS

creative web solution said...

We are the one of the top blue art pottery manufacturers in jaipur get contact us and get all informations in detail visit our site
blue pottery jaipur
blue pottery shop in jaipur
blue pottery manufacturers in jaipur
blue pottery market in jaipur
blue pottery work shop in jaipur
blue pottery
top blue pottery in jaipur
blue pottery wholesale in jaipur

creative web solution said...

We are the one of the top blue art pottery manufacturers in jaipur get contact us and get all informations in detail visit our site
blue pottery jaipur
blue pottery shop in jaipur
blue pottery manufacturers in jaipur
blue pottery market in jaipur
blue pottery work shop in jaipur
blue pottery
top blue pottery in jaipur
blue pottery wholesale in jaipur

creative web solution said...

Rihan electronics is one of the best repairing service provider all over india we are giving our service in many different different cities like Noida,Gazibad,Delhi,Delhi NCR

AC Repair in NOIDA
Refrigerator Repair Gaziabad
Refrigerator repair in NOIDA
washing machine repair in Delhi
LED Light Repair in Delhi NCR
plasma TV repair in Gaziyabad
LCD TV Repair in Delhi NCR
LED TV Repair in Delhi

creative web solution said...

Get the best furniture manufacturers in delhi ncr for more info visit our site
top Sofa Manufacturers in Delhi
Sofa set manufacturers in Delhi
Best Furniture dealers in Delhi
Furniture repairing in Delhi
Sofa repairing in Delhi
designed furniture manufacturers in delhi
Best bed repairing in Delhi
Best furniture manufacturing in Delhi NCR
headboard manufacturing services
Best headboard manufacturing  manufacturers in Delhi
 Home Furniture repairing in Delhi

creative web solution said...

Get the best and cheapest sound systems led wals lfd screen and more other components on rent for more info visit our site
Projector on rent
4K LED TV on rent
Sound system on rent
LED Wall on rent
LFD Video wall on rent
Touch Screen Monitor on rent
LFD Video wall for exhibition
Projector and screen
Professional Sound System
LED display wall on rent

creative web solution said...

We are the one of top rated service provider in construction and contractor Fore more info visit our site

Best interior decorators in manesar
Best civil contractors in delhi
Best building contractor in gurugram
Top builder in gurugram
Top builder in Noida
Best Interior decorators in delhi NCR
Best office interior decorators in gurugram
Construction finishing in delhi NCR
Residential construction in delhi NCR

creative web solution said...

We are the best service provider for the ads and posters on auto or wall paints for all informations visit our site

Rural wall painting advertising
Auto rickshaw ad in delhi
Advertising wall painting
Auto Rickshaw advertising
Digital wall painting
Hoardings in Delhi
Wall painting advertising
Outdoor advertising
Outdoor Advertising Companies in Delhi
Hoarding advertising

creative web solution said...

We are the best service provider for the ads and posters on auto or wall paints for all informations visit our site

Rural wall painting advertising
Auto rickshaw ad in delhi
Advertising wall painting
Auto Rickshaw advertising
Digital wall painting
Hoardings in Delhi
Wall painting advertising
Outdoor advertising
Outdoor Advertising Companies in Delhi
Hoarding advertising

raju said...

we are the best service provider...
dominican republic web hosting
iran hosting
palestinian territory web hosting
panama web hosting
syria hosting
services hosting
afghanistan shared web hosting
andorra web hosting
belarus web hosting
brunei darussalam hosting

shree said...

nice..
luxembourg web hosting
mauritius web hosting mongolia web hosting
namibia web hosting
norway web hosting
rwanda web hosting
spain hosting
turkey web hosting
venezuela hosting
vietnam shared web hosting

shri said...

very nice.....
internship in chennai for ece students
internships in chennai for cse students 2019
Inplant training in chennai
internship for eee students
free internship in chennai
eee internship in chennai
internship for ece students in chennai
inplant training in bangalore for cse
inplant training in bangalore
ccna training in chennai


anuarun said...

ree internship in bangalore for computer science students
internship for aeronautical engineering
internship for eee students in hyderabad
internship in pune for computer engineering students 2018
kaashiv infotech internship fees
industrial training certificate format for mechanical engineering students
internship report on machine learning with python
internship for biomedical engineering students in chennai
internships in bangalore for cse
internship in coimbatore for ece

shree said...

excellent
kyrgyzstan web hosting
lebanon web hosting
lithuania web hosting
macao hosting
madagascar web hosting
pakistan hosting
panama shared web hosting
paraguay web hosting
peru web hosting
philippines hosting

sibiselvan said...


it is best blogs for you...!!!
paid internships in pune for computer science students
machine learning training in chennai
data science internship in chennai
dot net training in chennai
kaashiv infotech chennai
internship for aeronautical engineering students in india
internship in automobile industry
big data internship in chennai
machine learning internship in chennai
internship in chennai for it students

KUTTYMA said...

IT IS A BEST ONE FOR SEARCHING....
kaashiv infotech internship in bangalore
internship for ece
mba internship
final year project proposal for information technology
internships in chennai for ece students
companies for industrial visit in chennai for cse students
internship in bangalore for eee
internship in chennai
inplant training certificate format for mechanical engineering
internship for aeronautical engineering students in chennai

anuarun said...

Good information
kaashiv infotech internship fees
internship
internship in chennai for cse students
internships in hyderabad for eee students 2019
inplant training for cse
internship for ece students in coimbatore
internship for cse
internship for cse 3rd year students
inplant training in chennai for ece students
summer training for 3rd year electronics and communication engineering students

nivetha said...

hiiii hlo this is gud.....
internships for cse students in bangalore
internship for cse students
industrial training for diploma eee students
internship in chennai for it students
kaashiv infotech in chennai
internship in trichy for ece
inplant training for ece
inplant training in coimbatore for ece
industrial training certificate format for electrical engineering students
internship certificate for mechanical engineering students

Unknown said...

This was a useful post and I think it's fairly easy to see in the other reviews, so this post is well written and useful. Keep up the good work.
Home Furniture Manufacturers in South Delhi
Home Furniture Manufacturers in Delhi NCR