Neo4j:遍历查询超时
生活随笔
收集整理的這篇文章主要介紹了
Neo4j:遍历查询超时
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在過去的幾周中,我一直在花一些業余時間來創建一個應用程序,該應用程序從Open Roads數據生成運行路線-當然已轉換并導入到Neo4j中!
我創建了一個用戶定義的過程,該過程結合了幾個最短路徑查詢,但是如果它們花費的時間太長,我想退出所有這些最短路徑搜索。 我的代碼沒有超時看起來像這樣:
StandardExpander orderedExpander = new OrderedByTypeExpander().add( RelationshipType.withName( "CONNECTS" ), Direction.BOTH );PathFinder<Path> shortestPathFinder = GraphAlgoFactory.shortestPath( expander, 250 );...我們可以在幾個地方檢查經過的時間,但是對我來說, 擴展器中的expand方法似乎很明顯。 我編寫了自己的Expander類,如下所示:
public class TimeConstrainedExpander implements PathExpander {private final StandardExpander expander;private final long startTime;private final Clock clock;private int pathsExpanded = 0;private long timeLimitInMillis;public TimeConstrainedExpander( StandardExpander expander, Clock clock, long timeLimitInMillis ){this.expander = expander;this.clock = clock;this.startTime = clock.instant().toEpochMilli();this.timeLimitInMillis = timeLimitInMillis;}@Overridepublic Iterable<Relationship> expand( Path path, BranchState state ){long timeSoFar = clock.instant().toEpochMilli() - startTime;if ( timeSoFar > timeLimitInMillis ){return Collections.emptyList();}return expander.expand( path, state );}@Overridepublic PathExpander reverse(){return expander.reverse();} }現在需要更新較早版本的代碼片段,以使用我們的新類,這不太麻煩:
StandardExpander orderedExpander = new OrderedByTypeExpander().add( RelationshipType.withName( "CONNECTS" ), Direction.BOTH );TimeConstrainedExpander expander = new TimeConstrainedExpander(orderedExpander, Clock.systemUTC(), 200);PathFinder<Path> shortestPathFinder = GraphAlgoFactory.shortestPath( expander, 250 ); ...我不確定這是否是實現我想要的最佳方法,但是在其他幾種方法失敗之后,至少這種方法確實有效!
翻譯自: https://www.javacodegeeks.com/2017/11/neo4j-traversal-query-timeout.html
總結
以上是生活随笔為你收集整理的Neo4j:遍历查询超时的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 华为路由Q6评测华为路由器 如何分
- 下一篇: 记录意外的开关选项