Neo4J OGM与Quarkus
在下面的視頻中,我演示了一個(gè)使用Neo4J數(shù)據(jù)庫和Neo4J OGM的Quarkus應(yīng)用程序示例。
看一下GitHub上的示例項(xiàng)目 。
我為咖啡豆創(chuàng)建了一個(gè)示例域,其中包含我們可以查詢和匹配的某些風(fēng)味配置文件。 紅色節(jié)點(diǎn)是來自某個(gè)國(guó)家/地區(qū)的咖啡豆,味道像某些口味。 豆類起源國(guó)也以“某些風(fēng)味”而聞名。
在當(dāng)前的1.4.2.Final版本中,Quarkus帶有基本的Neo4J支持 ,但是在撰寫本文時(shí)還沒有包括對(duì)OGM映射的支持。 但是,我們可以使用一個(gè)簡(jiǎn)單的生產(chǎn)者添加支持,該生產(chǎn)者公開Neo4J OGM SessionFactory :
import org.neo4j.ogm.config.Configuration; import org.neo4j.ogm.session.SessionFactory; ... @ApplicationScoped public class SessionFactoryProducer { public static final String PACKAGE = "com.sebastian_daschner.coffee.entity" ; @ConfigProperty (name = "quarkus.neo4j.uri" ) String databaseUri; @ConfigProperty (name = "quarkus.neo4j.authentication.username" ) String username; @ConfigProperty (name = "quarkus.neo4j.authentication.password" ) String password; @Produces SessionFactory produceSessionFactory() { Configuration neoConfig = new Configuration.Builder() .uri(databaseUri) .credentials(username, password) .useNativeTypes() .build(); return new SessionFactory(neoConfig, PACKAGE); } void disposeSessionFactory( @Disposes SessionFactory sessionFactory) { sessionFactory.close(); } }現(xiàn)在,我們可以將SessionFactory注入我們的bean中,并使用它來查詢圖形數(shù)據(jù)庫:
import org.neo4j.ogm.session.*; ... @ApplicationScoped public class CoffeeBeans { @Inject SessionFactory sessionFactory; public List<CoffeeBean> getCoffeeBeans() { Session session = sessionFactory.openSession(); return new ArrayList<>(session.loadAll(CoffeeBean. class , new SortOrder( "name" ), 1 )); } public List<CoffeeBean> getCoffeeBeansSpecificFlavor(String flavor) { Session session = sessionFactory.openSession(); Iterable<CoffeeBean> result = session.query(CoffeeBean. class , "MATCH (b:CoffeeBean)-[:TASTES]->(:Flavor {description: $flavor})\n" + "MATCH (b)-[isFrom:IS_FROM]->(country)\n" + "MATCH (b)-[tastes:TASTES]->(flavor)\n" + "RETURN b, collect(isFrom), collect(country)," + " collect(tastes), collect(flavor)\n" + "ORDER by b.name;" , Map.of( "flavor" , flavor)); return resultList(result); } ... }我的示例圖如下所示,其中CoffeeBean對(duì)象是通過Neo4J OGM映射的:
import org.neo4j.ogm.annotation.*; ... @NodeEntity public class CoffeeBean { @Id public String name; @Relationship ( "IS_FROM" ) public Set<Origin> origins = new HashSet<>(); @Property public Roast roast; @Relationship ( "TASTES" @Relationship "TASTES" ) public Set<FlavorProfile> flavorProfiles = new HashSet<>(); ... }觀看視頻,并查看GitHub上的項(xiàng)目以獲取完整圖片。
玩得開心探索您的圖表! 作為進(jìn)一步的練習(xí),您可以編寫查詢來匹配具有某種風(fēng)味的咖啡豆作為其“主要”風(fēng)味(百分比最高的咖啡豆)或具有相似風(fēng)味特征的咖啡豆等。
翻譯自: https://www.javacodegeeks.com/2020/05/neo4j-ogm-with-quarkus.html
總結(jié)
以上是生活随笔為你收集整理的Neo4J OGM与Quarkus的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux挂载u盘命令(linux 挂载
- 下一篇: 进浙江施工备案流程(进浙江施工备案)