Search in sources :

Example 16 with Config

use of org.neo4j.driver.v1.Config in project cypher-for-gremlin by opencypher.

the class GremlinNeo4jDriverTest method translating.

@Test
public void translating() {
    Config config = Config.build().withTranslation(TranslatorFlavor.gremlinServer()).toConfig();
    Driver driver = GremlinDatabase.driver("//localhost:" + server.getPort(), config);
    try (Session session = driver.session()) {
        StatementResult result = session.run("MATCH (n:person) RETURN count(n) as count");
        int count = result.single().get("count").asInt();
        assertThat(count).isEqualTo(4);
    }
}
Also used : StatementResult(org.neo4j.driver.v1.StatementResult) Config(org.opencypher.Config) Driver(org.neo4j.driver.v1.Driver) Session(org.neo4j.driver.v1.Session) Test(org.junit.Test)

Example 17 with Config

use of org.neo4j.driver.v1.Config in project cypher-for-gremlin by opencypher.

the class GremlinNeo4jDriverTest method invalidSyntaxInTranslation.

@Test
public void invalidSyntaxInTranslation() {
    Config config = Config.build().withTranslation(TranslatorFlavor.gremlinServer()).toConfig();
    Driver driver = GremlinDatabase.driver("//localhost:" + server.getPort(), config);
    try (Session session = driver.session()) {
        StatementResult result = session.run("INVALID");
        Throwable throwable = catchThrowable(result::list);
        assertThat(throwable).hasMessageContaining("Invalid input");
    }
}
Also used : StatementResult(org.neo4j.driver.v1.StatementResult) Config(org.opencypher.Config) Driver(org.neo4j.driver.v1.Driver) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) Session(org.neo4j.driver.v1.Session) Test(org.junit.Test)

Example 18 with Config

use of org.neo4j.driver.v1.Config in project cypher-for-gremlin by opencypher.

the class CypherGremlinNeo4jDriver method createUseDriver.

@Test
public void createUseDriver() throws Exception {
    int port = gremlinServer.getPort();
    // freshReadmeSnippet: createConfiguration
    Config config = Config.build().withTranslation().toConfig();
    String uri = "//localhost:" + port;
    Driver driver = GremlinDatabase.driver(uri, config);
    // freshReadmeSnippet: useDriver
    try (Session session = driver.session()) {
        StatementResult result = session.run("CREATE (a:Greeting) " + "SET a.message = $message " + "RETURN a.message", parameters("message", "Hello"));
        String message = result.single().get(0).asString();
        assertThat(message).isEqualTo("Hello");
    }
// freshReadmeSnippet: useDriver
}
Also used : StatementResult(org.neo4j.driver.v1.StatementResult) Config(org.opencypher.Config) Driver(org.neo4j.driver.v1.Driver) Session(org.neo4j.driver.v1.Session) Test(org.junit.Test)

Example 19 with Config

use of org.neo4j.driver.v1.Config in project open-kilda by telstra.

the class FlowJanitor method main.

/**
 * With the right URL, username, password, and method keyword, the methods above can be called.
 */
@SuppressWarnings({ "squid:S106", "squid:S1135" })
public static void main(String[] args) {
    Options options = new Options();
    options.addOption(Option.builder("url").required(true).hasArg().desc("The URL of the Neo4J DB - i.e. bolt://neo..:7474").build());
    options.addOption(Option.builder("u").required(true).hasArg().longOpt("user").desc("The Neo4J username - e.g. neo4j").build());
    options.addOption(Option.builder("p").required(true).hasArg().longOpt("password").desc("The Neo4J password - e.g. neo4j").build());
    options.addOption(Option.builder("nburl").required(true).hasArg().desc("The URL of the Neo4J DB - i.e. http://northboud..:8080").build());
    options.addOption(Option.builder("nbu").required(true).hasArg().longOpt("user").desc("The Neo4J username - e.g. kilda").build());
    options.addOption(Option.builder("nbp").required(true).hasArg().longOpt("password").desc("The Neo4J password - e.g. kilda").build());
    options.addOption(Option.builder("a").required(true).hasArg().longOpt("action").desc("The action to take - e.g. ountDuplicateCookies").build());
    options.addOption(Option.builder("v").required(false).longOpt("verbose").desc("Where appropriate, return a verbose response").build());
    CommandLine commandLine;
    CommandLineParser parser = new DefaultParser();
    Driver driver = null;
    try {
        commandLine = parser.parse(options, args);
        FlowJanitor.Config config = new FlowJanitor.Config();
        config.neoUrl = commandLine.getOptionValue("url");
        config.neoUser = commandLine.getOptionValue("u");
        config.neoPswd = commandLine.getOptionValue("p");
        config.nbUrl = commandLine.getOptionValue("nburl");
        config.nbUser = commandLine.getOptionValue("nbu");
        config.nbPswd = commandLine.getOptionValue("nbp");
        config.action = commandLine.getOptionValue("a");
        driver = GraphDatabase.driver(config.neoUrl, AuthTokens.basic(config.neoUser, config.neoPswd));
        if (config.action.equals("DeDupeFlows")) {
            Session session = driver.session();
            StatementResult result = session.run(DUPLICATE_FLOWS_QUERY);
            Map<String, List<Long>> flowsToUpdate = new HashMap<>();
            for (Record record : result.list()) {
                String flowid = record.get("affected_flow_id").asString();
                List<Long> priors = flowsToUpdate.computeIfAbsent(flowid, empty -> new ArrayList<>());
                priors.add(record.get("affected_flow_cookie").asLong());
            }
            session.close();
            System.out.println("flowsToUpdate.size() = " + flowsToUpdate.size());
            System.out.println("flowsToUpdate = " + flowsToUpdate);
            System.out.println("Will De-Dupe the Flows");
            String authString = config.nbUser + ":" + config.nbPswd;
            String authStringEnc = Base64.getEncoder().encodeToString(authString.getBytes());
            Client client = ClientBuilder.newClient();
            for (String flowid : flowsToUpdate.keySet()) {
                /*
                     * Get the Flows .. call NB for each
                     */
                sleep();
                System.out.println("RUNNING: flowid = " + flowid);
                WebTarget webTarget = client.target(config.nbUrl + "flows").path(flowid);
                Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON).header("Authorization", "Basic " + authStringEnc);
                Response response = invocationBuilder.get(Response.class);
                if (response.getStatus() != 200) {
                    throw new IllegalStateException("Failed : HTTP error code : " + response.getStatus());
                }
                String output = response.readEntity(String.class);
                System.out.println("output = " + output);
                System.exit(0);
            }
        } else {
            // TODO: switch, based on action
            Session session = driver.session();
            StatementResult result = session.run(DUPLICATE_COOKIES_QUERY);
            List<String> flowsToUpdate = new ArrayList<>();
            for (Record record : result.list()) {
                flowsToUpdate.add(record.get("affected_flow_id").asString());
            }
            session.close();
            System.out.println("flowsToUpdate.size() = " + flowsToUpdate.size());
            System.out.println("flowsToUpdate = " + flowsToUpdate);
            System.exit(0);
            FlowJanitor.updateFlows(config, flowsToUpdate);
        }
    } catch (ParseException exception) {
        System.out.print("Parse error: ");
        System.out.println(exception.getMessage());
        // automatically generate the help statement
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("FlowJanitor", options);
    } finally {
        if (driver != null) {
            driver.close();
        }
    }
}
Also used : Options(org.apache.commons.cli.Options) StatementResult(org.neo4j.driver.v1.StatementResult) Invocation(javax.ws.rs.client.Invocation) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Driver(org.neo4j.driver.v1.Driver) HelpFormatter(org.apache.commons.cli.HelpFormatter) ArrayList(java.util.ArrayList) List(java.util.List) Record(org.neo4j.driver.v1.Record) CommandLineParser(org.apache.commons.cli.CommandLineParser) Client(javax.ws.rs.client.Client) DefaultParser(org.apache.commons.cli.DefaultParser) Response(javax.ws.rs.core.Response) CommandLine(org.apache.commons.cli.CommandLine) WebTarget(javax.ws.rs.client.WebTarget) ParseException(org.apache.commons.cli.ParseException) Session(org.neo4j.driver.v1.Session)

Aggregations

Test (org.junit.Test)9 Driver (org.neo4j.driver.v1.Driver)9 Config (sun.security.krb5.Config)8 Session (org.neo4j.driver.v1.Session)7 StatementResult (org.neo4j.driver.v1.StatementResult)4 IOException (java.io.IOException)3 Config (org.opencypher.Config)3 KrbException (sun.security.krb5.KrbException)3 RemoteException (java.rmi.RemoteException)2 GuardTimeoutException (org.neo4j.kernel.guard.GuardTimeoutException)2 GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)2 CommunityNeoServer (org.neo4j.server.CommunityNeoServer)2 ShellException (org.neo4j.shell.ShellException)2 CyNodeLoadingHandler (uk.ac.rothamsted.rdf.neo4j.load.support.CyNodeLoadingHandler)2 CyNodeLoadingProcessor (uk.ac.rothamsted.rdf.neo4j.load.support.CyNodeLoadingProcessor)2 CyRelationLoadingHandler (uk.ac.rothamsted.rdf.neo4j.load.support.CyRelationLoadingHandler)2 CyRelationLoadingProcessor (uk.ac.rothamsted.rdf.neo4j.load.support.CyRelationLoadingProcessor)2 Neo4jDataManager (uk.ac.rothamsted.rdf.neo4j.load.support.Neo4jDataManager)2 RdfDataManager (uk.ac.rothamsted.rdf.neo4j.load.support.RdfDataManager)2 RdfDataManagerTest (uk.ac.rothamsted.rdf.neo4j.load.support.RdfDataManagerTest)2