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();
}
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[]
}
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[]
}
}
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;
}
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();
}
Aggregations