let me show you a demo.
step 1. prepare data.
{
set {
# friends
_:a <friend> _:b .
_:b <friend> _:c .
_:c <friend> _:d .
_:a <friend> _:d .
_:a <name> "Alice" .
_:b <name> "Bob" .
_:c <name> "Tom" .
_:d <name> "Mallory" .
# family
_:f <name> "FlyBird" .
_:h <name> "Hellen" .
_:f <family> _:a .
_:h <family> _:b .
}
}
step 2. hack the source code of dgraph,and build.
I do this step in my own repo. we talk about it later.
step 3. run a query
3.1 louvain it’s a method for community detection
query x{
q (func: has(name)) {
name
label : math(louvain(friend,family)) # means build edges via friend family
}
}
the response is this:
{
"extensions": {
"server_latency": {
"parsing_ns": 15087,
"processing_ns": 42974591,
"encoding_ns": 957638
},
"txn": {
"start_ts": 13
}
},
"data": {
"q": [
{
"name": "FlyBird",
"label": 0
},
{
"name": "Hellen",
"label": 1
},
{
"name": "Alice",
"label": 0
},
{
"name": "Bob",
"label": 1
},
{
"name": "Tom",
"label": 2
},
{
"name": "Mallory",
"label": 2
}
]
}
}
3.2 pagerank
query x{
q (func: has(name)) {
name
rank : math(pagerank(friend,family)) # means build edges via friend family
}
}
response:
{
"extensions": {
"server_latency": {
"parsing_ns": 18284,
"processing_ns": 47249353,
"encoding_ns": 866556
},
"txn": {
"start_ts": 17
}
},
"data": {
"q": [
{
"name": "FlyBird",
"rank": 0.070086
},
{
"name": "Hellen",
"rank": 0.070086
},
{
"name": "Alice",
"rank": 0.129653
},
{
"name": "Bob",
"rank": 0.184756
},
{
"name": "Tom",
"rank": 0.227142
},
{
"name": "Mallory",
"rank": 0.318277
}
]
}
}
ok,now , what I’m facing…
I do my own hack in “parser” ,and the louvain
,pagerank
function inject as a math function .
Question 1:
Is it a good or graceful way to put this kind of functions in math()
block?
pagerank,louvain is all about graph computing. not the common math as we familar (+,-,*,/,sin,cos ,…).
Question 2:
the grammar, or the query we write is in this pattern.
(partA) {
partB
}
In partA, what you do is filter and get all uids
you need.
for example: q (func: has(name)) { # I use has name to get all matched uids. }
In partB, what you do is apply functions with all uids
for example:
(partA) {
name
# I apply pagerank function to all uids I selected.
rank : math(pagerank(friend,family))
}
Is this struct (partA) {partB}
a good or graceful or nature way for applying graph-computing functions ?
PS:
I build a image in docker.
use the docker-compose file you can quickly have a test.
docker-compose-pagerank.yaml (641 Bytes)
docker-comose -f docker-compuse-pagerank.yaml up