Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
tree: cb93594abb
Fetching contributors…

Cannot retrieve contributors at this time

132 lines (89 sloc) 4.519 kB

Importing an Erlang digraph

Contributed By: [[Ryan Maclear|https://github.com/ryanmaclear]]

[[Source File on GitHub|https://github.com/basho/riak_function_contrib/blob/master/other/erlang/digraph_importer.erl]]

This Erlang function allows you to import an Erlang digraph into a bucket. Each vertex corresponds to an object, with the key being assigned the vertex, and the value being assigned the vertex label. Edges are mapped to links on the corresponding object. Edge labels are ignored. A content type is required for the label. If the label contains erlang terms, use a content type of application/x-erlang-binary.

The digraphs imported can be recreated using [[Digraph Exporter|digraph_exporter]] found elsewhere on this site.

The function takes 5 arguments:
digraph_importer:import_digraph(address of server, port, bucket, digraph, content-type).

Notes

(Note: The cluster used for the example below is the dev cluster used in the Fast Track, so the port number used is 8082, and not the default port of 8087).

The function uses the Erlang Protocol Buffer client, so the correct ip address and port must be specified.

There are currently some restrictions regarding the construction of the digraph:

  1. The vertices should be created with digraph:add_vertex/2 or digraph:add_vertex/3
  2. and the edges need to be created with digraph:add_edge/5

Examples

A simple example where the vertex labels are json objects:

```erlang
D = digraph:new([cyclic, protected]).

JoeLabel = “{\\”name\\“:\\”Joe\\“,
\\”surname\\“:\\”Blogs\\“}”.
FredLabel = “{\\”name\\“:\\”Fred\\“,
\\”surname\\“:\\”Blogs\\“,
\\”born\\“:\\”3 January 1910\\“}”.

digraph:add_vertex(D, “joe”, JoeLabel).
digraph:add_vertex(D, “fred”, FredLabel).
digraph:add_edge(D, “son”, “joe”, “fred”, []).
digraph:add_edge(D, “father”, “fred”, “joe”, []).

digraph_importer:import_digraph(“127.0.0.1”, 8082, <<"family">>, D, “application/json”).
```

The same example where the vertex labels are erlang terms:

```erlang
D = digraph:new([cyclic, protected]).

JoeLabel = [{name, "Joe"},
{surname, "Blogs"}].
FredLabel = [{name, "Fred"},
{surname, "Blogs"},
{born, “3 January 1910”}].

digraph:add_vertex(D, “joe”, JoeLabel).
digraph:add_vertex(D, “fred”, FredLabel).
digraph:add_edge(D, “son”, “joe”, “fred”, []).
digraph:add_edge(D, “father”, “fred”, “joe”, []).

digraph_importer:import_digraph(“127.0.0.1”, 8082, <<"family">>, D, “application/x-erlang-binary”).
```

Source

```erlang
-module(digraph_importer).

-export([import_digraph/5]).

% @spec import_digraph(Server :: ip_address(),
% Port :: integer(),
% Bucket :: bucket(),
% Ref :: digraph(),
%% ContentType :: string()) → ok.

import_digraph(Server, Port, Bucket, Ref, ContentType) →
{ok, Client} = riakc_pb_socket:start(Server, Port),

Vertices = digraph:vertices(Ref), Edges = digraph:edges(Ref), VTupleList = [{Vertex, []} || Vertex <- Vertices], MappedVTupleList = map_edges(VTupleList, Edges, Ref, Bucket), lists:foreach(fun(VTuple) → load_data(Client, VTuple, Ref, Bucket, ContentType) end, MappedVTupleList).

map_edges(VTuple, [], _Ref, _Bucket) → VTuple;

map_edges(VTuple, [H|T], Ref, Bucket) →
{Edge, Src, Dest, } = digraph:edge(Ref, H),
{value, {
, Links}} = lists:keysearch(Src, 1, VTuple),

NewLinks = Links++[{{Bucket, to_binary(Dest)}, to_binary(Edge)}], NewVtuple = lists:keyreplace(Src, 1, VTuple, {Src, NewLinks}), map_edges(NewVtuple, T, Ref, Bucket).

load_data(Client, {Vertex, LinkList}, Ref, Bucket, ContentType) →
{_, Label} = digraph:vertex(Ref, Vertex),

Key = to_binary(Vertex), Value = if ContentType == “application/x-erlang-binary” → term_to_binary(Label); true → to_binary(Label) end, Metadata = dict:from_list([{<<“content-type”>>, ContentType}, {<<"Links">>, LinkList}]), Object = riakc_obj:new(to_binary(Bucket), Key, Value), MDObject = riakc_obj:update_metadata(Object, Metadata), riakc_pb_socket:put(Client, MDObject), io:format(“Vertex: ~p~n”, [Key]).

to_binary(Item) when is_atom(Item) →
list_to_binary(atom_to_list(Item));
to_binary(Item) when is_list(Item) →
list_to_binary(Item);
to_binary(Item) when is_binary(Item) →
Item.
```

Jump to Line
Something went wrong with that request. Please try again.