Search in sources :

Example 16 with DatabaseManagementServiceBuilder

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

the class JavaQuery method run.

void run() {
    clearDbPath();
    // tag::addData[]
    DatabaseManagementService managementService = new DatabaseManagementServiceBuilder(databaseDirectory).build();
    GraphDatabaseService db = managementService.database(DEFAULT_DATABASE_NAME);
    try (Transaction tx = db.beginTx()) {
        Node myNode = tx.createNode();
        myNode.setProperty("name", "my node");
        tx.commit();
    }
    // tag::execute[]
    try (Transaction tx = db.beginTx();
        Result result = tx.execute("MATCH (n {name: 'my node'}) RETURN n, n.name")) {
        while (result.hasNext()) {
            Map<String, Object> row = result.next();
            for (Entry<String, Object> column : row.entrySet()) {
                rows += column.getKey() + ": " + column.getValue() + "; ";
            }
            rows += "\n";
        }
    }
    // the result is now empty, get a new one
    try (Transaction tx = db.beginTx();
        Result result = tx.execute("MATCH (n {name: 'my node'}) RETURN n, n.name")) {
        // tag::items[]
        Iterator<Node> n_column = result.columnAs("n");
        n_column.forEachRemaining(node -> nodeResult = node + ": " + node.getProperty("name"));
        // end::items[]
        // tag::columns[]
        List<String> columns = result.columns();
        // end::columns[]
        columnsString = columns.toString();
        resultString = tx.execute("MATCH (n {name: 'my node'}) RETURN n, n.name").resultAsString();
    }
    managementService.shutdown();
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) DatabaseManagementServiceBuilder(org.neo4j.dbms.api.DatabaseManagementServiceBuilder) DatabaseManagementService(org.neo4j.dbms.api.DatabaseManagementService) Result(org.neo4j.graphdb.Result)

Example 17 with DatabaseManagementServiceBuilder

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

the class EmbeddedNeo4jWithIndexing method main.

public static void main(final String[] args) throws IOException {
    System.out.println("Starting database ...");
    FileUtils.deleteDirectory(databaseDirectory);
    // tag::startDb[]
    DatabaseManagementService managementService = new DatabaseManagementServiceBuilder(databaseDirectory).build();
    GraphDatabaseService graphDb = managementService.database(DEFAULT_DATABASE_NAME);
    // end::startDb[]
    {
        // tag::createIndex[]
        IndexDefinition usernamesIndex;
        try (Transaction tx = graphDb.beginTx()) {
            Schema schema = tx.schema();
            usernamesIndex = // <1>
            schema.indexFor(Label.label("User")).on(// <2>
            "username").withName(// <3>
            "usernames").create();
            // <5>
            tx.commit();
        }
        // tag::wait[]
        try (Transaction tx = graphDb.beginTx()) {
            Schema schema = tx.schema();
            schema.awaitIndexOnline(usernamesIndex, 10, TimeUnit.SECONDS);
        }
        // tag::progress[]
        try (Transaction tx = graphDb.beginTx()) {
            Schema schema = tx.schema();
            System.out.println(String.format("Percent complete: %1.0f%%", schema.getIndexPopulationProgress(usernamesIndex).getCompletedPercentage()));
        }
    // end::progress[]
    }
    {
        // tag::addUsers[]
        try (Transaction tx = graphDb.beginTx()) {
            Label label = Label.label("User");
            // Create some users
            for (int id = 0; id < 100; id++) {
                Node userNode = tx.createNode(label);
                userNode.setProperty("username", "user" + id + "@neo4j.org");
            }
            System.out.println("Users created");
            tx.commit();
        }
    // end::addUsers[]
    }
    {
        // tag::findUsers[]
        Label label = Label.label("User");
        int idToFind = 45;
        String nameToFind = "user" + idToFind + "@neo4j.org";
        try (Transaction tx = graphDb.beginTx()) {
            try (ResourceIterator<Node> users = tx.findNodes(label, "username", nameToFind)) {
                ArrayList<Node> userNodes = new ArrayList<>();
                while (users.hasNext()) {
                    userNodes.add(users.next());
                }
                for (Node node : userNodes) {
                    System.out.println("The username of user " + idToFind + " is " + node.getProperty("username"));
                }
            }
        }
    // end::findUsers[]
    }
    {
        // tag::resourceIterator[]
        Label label = Label.label("User");
        int idToFind = 45;
        String nameToFind = "user" + idToFind + "@neo4j.org";
        try (Transaction tx = graphDb.beginTx();
            ResourceIterator<Node> users = tx.findNodes(label, "username", nameToFind)) {
            Node firstUserNode;
            if (users.hasNext()) {
                firstUserNode = users.next();
            }
            users.close();
        // ... Do stuff with the firstUserNode we found ...
        }
    // end::resourceIterator[]
    }
    {
        // tag::updateUsers[]
        try (Transaction tx = graphDb.beginTx()) {
            Label label = Label.label("User");
            int idToFind = 45;
            String nameToFind = "user" + idToFind + "@neo4j.org";
            for (Node node : loop(tx.findNodes(label, "username", nameToFind))) {
                node.setProperty("username", "user" + (idToFind + 1) + "@neo4j.org");
            }
            tx.commit();
        }
    // end::updateUsers[]
    }
    {
        // tag::deleteUsers[]
        try (Transaction tx = graphDb.beginTx()) {
            Label label = Label.label("User");
            int idToFind = 46;
            String nameToFind = "user" + idToFind + "@neo4j.org";
            for (Node node : loop(tx.findNodes(label, "username", nameToFind))) {
                node.delete();
            }
            tx.commit();
        }
    // end::deleteUsers[]
    }
    {
        // tag::dropIndex[]
        try (Transaction tx = graphDb.beginTx()) {
            // <1>
            IndexDefinition usernamesIndex = tx.schema().getIndexByName("usernames");
            usernamesIndex.drop();
            tx.commit();
        }
    // end::dropIndex[]
    }
    System.out.println("Shutting down database ...");
    // tag::shutdownDb[]
    managementService.shutdown();
// end::shutdownDb[]
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition) Transaction(org.neo4j.graphdb.Transaction) Schema(org.neo4j.graphdb.schema.Schema) Node(org.neo4j.graphdb.Node) DatabaseManagementServiceBuilder(org.neo4j.dbms.api.DatabaseManagementServiceBuilder) Label(org.neo4j.graphdb.Label) ArrayList(java.util.ArrayList) ResourceIterator(org.neo4j.graphdb.ResourceIterator) DatabaseManagementService(org.neo4j.dbms.api.DatabaseManagementService)

Example 18 with DatabaseManagementServiceBuilder

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

the class TerminateTransactions method run.

public String run() throws IOException {
    FileUtils.deleteDirectory(databaseDirectory);
    // tag::startDb[]
    DatabaseManagementService managementService = new DatabaseManagementServiceBuilder(databaseDirectory).build();
    GraphDatabaseService graphDb = managementService.database(DEFAULT_DATABASE_NAME);
    // end::startDb[]
    // tag::mkTree[]
    RelationshipType relType = RelationshipType.withName("CHILD");
    Queue<Node> nodes = new LinkedList<>();
    int depth = 1;
    try (Transaction tx = graphDb.beginTx()) {
        Node rootNode = tx.createNode();
        nodes.add(rootNode);
        // end::mkTree[]
        Terminator terminator = new Terminator(tx);
        terminator.terminateAfter(1000);
        // tag::mkTree[]
        for (; true; depth++) {
            int nodesToExpand = nodes.size();
            for (int i = 0; i < nodesToExpand; ++i) {
                Node parent = nodes.remove();
                Node left = tx.createNode();
                Node right = tx.createNode();
                parent.createRelationshipTo(left, relType);
                parent.createRelationshipTo(right, relType);
                nodes.add(left);
                nodes.add(right);
            }
        }
    } catch (TransactionTerminatedException ignored) {
        return String.format("Created tree up to depth %s in 1 sec", depth);
    } finally // end::mkTree[]
    {
        // tag::shutdownDb[]
        managementService.shutdown();
    // end::shutdownDb[]
    }
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) TransactionTerminatedException(org.neo4j.graphdb.TransactionTerminatedException) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) DatabaseManagementServiceBuilder(org.neo4j.dbms.api.DatabaseManagementServiceBuilder) RelationshipType(org.neo4j.graphdb.RelationshipType) DatabaseManagementService(org.neo4j.dbms.api.DatabaseManagementService) LinkedList(java.util.LinkedList)

Example 19 with DatabaseManagementServiceBuilder

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

the class Neo4jInstance method newCommunityInstance.

public DatabaseManagementService newCommunityInstance() throws IOException {
    Files.createDirectories(baseDatabaseDirectory);
    DatabaseManagementService managementService = new DatabaseManagementServiceBuilder(databaseDirectory()).setConfig(GraphDatabaseSettings.auth_enabled, true).build();
    registerShutdownHook(managementService);
    return managementService;
}
Also used : EnterpriseDatabaseManagementServiceBuilder(com.neo4j.dbms.api.EnterpriseDatabaseManagementServiceBuilder) DatabaseManagementServiceBuilder(org.neo4j.dbms.api.DatabaseManagementServiceBuilder) DatabaseManagementService(org.neo4j.dbms.api.DatabaseManagementService)

Example 20 with DatabaseManagementServiceBuilder

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

the class Neo4jBasicDocTest method startWithConfiguration.

// end::afterTest[]
@Test
void startWithConfiguration() {
    // tag::startDbWithConfig[]
    DatabaseManagementService service = new DatabaseManagementServiceBuilder(directory.resolve("withConfiguration")).setConfig(GraphDatabaseSettings.pagecache_memory, ByteUnit.mebiBytes(512)).setConfig(GraphDatabaseSettings.transaction_timeout, Duration.ofSeconds(60)).setConfig(GraphDatabaseSettings.preallocate_logical_logs, true).build();
    // end::startDbWithConfig[]
    service.shutdown();
}
Also used : DatabaseManagementServiceBuilder(org.neo4j.dbms.api.DatabaseManagementServiceBuilder) DatabaseManagementService(org.neo4j.dbms.api.DatabaseManagementService) Test(org.junit.jupiter.api.Test)

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