Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
neo4j 4.0 example
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.logging.Level;
import static java.util.concurrent.TimeUnit.SECONDS;
import java.time.Duration;
import java.time.Instant;
import org.neo4j.driver.internal.logging.ConsoleLogging;
import org.neo4j.driver.AuthToken;
import org.neo4j.driver.AuthTokens;
import org.neo4j.driver.Config;
import org.neo4j.driver.Driver;
import org.neo4j.driver.GraphDatabase;
import org.neo4j.driver.Record;
import org.neo4j.driver.Session;
import org.neo4j.driver.StatementResult;
import org.neo4j.driver.Transaction;
import org.neo4j.driver.exceptions.ServiceUnavailableException;
import org.neo4j.driver.*;
import org.reactivestreams.*;
import static org.neo4j.driver.SessionConfig.builder;
import static org.neo4j.driver.Values.parameters;
public class Neo4jDriverTest {
/*
private static final Config config = Config.build()
.withLogging( new ConsoleLogging( Level.FINE ) )
.withMaxTransactionRetryTime( 60, SECONDS )
.withEncryption()
.withTrustStrategy( Config.TrustStrategy.trustAllCertificates()).toConfig();
*/
public static void main(String[] argv) throws Exception {
System.out.println("-------- Neo4j Bolt Connection Testing ------");
Transaction transaction;
List<String> uris = new ArrayList<String>();
uris.add("bolt+routing://localhost:7687");
uris.add("bolt+routing://localhost:7697");
uris.add("bolt+routing://localhost:7607");
Driver driver = null;
int i=0;
for (i =0; i<1; i++){
int j = 100;
// driver = acquireDriver(uris,AuthTokens.basic( "neo4j", "password" ),config );
driver = GraphDatabase.driver( "neo4j://localhost:7687", AuthTokens.basic( "Neo4j Admin", "neo4j" ) );
Instant start = Instant.now();
try (Session session = driver.session( builder().withDatabase( "iam" ).withDefaultAccessMode( AccessMode.WRITE ).build() ) )
{
transaction = session.beginTransaction();
for (i =0; i<30; i++){
transaction.run("unwind range(1,20000) as id create (:Person{id:id})");
transaction.success();
//transaction.run("CREATE (a:Person {name: $name})", parameters( "name", "smith" ) );
}
transaction.close();
}
Instant end = Instant.now();
Duration timeElapsed = Duration.between(start, end);
System.out.println("Time taken: "+ timeElapsed.toMillis() +" milliseconds");
}
driver.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment