use of org.eclipse.rdf4j.sail.SailConnection in project incubator-rya by apache.
the class RyaSinkTaskTest method multipleRecords.
@Test
public void multipleRecords() {
// Create the Statements that will be put by the task.
final ValueFactory vf = SimpleValueFactory.getInstance();
final Set<Statement> batch1 = Sets.newHashSet(vf.createStatement(vf.createIRI("urn:Alice"), vf.createIRI("urn:WorksAt"), vf.createIRI("urn:Taco Shop"), vf.createIRI("urn:graph1")), vf.createStatement(vf.createIRI("urn:Bob"), vf.createIRI("urn:TalksTo"), vf.createIRI("urn:Charlie"), vf.createIRI("urn:graph2")));
final Set<Statement> batch2 = Sets.newHashSet(vf.createStatement(vf.createIRI("urn:Eve"), vf.createIRI("urn:ListensTo"), vf.createIRI("urn:Alice"), vf.createIRI("urn:graph1")));
// Create the task that will be tested.
final InMemoryRyaSinkTask task = new InMemoryRyaSinkTask();
// Setup the properties that will be used to configure the task. We don't actually need to set anything
// here since we're always returning true for ryaInstanceExists(...) and use an in memory RDF store.
final Map<String, String> props = new HashMap<>();
try {
// Start the task.
task.start(props);
// Put the statements as SinkRecords.
final Collection<SinkRecord> records = Sets.newHashSet(new SinkRecord("topic", 1, null, "key", null, batch1, 0), new SinkRecord("topic", 1, null, "key", null, batch2, 1));
task.put(records);
// Flush the statements.
task.flush(new HashMap<>());
// Fetch the stored Statements to show they match the original set.
final Set<Statement> fetched = new HashSet<>();
final Sail sail = task.makeSail(props);
try (SailConnection conn = sail.getConnection();
CloseableIteration<? extends Statement, SailException> it = conn.getStatements(null, null, null, false)) {
while (it.hasNext()) {
fetched.add(it.next());
}
}
assertEquals(Sets.union(batch1, batch2), fetched);
} finally {
// Stop the task.
task.stop();
}
}
use of org.eclipse.rdf4j.sail.SailConnection in project incubator-rya by apache.
the class ProspectorExample method main.
public static void main(final String[] args) throws Exception {
setupLogging();
// Configure Rya to use a mock instance.
final AccumuloRdfConfiguration config = new AccumuloRdfConfiguration();
config.useMockInstance(true);
config.setTablePrefix("rya_");
config.setUsername("user");
config.setPassword("pass");
config.setInstanceName("accumulo");
// Load some data into Rya.
final List<Statement> statements = Lists.newArrayList(VALUE_FACTORY.createStatement(ALICE, WORKS_AT, BURGER_JOINT), VALUE_FACTORY.createStatement(ALICE, ADMIRES, BOB), VALUE_FACTORY.createStatement(BOB, WORKS_AT, DONUT_SHOP), VALUE_FACTORY.createStatement(CHARLIE, WORKS_AT, DONUT_SHOP), VALUE_FACTORY.createStatement(CHARLIE, LIVES_WITH, BOB), VALUE_FACTORY.createStatement(BOB, LIVES_WITH, CHARLIE), VALUE_FACTORY.createStatement(BOB, LIVES_WITH, ALICE));
final Sail sail = RyaSailFactory.getInstance(config);
final SailConnection conn = sail.getConnection();
log.info("Loading the following statements into a Mock instance of Accumulo Rya:");
conn.begin();
for (final Statement statement : statements) {
log.info(" " + statement.toString());
conn.addStatement(statement.getSubject(), statement.getPredicate(), statement.getObject());
}
conn.commit();
conn.close();
// Create the table that the Prospector's results will be written to.
ConnectorFactory.connect(config).tableOperations().create("rya_prospects");
// Run the Prospector using the configuration file that is in the resources directory.
log.info("");
log.info("Running the Map Reduce job that computes the Prospector results.");
ToolRunner.run(new Prospector(), new String[] { "src/main/resources/stats_cluster_config.xml" });
// Print the table that was created by the Prospector.
log.info("");
log.info("The following cardinalities were written to the Prospector table:");
final ProspectorServiceEvalStatsDAO dao = ProspectorServiceEvalStatsDAO.make(config);
// Do each of the Subjects.
double cardinality = dao.getCardinality(config, CARDINALITY_OF.SUBJECT, Lists.newArrayList(ALICE));
log.info(" subject: " + ALICE + ", cardinality: " + cardinality);
cardinality = dao.getCardinality(config, CARDINALITY_OF.SUBJECT, Lists.newArrayList(BOB));
log.info(" subject: " + BOB + ", cardinality: " + cardinality);
cardinality = dao.getCardinality(config, CARDINALITY_OF.SUBJECT, Lists.newArrayList(CHARLIE));
log.info(" subject: " + CHARLIE + ", cardinality: " + cardinality);
// Do each of the Predicates.
cardinality = dao.getCardinality(config, CARDINALITY_OF.PREDICATE, Lists.newArrayList(WORKS_AT));
log.info(" predicate: " + WORKS_AT + ", cardinality: " + cardinality);
cardinality = dao.getCardinality(config, CARDINALITY_OF.PREDICATE, Lists.newArrayList(ADMIRES));
log.info(" predicate: " + ADMIRES + ", cardinality: " + cardinality);
cardinality = dao.getCardinality(config, CARDINALITY_OF.PREDICATE, Lists.newArrayList(LIVES_WITH));
log.info(" predicate: " + LIVES_WITH + ", cardinality: " + cardinality);
// Do each of the Objects.
cardinality = dao.getCardinality(config, CARDINALITY_OF.OBJECT, Lists.newArrayList(BURGER_JOINT));
log.info(" object: " + BURGER_JOINT + ", cardinality: " + cardinality);
cardinality = dao.getCardinality(config, CARDINALITY_OF.OBJECT, Lists.newArrayList(DONUT_SHOP));
log.info(" object: " + DONUT_SHOP + ", cardinality: " + cardinality);
cardinality = dao.getCardinality(config, CARDINALITY_OF.OBJECT, Lists.newArrayList(ALICE));
log.info(" object: " + ALICE + ", cardinality: " + cardinality);
cardinality = dao.getCardinality(config, CARDINALITY_OF.OBJECT, Lists.newArrayList(BOB));
log.info(" object: " + BOB + ", cardinality: " + cardinality);
cardinality = dao.getCardinality(config, CARDINALITY_OF.OBJECT, Lists.newArrayList(CHARLIE));
log.info(" object: " + CHARLIE + ", cardinality: " + cardinality);
// Do each of the Subject/Predicate pairs.
cardinality = dao.getCardinality(config, CARDINALITY_OF.SUBJECTPREDICATE, Lists.newArrayList(ALICE, WORKS_AT));
log.info(" subject/predicate: " + ALICE + "/" + WORKS_AT + ", cardinality: " + cardinality);
cardinality = dao.getCardinality(config, CARDINALITY_OF.SUBJECTPREDICATE, Lists.newArrayList(ALICE, ADMIRES));
log.info(" subject/predicate: " + ALICE + "/" + ADMIRES + ", cardinality: " + cardinality);
cardinality = dao.getCardinality(config, CARDINALITY_OF.SUBJECTPREDICATE, Lists.newArrayList(BOB, WORKS_AT));
log.info(" subject/predicate: " + BOB + "/" + WORKS_AT + ", cardinality: " + cardinality);
cardinality = dao.getCardinality(config, CARDINALITY_OF.SUBJECTPREDICATE, Lists.newArrayList(CHARLIE, WORKS_AT));
log.info(" subject/predicate: " + CHARLIE + "/" + WORKS_AT + ", cardinality: " + cardinality);
cardinality = dao.getCardinality(config, CARDINALITY_OF.SUBJECTPREDICATE, Lists.newArrayList(CHARLIE, LIVES_WITH));
log.info(" subject/predicate: " + CHARLIE + "/" + LIVES_WITH + ", cardinality: " + cardinality);
cardinality = dao.getCardinality(config, CARDINALITY_OF.SUBJECTPREDICATE, Lists.newArrayList(BOB, LIVES_WITH));
log.info(" subject/predicate: " + BOB + "/" + LIVES_WITH + ", cardinality: " + cardinality);
// Do each of the Subject/Object pairs.
cardinality = dao.getCardinality(config, CARDINALITY_OF.SUBJECTOBJECT, Lists.newArrayList(ALICE, BURGER_JOINT));
log.info(" subject/object: " + ALICE + "/" + BURGER_JOINT + ", cardinality: " + cardinality);
cardinality = dao.getCardinality(config, CARDINALITY_OF.SUBJECTOBJECT, Lists.newArrayList(ALICE, BOB));
log.info(" subject/object: " + ALICE + "/" + BOB + ", cardinality: " + cardinality);
cardinality = dao.getCardinality(config, CARDINALITY_OF.SUBJECTOBJECT, Lists.newArrayList(BOB, DONUT_SHOP));
log.info(" subject/object: " + ALICE + "/" + DONUT_SHOP + ", cardinality: " + cardinality);
cardinality = dao.getCardinality(config, CARDINALITY_OF.SUBJECTOBJECT, Lists.newArrayList(CHARLIE, DONUT_SHOP));
log.info(" subject/object: " + CHARLIE + "/" + DONUT_SHOP + ", cardinality: " + cardinality);
cardinality = dao.getCardinality(config, CARDINALITY_OF.SUBJECTOBJECT, Lists.newArrayList(CHARLIE, BOB));
log.info(" subject/object: " + CHARLIE + "/" + BOB + ", cardinality: " + cardinality);
cardinality = dao.getCardinality(config, CARDINALITY_OF.SUBJECTOBJECT, Lists.newArrayList(BOB, CHARLIE));
log.info(" subject/object: " + BOB + "/" + CHARLIE + ", cardinality: " + cardinality);
cardinality = dao.getCardinality(config, CARDINALITY_OF.SUBJECTOBJECT, Lists.newArrayList(BOB, ALICE));
log.info(" subject/object: " + BOB + "/" + ALICE + ", cardinality: " + cardinality);
// Do each of the Predicate/Object pairs.
cardinality = dao.getCardinality(config, CARDINALITY_OF.PREDICATEOBJECT, Lists.newArrayList(WORKS_AT, BURGER_JOINT));
log.info(" predicate/object: " + WORKS_AT + "/" + BURGER_JOINT + ", cardinality: " + cardinality);
cardinality = dao.getCardinality(config, CARDINALITY_OF.PREDICATEOBJECT, Lists.newArrayList(ADMIRES, BOB));
log.info(" predicate/object: " + ADMIRES + "/" + BOB + ", cardinality: " + cardinality);
cardinality = dao.getCardinality(config, CARDINALITY_OF.PREDICATEOBJECT, Lists.newArrayList(WORKS_AT, DONUT_SHOP));
log.info(" predicate/object: " + WORKS_AT + "/" + DONUT_SHOP + ", cardinality: " + cardinality);
cardinality = dao.getCardinality(config, CARDINALITY_OF.PREDICATEOBJECT, Lists.newArrayList(LIVES_WITH, BOB));
log.info(" predicate/object: " + LIVES_WITH + "/" + BOB + ", cardinality: " + cardinality);
cardinality = dao.getCardinality(config, CARDINALITY_OF.PREDICATEOBJECT, Lists.newArrayList(LIVES_WITH, CHARLIE));
log.info(" predicate/object: " + LIVES_WITH + "/" + CHARLIE + ", cardinality: " + cardinality);
cardinality = dao.getCardinality(config, CARDINALITY_OF.PREDICATEOBJECT, Lists.newArrayList(LIVES_WITH, ALICE));
log.info(" predicate/object: " + LIVES_WITH + "/" + ALICE + ", cardinality: " + cardinality);
}
use of org.eclipse.rdf4j.sail.SailConnection in project incubator-rya by apache.
the class MongoBatchUpdatePCJ method updatePCJResults.
private void updatePCJResults(final String ryaInstanceName, final String pcjId, final MongoClient client) throws InstanceDoesNotExistException, PCJDoesNotExistException, RyaClientException {
// Things that have to be closed before we exit.
Sail sail = null;
SailConnection sailConn = null;
try (final PrecomputedJoinStorage pcjStorage = new MongoPcjStorage(client, ryaInstanceName)) {
// Create an instance of Sail backed by the Rya instance.
sail = connectToRya(ryaInstanceName);
final SailRepository sailRepo = new SailRepository(sail);
final SailRepositoryConnection sailRepoConn = sailRepo.getConnection();
// Purge the old results from the PCJ.
try {
pcjStorage.purge(pcjId);
} catch (final PCJStorageException e) {
throw new RyaClientException("Could not batch update PCJ with ID '" + pcjId + "' because the old " + "results could not be purged from it.", e);
}
// Parse the PCJ's SPARQL query.
final PcjMetadata metadata = pcjStorage.getPcjMetadata(pcjId);
final String sparql = metadata.getSparql();
sailConn = sail.getConnection();
final TupleQuery tupleQuery = sailRepoConn.prepareTupleQuery(QueryLanguage.SPARQL, sparql);
// Execute the query.
final List<VisibilityBindingSet> batch = new ArrayList<>(1000);
tupleQuery.evaluate(new AbstractTupleQueryResultHandler() {
@Override
public void handleSolution(final BindingSet bindingSet) throws TupleQueryResultHandlerException {
final VisibilityBindingSet result = new VisibilityBindingSet(bindingSet, "");
log.warn("Visibility information on the binding set is lost during a batch update." + " This can create data leaks.");
batch.add(result);
if (batch.size() == 1000) {
try {
pcjStorage.addResults(pcjId, batch);
} catch (final PCJStorageException e) {
throw new TupleQueryResultHandlerException("Fail to batch load new results into the PCJ with ID '" + pcjId + "'.", e);
}
batch.clear();
}
}
});
if (!batch.isEmpty()) {
pcjStorage.addResults(pcjId, batch);
batch.clear();
}
} catch (final MalformedQueryException | PCJStorageException | SailException | QueryEvaluationException | RepositoryException | TupleQueryResultHandlerException e) {
throw new RyaClientException("Fail to batch load new results into the PCJ with ID '" + pcjId + "'.", e);
} finally {
if (sailConn != null) {
try {
sailConn.close();
} catch (final SailException e) {
log.warn(e.getMessage(), e);
}
}
if (sail != null) {
try {
sail.shutDown();
} catch (final SailException e) {
log.warn(e.getMessage(), e);
}
}
}
}
use of org.eclipse.rdf4j.sail.SailConnection in project incubator-rya by apache.
the class AccumuloRemoveUserIT method removedUserCanNotInsert.
/**
* Ensure a user that has been removed from the Rya instance can not interact with it.
*/
@Test
public void removedUserCanNotInsert() throws Exception {
final String adminUser = testInstance.createUniqueUser();
final String user = testInstance.createUniqueUser();
final SecurityOperations secOps = super.getConnector().securityOperations();
// Create the user that will install the instance of Rya.
secOps.createLocalUser(adminUser, new PasswordToken(adminUser));
secOps.grantSystemPermission(adminUser, SystemPermission.CREATE_TABLE);
final RyaClient userAClient = AccumuloRyaClientFactory.build(new AccumuloConnectionDetails(adminUser, adminUser.toCharArray(), getInstanceName(), getZookeepers()), super.getClusterInstance().getCluster().getConnector(adminUser, adminUser));
// Create the user that will be added to the instance of Rya.
secOps.createLocalUser(user, new PasswordToken(user));
final RyaClient userCClient = AccumuloRyaClientFactory.build(new AccumuloConnectionDetails(user, user.toCharArray(), getInstanceName(), getZookeepers()), super.getClusterInstance().getCluster().getConnector(user, user));
// Install the instance of Rya.
userAClient.getInstall().install(getRyaInstanceName(), InstallConfiguration.builder().build());
// Add userC.
userAClient.getAddUser().get().addUser(getRyaInstanceName(), user);
// Remove userA.
userCClient.getRemoveUser().get().removeUser(getRyaInstanceName(), adminUser);
// Show that userA can not insert anything.
boolean securityExceptionThrown = false;
Sail sail = null;
SailConnection sailConn = null;
try {
final AccumuloRdfConfiguration userAConf = makeRyaConfig(getRyaInstanceName(), adminUser, adminUser, getInstanceName(), getZookeepers());
sail = RyaSailFactory.getInstance(userAConf);
sailConn = sail.getConnection();
final ValueFactory vf = sail.getValueFactory();
sailConn.addStatement(vf.createIRI("urn:Alice"), vf.createIRI("urn:talksTo"), vf.createIRI("urn:Bob"));
} catch (final RuntimeException e) {
final Throwable cause = e.getCause();
if (cause instanceof AccumuloSecurityException) {
securityExceptionThrown = true;
}
} finally {
if (sailConn != null) {
sailConn.close();
}
if (sail != null) {
sail.shutDown();
}
}
assertTrue(securityExceptionThrown);
}
use of org.eclipse.rdf4j.sail.SailConnection in project incubator-rya by apache.
the class RyaClientExample method main.
public static void main(final String[] args) throws Exception {
final String accumuloUsername = "root";
final String accumuloPassword = "password";
MiniAccumuloCluster cluster = null;
MiniFluo fluo = null;
Sail ryaSail = null;
try {
// Setup a Mini Accumulo Cluster to host the Rya instance.
log.info("Setting up the Mini Accumulo Cluster used by this example.");
final File miniDataDir = Files.createTempDir();
final MiniAccumuloConfig cfg = new MiniAccumuloConfig(miniDataDir, accumuloPassword);
cluster = new MiniAccumuloCluster(cfg);
cluster.start();
// Setup a Mini Fluo application that will be used to incrementally update the PCJ indicies.
log.info("Setting up the Mini Fluo application used by this example.");
final String fluoAppName = "demoInstance_pcjUpdater";
fluo = makeMiniFluo(accumuloUsername, accumuloPassword, cluster.getInstanceName(), cluster.getZooKeepers(), fluoAppName);
// Give the root user the 'U' authorizations.
final Connector connector = cluster.getConnector(accumuloUsername, accumuloPassword);
connector.securityOperations().changeUserAuthorizations(accumuloUsername, new Authorizations("U"));
// Setup a Rya Client that is able to interact with the mini cluster.
final AccumuloConnectionDetails connectionDetails = new AccumuloConnectionDetails(accumuloUsername, accumuloPassword.toCharArray(), cluster.getInstanceName(), cluster.getZooKeepers());
final RyaClient ryaClient = AccumuloRyaClientFactory.build(connectionDetails, connector);
// Install an instance of Rya that has all of the secondary indexers turned on.
final String ryaInstanceName = "demoInstance_";
final InstallConfiguration installConfig = InstallConfiguration.builder().setEnableTableHashPrefix(true).setEnableEntityCentricIndex(true).setEnableGeoIndex(true).setEnableFreeTextIndex(true).setEnableTemporalIndex(true).setEnablePcjIndex(true).setFluoPcjAppName(fluoAppName).build();
ryaClient.getInstall().install(ryaInstanceName, installConfig);
// Add a PCJ index.
final String sparql = "SELECT ?patron ?employee " + "WHERE { " + "?patron <http://talksTo> ?employee. " + "?employee <http://worksAt> <http://CoffeeShop>. " + "}";
// Load some statements into the Rya instance.
final AccumuloIndexingConfiguration conf = AccumuloIndexingConfiguration.builder().setAuths("U").setAccumuloUser(accumuloUsername).setAccumuloPassword(accumuloPassword).setAccumuloInstance(cluster.getInstanceName()).setAccumuloZooKeepers(cluster.getZooKeepers()).setRyaPrefix(ryaInstanceName).setPcjUpdaterFluoAppName(fluoAppName).build();
ryaSail = RyaSailFactory.getInstance(conf);
final ValueFactory vf = ryaSail.getValueFactory();
final List<Statement> statements = Lists.newArrayList(vf.createStatement(vf.createIRI("http://Eve"), vf.createIRI("http://talksTo"), vf.createIRI("http://Charlie")), vf.createStatement(vf.createIRI("http://David"), vf.createIRI("http://talksTo"), vf.createIRI("http://Alice")), vf.createStatement(vf.createIRI("http://Alice"), vf.createIRI("http://worksAt"), vf.createIRI("http://CoffeeShop")), vf.createStatement(vf.createIRI("http://Bob"), vf.createIRI("http://worksAt"), vf.createIRI("http://CoffeeShop")), vf.createStatement(vf.createIRI("http://George"), vf.createIRI("http://talksTo"), vf.createIRI("http://Frank")), vf.createStatement(vf.createIRI("http://Frank"), vf.createIRI("http://worksAt"), vf.createIRI("http://CoffeeShop")), vf.createStatement(vf.createIRI("http://Eve"), vf.createIRI("http://talksTo"), vf.createIRI("http://Bob")), vf.createStatement(vf.createIRI("http://Charlie"), vf.createIRI("http://worksAt"), vf.createIRI("http://CoffeeShop")));
SailConnection ryaConn = ryaSail.getConnection();
log.info("");
log.info("Loading the following statements:");
ryaConn.begin();
for (final Statement statement : statements) {
log.info(" " + statement.toString());
ryaConn.addStatement(statement.getSubject(), statement.getPredicate(), statement.getObject());
}
log.info("");
ryaConn.close();
fluo.waitForObservers();
// Execute the SPARQL query and print the results.
log.info("Executing the following query: ");
prettyLogSparql(sparql);
log.info("");
final ParsedQuery parsedQuery = new SPARQLParser().parseQuery(sparql, null);
ryaConn = ryaSail.getConnection();
final CloseableIteration<? extends BindingSet, QueryEvaluationException> result = ryaConn.evaluate(parsedQuery.getTupleExpr(), null, null, false);
log.info("Results:");
while (result.hasNext()) {
log.info(" " + result.next());
}
log.info("");
} finally {
if (ryaSail != null) {
log.info("Shutting down the Rya Sail instance.");
ryaSail.shutDown();
}
if (fluo != null) {
try {
log.info("Shutting down the Mini Fluo instance.");
fluo.close();
} catch (final Exception e) {
log.error("Could not shut down the Mini Fluo instance.", e);
}
}
if (cluster != null) {
log.info("Sutting down the Mini Accumulo Cluster.");
cluster.stop();
}
}
}
Aggregations