Search in sources :

Example 21 with DatabaseManagementServiceBuilder

use of org.neo4j.dbms.api.DatabaseManagementServiceBuilder in project neo4j-documentation by neo4j.

the class ProcedureExampleDocTest method listDenseNodesShouldWork.

@Test
void listDenseNodesShouldWork() throws Throwable {
    // Given
    new JarBuilder().createJarFor(directory.resolve("myProcedures.jar").toFile(), ProcedureExample.class);
    managementService = new DatabaseManagementServiceBuilder(directory).setConfig(GraphDatabaseSettings.plugin_dir, directory.toAbsolutePath()).build();
    db = managementService.database(DEFAULT_DATABASE_NAME);
    try (Transaction transaction = db.beginTx()) {
        Node node1 = transaction.createNode();
        Node node2 = transaction.createNode();
        Node node3 = transaction.createNode();
        node1.createRelationshipTo(node1, RelationshipType.withName("KNOWS"));
        node1.createRelationshipTo(node2, RelationshipType.withName("KNOWS"));
        node1.createRelationshipTo(node3, RelationshipType.withName("KNOWS"));
        // When
        Result res = transaction.execute("CALL org.neo4j.examples.findDenseNodes(2)");
        // Then
        assertEquals(map("degree", 3L, "nodeId", node1.getId()), res.next());
        assertFalse(res.hasNext());
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) DatabaseManagementServiceBuilder(org.neo4j.dbms.api.DatabaseManagementServiceBuilder) JarBuilder(org.neo4j.doc.kernel.impl.proc.JarBuilder) Result(org.neo4j.graphdb.Result) Test(org.junit.jupiter.api.Test)

Example 22 with DatabaseManagementServiceBuilder

use of org.neo4j.dbms.api.DatabaseManagementServiceBuilder in project neo4j-documentation by neo4j.

the class ReadOnlyDocTest method prepareReadOnlyDatabase.

/**
 * Create read only database.
 */
@Before
public void prepareReadOnlyDatabase() throws IOException {
    Path dir = Path.of("target/read-only-managementService/location");
    if (Files.exists(dir)) {
        FileUtils.deleteDirectory(dir);
    }
    new DatabaseManagementServiceBuilder(dir).build().shutdown();
    // tag::createReadOnlyInstance[]
    managementService = new DatabaseManagementServiceBuilder(dir).setConfig(GraphDatabaseSettings.read_only_database_default, true).build();
    graphDb = managementService.database(DEFAULT_DATABASE_NAME);
// end::createReadOnlyInstance[]
}
Also used : Path(java.nio.file.Path) DatabaseManagementServiceBuilder(org.neo4j.dbms.api.DatabaseManagementServiceBuilder) Before(org.junit.Before)

Example 23 with DatabaseManagementServiceBuilder

use of org.neo4j.dbms.api.DatabaseManagementServiceBuilder in project neo4j-documentation by neo4j.

the class TestGraphvizSubgraphOutput method testSimpleGraph.

@Test
public void testSimpleGraph() throws Exception {
    Path folder = Path.of("target/example-db" + System.nanoTime());
    DatabaseManagementService managementService = new DatabaseManagementServiceBuilder(folder).build();
    try {
        GraphDatabaseService neo = managementService.database(DEFAULT_DATABASE_NAME);
        try (Transaction tx = neo.beginTx()) {
            final Node emil = tx.createNode();
            emil.setProperty("name", "Emil Eifrém");
            emil.setProperty("country_of_residence", "USA");
            final Node tobias = tx.createNode();
            tobias.setProperty("name", "Tobias Ivarsson");
            tobias.setProperty("country_of_residence", "Sweden");
            final Node johan = tx.createNode();
            johan.setProperty("name", "Johan Svensson");
            johan.setProperty("country_of_residence", "Sweden");
            final Relationship emilKNOWStobias = emil.createRelationshipTo(tobias, type.KNOWS);
            final Relationship johanKNOWSemil = johan.createRelationshipTo(emil, type.KNOWS);
            final Relationship tobiasKNOWSjohan = tobias.createRelationshipTo(johan, type.KNOWS);
            final Relationship tobiasWORKS_FORemil = tobias.createRelationshipTo(emil, type.WORKS_FOR);
            OutputStream out = new ByteArrayOutputStream();
            SubgraphMapper subgraphMapper = node -> {
                if (node.hasProperty("country_of_residence")) {
                    return (String) node.getProperty("country_of_residence");
                }
                return null;
            };
            GraphvizWriter writer = new GraphvizWriter();
            SubgraphMapper.SubgraphMappingWalker walker = new SubgraphMapper.SubgraphMappingWalker(subgraphMapper) {

                @Override
                protected Iterable<Node> nodes() {
                    return asList(emil, tobias, johan);
                }

                @Override
                protected Iterable<Relationship> relationships() {
                    return asList(emilKNOWStobias, johanKNOWSemil, tobiasKNOWSjohan, tobiasWORKS_FORemil);
                }
            };
            writer.emit(out, walker);
            tx.commit();
        }
    } finally {
        managementService.shutdown();
        deleteDirectory(folder.toFile());
    }
}
Also used : Path(java.nio.file.Path) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DatabaseManagementServiceBuilder(org.neo4j.dbms.api.DatabaseManagementServiceBuilder) Test(org.junit.Test) Node(org.neo4j.graphdb.Node) FileUtils.deleteDirectory(org.apache.commons.io.FileUtils.deleteDirectory) DEFAULT_DATABASE_NAME(org.neo4j.configuration.GraphDatabaseSettings.DEFAULT_DATABASE_NAME) Relationship(org.neo4j.graphdb.Relationship) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Arrays.asList(java.util.Arrays.asList) RelationshipType(org.neo4j.graphdb.RelationshipType) DatabaseManagementService(org.neo4j.dbms.api.DatabaseManagementService) Transaction(org.neo4j.graphdb.Transaction) Path(java.nio.file.Path) SubgraphMapper(org.neo4j.visualization.SubgraphMapper) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) SubgraphMapper(org.neo4j.visualization.SubgraphMapper) Node(org.neo4j.graphdb.Node) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Transaction(org.neo4j.graphdb.Transaction) Relationship(org.neo4j.graphdb.Relationship) DatabaseManagementServiceBuilder(org.neo4j.dbms.api.DatabaseManagementServiceBuilder) DatabaseManagementService(org.neo4j.dbms.api.DatabaseManagementService) Test(org.junit.Test)

Example 24 with DatabaseManagementServiceBuilder

use of org.neo4j.dbms.api.DatabaseManagementServiceBuilder in project neo4j-documentation by neo4j.

the class TestNewGraphvizWriter method testSimpleGraph.

@Test
public void testSimpleGraph() throws Exception {
    Path folder = Path.of("target/example-db" + System.nanoTime());
    DatabaseManagementService managementService = new DatabaseManagementServiceBuilder(folder).build();
    try {
        GraphDatabaseService neo = managementService.database(DEFAULT_DATABASE_NAME);
        try (Transaction tx = neo.beginTx()) {
            final Node emil = tx.createNode();
            emil.setProperty("name", "Emil Eifrém");
            emil.setProperty("age", 30);
            final Node tobias = tx.createNode();
            tobias.setProperty("name", "Tobias \"thobe\" Ivarsson");
            tobias.setProperty("age", 23);
            tobias.setProperty("hours", new int[] { 10, 10, 4, 4, 0 });
            final Node johan = tx.createNode();
            johan.setProperty("!<>)", "!<>)");
            johan.setProperty("name", "!<>Johan '\\n00b' !<>Svensson");
            final Relationship emilKNOWStobias = emil.createRelationshipTo(tobias, type.KNOWS);
            emilKNOWStobias.setProperty("since", "2003-08-17");
            final Relationship johanKNOWSemil = johan.createRelationshipTo(emil, type.KNOWS);
            final Relationship tobiasKNOWSjohan = tobias.createRelationshipTo(johan, type.KNOWS);
            final Relationship tobiasWORKS_FORemil = tobias.createRelationshipTo(emil, type.WORKS_FOR);
            OutputStream out = new ByteArrayOutputStream();
            GraphvizWriter writer = new GraphvizWriter();
            Iterable<Node> traverser = tx.traversalDescription().depthFirst().relationships(type.KNOWS).relationships(type.WORKS_FOR).traverse(emil).nodes();
            writer.emit(out, Walker.crosscut(traverser, type.KNOWS, type.WORKS_FOR));
            tx.commit();
            out.toString();
        }
    } finally {
        managementService.shutdown();
        deleteDirectory(folder.toFile());
    }
}
Also used : Path(java.nio.file.Path) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DatabaseManagementServiceBuilder(org.neo4j.dbms.api.DatabaseManagementServiceBuilder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DatabaseManagementService(org.neo4j.dbms.api.DatabaseManagementService) Test(org.junit.Test)

Example 25 with DatabaseManagementServiceBuilder

use of org.neo4j.dbms.api.DatabaseManagementServiceBuilder in project neo4j-documentation by neo4j.

the class SocnetTest method setup.

@BeforeEach
void setup() throws Exception {
    managementService = new DatabaseManagementServiceBuilder(folder).build();
    graphDb = managementService.database(DEFAULT_DATABASE_NAME);
    try (Transaction tx = graphDb.beginTx()) {
        personRepository = new PersonRepository(graphDb, tx);
        createPersons(tx);
        setupFriendsBetweenPeople(tx, 10);
        tx.commit();
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) DatabaseManagementServiceBuilder(org.neo4j.dbms.api.DatabaseManagementServiceBuilder) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

DatabaseManagementServiceBuilder (org.neo4j.dbms.api.DatabaseManagementServiceBuilder)30 DatabaseManagementService (org.neo4j.dbms.api.DatabaseManagementService)19 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)13 Transaction (org.neo4j.graphdb.Transaction)11 Node (org.neo4j.graphdb.Node)10 TestDatabaseManagementServiceBuilder (org.neo4j.test.TestDatabaseManagementServiceBuilder)10 Test (org.junit.jupiter.api.Test)9 Path (java.nio.file.Path)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 OutputStream (java.io.OutputStream)2 Test (org.junit.Test)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 Disabled (org.junit.jupiter.api.Disabled)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 MethodSource (org.junit.jupiter.params.provider.MethodSource)2 Relationship (org.neo4j.graphdb.Relationship)2 RelationshipType (org.neo4j.graphdb.RelationshipType)2 Result (org.neo4j.graphdb.Result)2 DefaultFileSystemAbstraction (org.neo4j.io.fs.DefaultFileSystemAbstraction)2 EphemeralFileSystemAbstraction (org.neo4j.io.fs.EphemeralFileSystemAbstraction)2