Search in sources :

Example 1 with MongoConnectionDetails

use of org.apache.rya.api.client.mongo.MongoConnectionDetails in project incubator-rya by apache.

the class RyaPromptProviderTest method isConnected_noInstanceName_Mongo.

@Test
public void isConnected_noInstanceName_Mongo() {
    // Create a shared state that is connected to a storage, but not a rya instance.
    final SharedShellState sharedState = new SharedShellState();
    final MongoConnectionDetails connectionDetails = new MongoConnectionDetails("testMongoHost", 999, Optional.of("username"), Optional.of(new char[] {}));
    sharedState.connectedToMongo(connectionDetails, mock(RyaClient.class));
    // Create a prompt.
    final String prompt = new RyaPromptProvider(sharedState).getPrompt();
    // Verify the prompt is formatted correctly.
    final String expected = "rya/testMongoHost> ";
    assertEquals(expected, prompt);
}
Also used : MongoConnectionDetails(org.apache.rya.api.client.mongo.MongoConnectionDetails) RyaClient(org.apache.rya.api.client.RyaClient) Test(org.junit.Test)

Example 2 with MongoConnectionDetails

use of org.apache.rya.api.client.mongo.MongoConnectionDetails in project incubator-rya by apache.

the class MongoPCJIndexIT method sparqlQuery_Test.

@Test
public void sparqlQuery_Test() throws Exception {
    // Setup a Rya Client.
    final MongoConnectionDetails connectionDetails = getConnectionDetails();
    final RyaClient ryaClient = MongoRyaClientFactory.build(connectionDetails, getMongoClient());
    final String pcjQuery = "SELECT ?name WHERE {" + " ?name <urn:likes> <urn:icecream> ." + " ?name <urn:hasEyeColor> <urn:blue> ." + " }";
    // Install an instance of Rya and load statements.
    ryaClient.getInstall().install(conf.getRyaInstanceName(), InstallConfiguration.builder().setEnablePcjIndex(true).build());
    ryaClient.getLoadStatements().loadStatements(conf.getRyaInstanceName(), getStatements());
    final String pcjId = ryaClient.getCreatePCJ().createPCJ(conf.getRyaInstanceName(), pcjQuery);
    ryaClient.getBatchUpdatePCJ().batchUpdate(conf.getRyaInstanceName(), pcjId);
    // purge contents of rya triples collection
    getMongoClient().getDatabase(conf.getRyaInstanceName()).getCollection(conf.getTriplesCollectionName()).drop();
    // run the query.  since the triples collection is gone, if the results match, they came from the PCJ index.
    conf.setBoolean(ConfigUtils.USE_PCJ, true);
    conf.setBoolean(ConfigUtils.USE_OPTIMAL_PCJ, true);
    conf.setBoolean(ConfigUtils.DISPLAY_QUERY_PLAN, true);
    final Sail sail = RyaSailFactory.getInstance(conf);
    SailRepositoryConnection conn = new SailRepository(sail).getConnection();
    conn.begin();
    final TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, pcjQuery);
    tupleQuery.setBinding(RdfCloudTripleStoreConfiguration.CONF_QUERYPLAN_FLAG, RdfCloudTripleStoreConstants.VALUE_FACTORY.createLiteral(true));
    final TupleQueryResult rez = tupleQuery.evaluate();
    final Set<BindingSet> results = new HashSet<>();
    while (rez.hasNext()) {
        final BindingSet bs = rez.next();
        results.add(bs);
    }
    // Verify the correct results were loaded into the PCJ table.
    final Set<BindingSet> expectedResults = new HashSet<>();
    MapBindingSet bs = new MapBindingSet();
    bs.addBinding("name", VF.createURI("urn:Alice"));
    expectedResults.add(bs);
    bs = new MapBindingSet();
    bs.addBinding("name", VF.createURI("urn:Bob"));
    expectedResults.add(bs);
    bs = new MapBindingSet();
    bs.addBinding("name", VF.createURI("urn:Charlie"));
    expectedResults.add(bs);
    bs = new MapBindingSet();
    bs.addBinding("name", VF.createURI("urn:David"));
    expectedResults.add(bs);
    bs = new MapBindingSet();
    bs.addBinding("name", VF.createURI("urn:Eve"));
    expectedResults.add(bs);
    bs = new MapBindingSet();
    bs.addBinding("name", VF.createURI("urn:Frank"));
    expectedResults.add(bs);
    assertEquals(6, results.size());
    assertEquals(expectedResults, results);
}
Also used : MongoConnectionDetails(org.apache.rya.api.client.mongo.MongoConnectionDetails) MapBindingSet(org.openrdf.query.impl.MapBindingSet) BindingSet(org.openrdf.query.BindingSet) SailRepository(org.openrdf.repository.sail.SailRepository) Sail(org.openrdf.sail.Sail) TupleQuery(org.openrdf.query.TupleQuery) RyaClient(org.apache.rya.api.client.RyaClient) MapBindingSet(org.openrdf.query.impl.MapBindingSet) SailRepositoryConnection(org.openrdf.repository.sail.SailRepositoryConnection) TupleQueryResult(org.openrdf.query.TupleQueryResult) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with MongoConnectionDetails

use of org.apache.rya.api.client.mongo.MongoConnectionDetails in project incubator-rya by apache.

the class RyaConnectionCommands method printConnectionDetails.

@CliCommand(value = PRINT_CONNECTION_DETAILS_CMD, help = "Print information about the Shell's Rya storage connection.")
public String printConnectionDetails() {
    // Check to see if the shell is connected to any storages.
    final com.google.common.base.Optional<StorageType> storageType = sharedState.getShellState().getStorageType();
    if (!storageType.isPresent()) {
        return "The shell is not connected to anything.";
    }
    // Create a print out based on what it is connected to.
    switch(storageType.get()) {
        case ACCUMULO:
            final AccumuloConnectionDetails accDetails = sharedState.getShellState().getAccumuloDetails().get();
            return "The shell is connected to an instance of Accumulo using the following parameters:\n" + "    Username: " + accDetails.getUsername() + "\n" + "    Instance Name: " + accDetails.getInstanceName() + "\n" + "    Zookeepers: " + accDetails.getZookeepers();
        case MONGO:
            final MongoConnectionDetails mongoDetails = sharedState.getShellState().getMongoDetails().get();
            final StringBuilder message = new StringBuilder().append("The shell is connected to an instance of MongoDB using the following parameters:\n").append("    Hostname: " + mongoDetails.getHostname() + "\n").append("    Port: " + mongoDetails.getPort() + "\n");
            if (mongoDetails.getUsername().isPresent()) {
                message.append("    Username: " + mongoDetails.getUsername().get() + "\n");
            }
            return message.toString();
        default:
            throw new RuntimeException("Unrecognized StorageType: " + storageType.get());
    }
}
Also used : MongoConnectionDetails(org.apache.rya.api.client.mongo.MongoConnectionDetails) StorageType(org.apache.rya.shell.SharedShellState.StorageType) AccumuloConnectionDetails(org.apache.rya.api.client.accumulo.AccumuloConnectionDetails) CliCommand(org.springframework.shell.core.annotation.CliCommand)

Example 4 with MongoConnectionDetails

use of org.apache.rya.api.client.mongo.MongoConnectionDetails in project incubator-rya by apache.

the class RyaConnectionCommands method connectToMongo.

@CliCommand(value = CONNECT_MONGO_CMD, help = "Connect the shell to an instance of MongoDB.")
public String connectToMongo(@CliOption(key = { "username" }, mandatory = false, help = "The username that will be used to connect to MongoDB when performing administrative tasks.") final String username, @CliOption(key = { "hostname" }, mandatory = true, help = "The hostname of the MongoDB that will be connected to.") final String hostname, @CliOption(key = { "port" }, mandatory = true, help = "The port of the MongoDB that will be connected to.") final String port) {
    try {
        // If a username was provided, then prompt for a password.
        char[] password = null;
        if (username != null) {
            password = passwordPrompt.getPassword();
        }
        // Create the Mongo Connection Details that describe the Mongo DB Server we are interacting with.
        final MongoConnectionDetails connectionDetails = new MongoConnectionDetails(hostname, Integer.parseInt(port), Optional.ofNullable(username), Optional.ofNullable(password));
        // Connect to a MongoDB server. TODO Figure out how to provide auth info?
        final MongoClient adminClient = new MongoClient(hostname, Integer.parseInt(port));
        // Make sure the client is closed at shutdown.
        Runtime.getRuntime().addShutdownHook(new Thread() {

            @Override
            public void run() {
                adminClient.close();
            }
        });
        try {
            // attempt to get the connection point, essentially pinging mongo server.
            adminClient.getConnectPoint();
        } catch (final MongoException e) {
            // had to rethrow to get scope on adminClient.
            adminClient.close();
            throw e;
        }
        // Initialize the connected to Mongo shared state.
        final RyaClient ryaClient = MongoRyaClientFactory.build(connectionDetails, adminClient);
        sharedState.connectedToMongo(connectionDetails, ryaClient);
    } catch (final IOException | MongoException e) {
        throw new RuntimeException("Could not connection to MongoDB. Reason: " + e.getMessage(), e);
    }
    return "Connected. You must select a Rya instance to interact with next.";
}
Also used : MongoConnectionDetails(org.apache.rya.api.client.mongo.MongoConnectionDetails) MongoClient(com.mongodb.MongoClient) MongoException(com.mongodb.MongoException) RyaClient(org.apache.rya.api.client.RyaClient) IOException(java.io.IOException) CliCommand(org.springframework.shell.core.annotation.CliCommand)

Example 5 with MongoConnectionDetails

use of org.apache.rya.api.client.mongo.MongoConnectionDetails in project incubator-rya by apache.

the class MongoPCJIndexIT method sparqlQuery_Test_complex.

@Test
public void sparqlQuery_Test_complex() throws Exception {
    // Setup a Rya Client.
    final MongoConnectionDetails connectionDetails = getConnectionDetails();
    final RyaClient ryaClient = MongoRyaClientFactory.build(connectionDetails, getMongoClient());
    final String pcjQuery = "SELECT ?name WHERE {" + " ?name <urn:likes> <urn:icecream> ." + " ?name <urn:hasEyeColor> <urn:blue> ." + " }";
    final String testQuery = "SELECT ?name WHERE {" + " ?name <urn:hasHairColor> <urn:brown> ." + " ?name <urn:likes> <urn:icecream> ." + " ?name <urn:hasEyeColor> <urn:blue> ." + " }";
    // Install an instance of Rya and load statements.
    conf.setBoolean(ConfigUtils.USE_PCJ, true);
    conf.setBoolean(ConfigUtils.USE_OPTIMAL_PCJ, true);
    conf.setBoolean(ConfigUtils.DISPLAY_QUERY_PLAN, true);
    ryaClient.getInstall().install(conf.getRyaInstanceName(), InstallConfiguration.builder().setEnablePcjIndex(true).build());
    ryaClient.getLoadStatements().loadStatements(conf.getRyaInstanceName(), getStatements());
    final String pcjId = ryaClient.getCreatePCJ().createPCJ(conf.getRyaInstanceName(), pcjQuery);
    ryaClient.getBatchUpdatePCJ().batchUpdate(conf.getRyaInstanceName(), pcjId);
    System.out.println("Triples: " + getMongoClient().getDatabase(conf.getRyaInstanceName()).getCollection(conf.getTriplesCollectionName()).count());
    System.out.println("PCJS: " + getMongoClient().getDatabase(conf.getRyaInstanceName()).getCollection("pcjs").count());
    // run the query.  since the triples collection is gone, if the results match, they came from the PCJ index.
    final Sail sail = RyaSailFactory.getInstance(conf);
    SailRepositoryConnection conn = new SailRepository(sail).getConnection();
    conn.begin();
    final TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, testQuery);
    tupleQuery.setBinding(RdfCloudTripleStoreConfiguration.CONF_QUERYPLAN_FLAG, RdfCloudTripleStoreConstants.VALUE_FACTORY.createLiteral(true));
    final TupleQueryResult rez = tupleQuery.evaluate();
    final Set<BindingSet> results = new HashSet<>();
    while (rez.hasNext()) {
        final BindingSet bs = rez.next();
        results.add(bs);
    }
    // Verify the correct results were loaded into the PCJ table.
    final Set<BindingSet> expectedResults = new HashSet<>();
    MapBindingSet bs = new MapBindingSet();
    bs = new MapBindingSet();
    bs.addBinding("name", VF.createURI("urn:David"));
    expectedResults.add(bs);
    bs = new MapBindingSet();
    bs.addBinding("name", VF.createURI("urn:Eve"));
    expectedResults.add(bs);
    bs = new MapBindingSet();
    bs.addBinding("name", VF.createURI("urn:Frank"));
    expectedResults.add(bs);
    assertEquals(3, results.size());
    assertEquals(expectedResults, results);
}
Also used : MongoConnectionDetails(org.apache.rya.api.client.mongo.MongoConnectionDetails) MapBindingSet(org.openrdf.query.impl.MapBindingSet) BindingSet(org.openrdf.query.BindingSet) SailRepository(org.openrdf.repository.sail.SailRepository) Sail(org.openrdf.sail.Sail) TupleQuery(org.openrdf.query.TupleQuery) RyaClient(org.apache.rya.api.client.RyaClient) MapBindingSet(org.openrdf.query.impl.MapBindingSet) SailRepositoryConnection(org.openrdf.repository.sail.SailRepositoryConnection) TupleQueryResult(org.openrdf.query.TupleQueryResult) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

MongoConnectionDetails (org.apache.rya.api.client.mongo.MongoConnectionDetails)5 RyaClient (org.apache.rya.api.client.RyaClient)4 Test (org.junit.Test)3 HashSet (java.util.HashSet)2 BindingSet (org.openrdf.query.BindingSet)2 TupleQuery (org.openrdf.query.TupleQuery)2 TupleQueryResult (org.openrdf.query.TupleQueryResult)2 MapBindingSet (org.openrdf.query.impl.MapBindingSet)2 SailRepository (org.openrdf.repository.sail.SailRepository)2 SailRepositoryConnection (org.openrdf.repository.sail.SailRepositoryConnection)2 Sail (org.openrdf.sail.Sail)2 CliCommand (org.springframework.shell.core.annotation.CliCommand)2 MongoClient (com.mongodb.MongoClient)1 MongoException (com.mongodb.MongoException)1 IOException (java.io.IOException)1 AccumuloConnectionDetails (org.apache.rya.api.client.accumulo.AccumuloConnectionDetails)1 StorageType (org.apache.rya.shell.SharedShellState.StorageType)1