use of org.neo4j.driver.Session in project beam by apache.
the class Neo4jIOIT method testWriteUnwind.
@Test
public void testWriteUnwind() throws Exception {
PCollection<String> stringsCollections = writeUnwindPipeline.apply(Create.of(Arrays.asList("one", "two", "three")));
// Every row is represented by a Map<String, Object> in the parameters map.
// We accumulate the rows and 'unwind' those to Neo4j for performance reasons.
//
SerializableFunction<String, Map<String, Object>> parametersMapper = name -> Collections.singletonMap("name", name);
Neo4jIO.WriteUnwind<String> read = Neo4jIO.<String>writeUnwind().withDriverConfiguration(Neo4jTestUtil.getDriverConfiguration(containerHostname, containerPort)).withSessionConfig(SessionConfig.forDatabase(Neo4jTestUtil.NEO4J_DATABASE)).withBatchSize(5000).withUnwindMapName("rows").withCypher("UNWIND $rows AS row MERGE(n:Num { name : row.name })").withParametersFunction(parametersMapper).withCypherLogging();
stringsCollections.apply(read);
// Now run this pipeline
//
PipelineResult pipelineResult = writeUnwindPipeline.run();
Assert.assertEquals(PipelineResult.State.DONE, pipelineResult.getState());
//
try (Driver driver = Neo4jTestUtil.getDriver(containerHostname, containerPort)) {
try (Session session = Neo4jTestUtil.getSession(driver, true)) {
List<String> names = session.readTransaction(tx -> {
List<String> list = new ArrayList<>();
Result result = tx.run("MATCH(n:Num) RETURN n.name");
while (result.hasNext()) {
Record record = result.next();
list.add(record.get(0).asString());
}
return list;
});
assertThat(names, containsInAnyOrder("one", "two", "three"));
}
}
}
use of org.neo4j.driver.Session in project spring-boot by spring-projects.
the class Neo4jHealthIndicatorTests method mockDriver.
private Driver mockDriver(ResultSummary resultSummary, String version, String edition) {
Result statementResult = mockStatementResult(resultSummary, version, edition);
Session session = mock(Session.class);
given(session.run(anyString())).willReturn(statementResult);
Driver driver = mock(Driver.class);
given(driver.session(any(SessionConfig.class))).willReturn(session);
return driver;
}
use of org.neo4j.driver.Session in project spring-boot by spring-projects.
the class Neo4jHealthIndicatorTests method neo4jIsUpWithOneSessionExpiredException.
@Test
void neo4jIsUpWithOneSessionExpiredException() {
ResultSummary resultSummary = ResultSummaryMock.createResultSummary("My Home", "");
Session session = mock(Session.class);
Result statementResult = mockStatementResult(resultSummary, "4711", "some edition");
AtomicInteger count = new AtomicInteger();
given(session.run(anyString())).will((invocation) -> {
if (count.compareAndSet(0, 1)) {
throw new SessionExpiredException("Session expired");
}
return statementResult;
});
Driver driver = mock(Driver.class);
given(driver.session(any(SessionConfig.class))).willReturn(session);
Neo4jHealthIndicator healthIndicator = new Neo4jHealthIndicator(driver);
Health health = healthIndicator.health();
assertThat(health.getStatus()).isEqualTo(Status.UP);
assertThat(health.getDetails()).containsEntry("server", "4711@My Home");
then(session).should(times(2)).close();
}
use of org.neo4j.driver.Session in project spring-boot by spring-projects.
the class MockedDriverConfiguration method driver.
@Bean
Driver driver() {
Driver driver = mock(Driver.class);
TypeSystem typeSystem = mock(TypeSystem.class);
Session session = mock(Session.class);
given(driver.defaultTypeSystem()).willReturn(typeSystem);
given(driver.session(ArgumentMatchers.any(SessionConfig.class))).willReturn(session);
return driver;
}
use of org.neo4j.driver.Session in project zeppelin by apache.
the class Neo4jConnectionManager method execute.
public List<Record> execute(String cypherQuery, InterpreterContext interpreterContext) {
Map<String, Object> params = new HashMap<>();
if (interpreterContext != null) {
ResourcePool resourcePool = interpreterContext.getResourcePool();
Set<String> keys = extractParams(cypherQuery, PROPERTY_PATTERN, REPLACE_CURLY_BRACKETS);
keys.addAll(extractParams(cypherQuery, $_PATTERN, REPLACE_$));
for (String key : keys) {
Resource resource = resourcePool.get(key);
if (resource != null) {
params.put(key, resource.get());
}
}
}
LOGGER.debug("Executing cypher query {} with params {}", cypherQuery, params);
try (Session session = getSession()) {
final Result result = params.isEmpty() ? session.run(cypherQuery) : session.run(cypherQuery, params);
return result.list();
}
}
Aggregations