我正在撰寫一個資料庫配接器以在 Spring Boot 中使用 Amazon Neptune 資料庫。我想回傳自定義資料型別而不是 Vertex,或者在搜索案例中回傳 Vertex 串列。因此,當我執行搜索時,我必須將找到的頂點映射到我的自定義類。
對于我正在進行的步驟(只是將頂點資料寫入控制臺),我只需要獲取每個節點的 ID、標簽和屬性,但我總是得到空屬性。
我有這個示例圖:

我使用 key = "city", value = "Malaga" 呼叫該方法。這將回傳兩個節點。
方法一:
public void queryNodesByProperty(String key, String value) throws Exception {
if(key == null || key.trim().length() == 0) throw new Exception("Key cannot be empty");
List<TreeNode> result = g.V().has(key,value).toList().stream().map(e->{
System.out.println(e.id()); //works
System.out.println(e.label()); //works
System.out.println(e.properties()); //returns java.util.Collections$EmptyIterator
System.out.println(e.property("name")); //returns empty
System.out.println(e.properties().next()); //Throws NoSuchElementException
//I will call my mapper here
return TreeNodeMapper.getTreeNode(e);
}).collect(Collectors.toList());
}
方法2:現在,我只是嘗試輸出資料
public void queryNodesByProperty(String key, String value) throws Exception {
if(key == null || key.trim().length() == 0) throw new Exception("Key cannot be empty");
Stream<Vertex> s = g.V().has(key,value).toStream();
s.forEach(v -> {
System.out.println(v.id()); //works
System.out.println(v.label()); //works
System.out.println(v.property("city")); //returns vp[empty]
System.out.println(v.keys()); //returns []
});
}
方法三:只輸出資料
public void queryNodesByProperty(String key, String value) throws Exception {
if(key == null || key.trim().length() == 0) throw new Exception("Key cannot be empty");
List <Vertex> l = g.V().has(key,value).toList();
l.forEach(v -> {
System.out.println(v.id()); //works
System.out.println(v.label()); //works
System.out.println(v.property("city")); //returns vp[empty]
System.out.println(v.keys()); //returns []
});
}
任何幫助表示贊賞。謝謝!
**更新**
正如@taylor-riggan 和@kelvin-lawrence 建議的那樣,我更新了代碼以迭代 elementMap() 的結果,而不是嘗試迭代頂點集合。
List<Map<Object,Object>> ln = g.V().has(key,value).elementMap().toList();
System.out.println(ln);
ln.forEach(ve->{
System.out.println(ve.get("id"));
System.out.println(ve.get("label"));
System.out.println(ve.get("area"));
});
這輸出:
[{id=30c21e3b-31fa-2e27-43b5-7ea2e78545b6, label=Person, name=Peter, town=Marbella, city=Malaga, area=Andalucia}, {id=b4c21e3b-32a8-972a-8623-26fa165e63e3, label=Person, name=Victor, town=Torremolinos, city=Malaga, area=Andalucia}]
null
null
Andalucia
null
null
Andalucia
正如您所說,每個元素中都存在 id 和 label ,但是當我嘗試獲取該值時,我仍然得到 null 。這就是我嘗試迭代頂點的原因。
當我除錯代碼并檢查地圖中的元素時,我看到了這一點:

所以 id 和 label 鍵在某種程度上是“特殊的”,不能使用 .get("id")
我怎樣才能得到這些值?
謝謝
uj5u.com熱心網友回復:
目前,從 3.6.x 開始,不會在圖形元素上回傳屬性,因此您必須使用 、 等步驟將 a或aVertex轉換Edge為 a 。如果您有興趣,此處描述了此設計決策的原因和歷史。Mapproject()elementMap()
要從 a 獲取 ID 和標簽Map,請注意這些值的鍵不是String型別T,因此您希望分別以T.id或訪問它們T.label。
最后,目前正在進行 3.7.x 的作業,以允許您對圖形元素中回傳的屬性進行整形,以便最終您可以直接使用 Vertex 和 Edge 實體及其屬性。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/527353.html
