neo4j graph db를 닷넷에서 이용하는 방법을 먼저 알아본후
액터 메시지큐에 그래프db이벤트를 발생시켜, 메시지큐를 통해 이벤트를 추가하는 방법을 알아보겠습니다.
사전셋팅 StandAlone Neo4j
docker-compose를 통해 로컬환경구축이 가능하며, neo4j 프로토콜을 사용하여 초기에 로그인을해주면
사용준비가 끝나게됩니다.
version: '2' services: neo4j: image: bitnami/neo4j:latest ports: - '7474:7474' - '7473:7473' - '7687:7687'
- 초기계정 : neo4j / bitnami
- Neo4j Browser : http://localhost:7474
- Neo4j DB : neo4j://localhost:7687
셋팅
의존모듈
<PackageReference Include="Neo4jClient" Version="4.1.14" /> <PackageReference Include="AkkaDotModule.Webnori" Version="1.1.1" />
appsetting.json
{ "Logging": { "LogLevel": { "Default": "Debug", "System": "Information", "Microsoft": "Information" } }, "AppSettings": { "GraphConnection": "http://localhost:7474", "GraphConnectionUser": "neo4j", "GraphConnectionPw": "bitnami" } }
GraphEngine
using Neo4jClient; using Neo4jClient.Cypher; public class GraphEngine { private readonly AppSettings appSettings; private readonly ILogger logger; private readonly GraphClient neo4jClient; public GraphEngine(AppSettings _appSettings, ILogger<GraphEngine> _logger) { appSettings = _appSettings; logger = _logger; neo4jClient = new GraphClient(new Uri(appSettings.GraphConnection), appSettings.GraphConnectionUser, appSettings.GraphConnectionPw); } public async Task<ICypherFluentQuery> GetCypher() { if (!neo4jClient.IsConnected) { await neo4jClient.ConnectAsync(); } return neo4jClient.Cypher; } public async Task<GraphClient> GetClient() { if (!neo4jClient.IsConnected) { await neo4jClient.ConnectAsync(); } return neo4jClient; } public async Task RemoveAll() { var cyper = await GetCypher(); await cyper .Match("(n)") .DetachDelete("n") .ExecuteWithoutResultsAsync(); } } DI셋팅 public void ConfigureServices(IServiceCollection services) { services.AddSingleton<GraphEngine>(); }