One. Installation
mkdir Cayley
CD Cayley
mkdir SRC
Export gopath=$ (PWD)
Go get Github.com/google/cayley
Go Build Src/github.com/google/cayley/cayley.go
Which is missing what package under what package, no HG tool installed HG
Modified under source Cayley.go
Switchcmd { Case "Init": Db. Init (CFG,*triplefile) Case "Load": ts, _ = db. Open (CFG) db. Load (ts, CFG,*triplefile) Ts. Close () Case "Repl": ts, _ = db. Open (CFG) db. REPL (TS,*querylanguage, CFG) ts. Close () Case "http": ts, _: = db. Open (CFG) http. Serve (ts, cfg) ts. Close ()default: FMT. Println ("No Command", CMD) flag. Usage ()}
Run
Go build $GOPATH/src/github.com/google/cayley/cayley.go &&./cayley http--port=8080--assets= $GOPATH/src/ Github.com/google/cayley--dbpath=src/testdata.nt
The assets parameter represents the directory in which HTML static resources are stored after the HTTP server is started, with its own source.
DBPath is a database of some initialization data, must be specified, otherwise started, can not add data, default is designated as/tmp/testdb file
In the browser input http://127.0.0.1:8080/if there is a page output description succeeded
Two. Basic concepts
Testdata.nt content is as follows
is is ' not cool ' is "notcool" .
Each line of content is a space-delimited four-tuple, each line is called a triple, the storage of multiple triple composed of Triplestore, each triple consists of four parts, the corresponding data each line separated by a space, respectively, called Subject,predicate, Object,provenance. Corresponding to the Chinese language, subject is the subject in Chinese, predicate is the predicate in Chinese, object is the object, provenance is the source. In other words, each line describes, who how the XX, or who is xx kind of. The subject is converted to a vertex in the graph, object is the vertex of the degree, and predicate is the path.
Cayley a set of diagram database framework, the official provides three kinds of storage memory,leveldb,mongodb can switch the storage engine, just to implement the interface, you can expand the storage mode, and MySQL and InnoDB relationship is similar.
Three. Using API1. G.V ()
Takes a vertex in a graph, that is, subject in triple, returns the object of a point
2. Path. Out (
[predicatePath], [tags]
)
Out is the degree to which a vertex is taken. However, this out-of-order is differentiated by predicates, when out () does not pass parameters, is to take out a vertex does not follow the path of all the degrees, when the predicatePath
argument is passed out, is to take out a vertex, on a path of all the degrees. Tags are the names used to store paths. For example:
In our inbound data, we take Alice's vertex as an example,
is cool
You can see that Alice has two paths to this vertex, namely follows and is
(1) Query the allice of all the degrees
G.V ("Alice"). All ()
(2) Query Alice's attention:
G.V ("Alice"). Out ("follows"). All ()
(3) query whether Allice is cool
G.V ("Alice"). Out ('is'
(4) Query Alice's attention and whether it is cool
G.V ("Alice"). Out (["follows", "is"]). All ()
" result " : [{ id ": " bob " id : " Cool
(5) Although you can intuitively see that Alice's concern is Bob, and Alice is a cool person, that is because it is through the literal meaning, for example, some people follows is empty, some people are empty, it is not possible to determine the degree of return on which path, this time should use the tag parameter
G.V ("Alice"). Out (["follows", "is""path") . All ()
3. Path. In (
[predicatePath], [tags]
)
In contrast to out, it is the degree of seeking.
(1) Ask all the cool people
G.V ("cool"). In ('is'). All ()
(2) Alice's fans
G.V ("Alice"). In ("follows"). All ()
4. Path. Both (
[predicatePath], [tags]
)
In and out of the result of the set, no go-heavy
5. Path. Has (
predicate, object
)
Reverse lookup, Paredicate is the path, object is the object in the ternary group
(1) Find Alice's fans
G.V (). Has ("follows" "Alice"). All ()
6.path. Follow (morphism)
Accelerating through the pipeline
G.V (). Has ("name", "Casablanca"). Out ("/film/film/starring"). Out ("/film/performance/actor"). Out ("name"). All ()
Equivalent to
var filmtoactor = G.morphism (). Out ("/film/film/starring"). Out ("/film/performance/actor")
G.V (). Has ("name", "Casablanca"). Follow (Filmtoactor). Out ("name"). All ()
The overall query pattern is, select Vertex, select path, all output