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);
}
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);
}
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());
}
}
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.";
}
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);
}
Aggregations