2017-08-10 81 views
2

我是gremlin的新手,並試圖瞭解如何使用Azure Cosmos DB與GraphSON在同一結果中獲取文章以及作者和附件。在tinkerpop3中輸出相同查詢中的頂點和相鄰頂點

My圖表看起來是這樣的:

[User] <- (edge: author) - [Article] - (edge: attachments) -> [File1, File2] 

我想取我需要的一切在UI顯示與作者和有關要求的附件信息一起的文章。

我試圖獲取與此類似僞代碼的東西:

{ 
article: {...}, 
author: [{author1}], 
attachment: [{file1}, {file2}] 
} 

我試圖至今:

g.V().hasLabel('article').as('article').out('author', 'attachments').as('author','attachments').select('article', 'author', 'attachments') 

我怎樣才能編寫查詢,以獲得不同的值?

+0

正如您所提到的,您正在嘗試獲取與給定的僞代碼類似的內容。所以你需要結果完全相同的格式,或者你希望結果只是4個對象,包括文章,作者,文件1,文件2(不同的值)? –

回答

4

當問小鬼總是有幫助的,以這樣的形式提供了一些樣本數據的問題:

g.addV('user').property('name','jim').as('jim'). 
    addV('user').property('name','alice').as('alice'). 
    addV('user').property('name','bill').as('bill'). 
    addV('article').property('title','Gremlin for Beginners').as('article'). 
    addV('file').property('file','/files/a.png').as('a'). 
    addV('file').property('file','/files/b.png').as('b'). 
    addE('authoredBy').from('article').to('jim'). 
    addE('authoredBy').from('article').to('alice'). 
    addE('authoredBy').from('article').to('bill'). 
    addE('attaches').from('article').to('a'). 
    addE('attaches').from('article').to('b').iterate() 

請注意,我修改您的邊緣標籤名稱更動詞等,以使他們脫穎而出更好地使用名詞式頂點標籤。它往往與邊緣的方向很好讀,如:article --authoredBy-> user

無論如何,你的問題是最容易與project() step解決:

gremlin> g.V().has('article','title','Gremlin for Beginners'). 
......1> project('article','authors','attachments'). 
......2>  by(). 
......3>  by(out('authoredBy').fold()). 
......4>  by(out('attaches').fold()) 
==>[article:v[6],authors:[v[0],v[2],v[4]],attachments:[v[10],v[8]]] 

在上面的代碼中,注意內使用fold()by()步驟 - 這將強制內部遍歷的全部迭代並將其置於列表中。如果你錯過了這一步,你只會得到一個結果(即第一個結果)。

更進一步,我添加了valueMap()並next'd結果,以便您可以更好地查看上面頂點中包含的屬性。

gremlin> g.V().has('article','title','Gremlin for Beginners'). 
......1> project('article','authors','attachments'). 
......2>  by(valueMap()). 
......3>  by(out('authoredBy').valueMap().fold()). 
......4>  by(out('attaches').valueMap().fold()).next() 
==>article={title=[Gremlin for Beginners]} 
==>authors=[{name=[jim]}, {name=[alice]}, {name=[bill]}] 
==>attachments=[{file=[/files/b.png]}, {file=[/files/a.png]}] 
+0

非常感謝您花時間解釋和發表評論。也感謝您指出如何提供示例。 :) –

相關問題