Search in sources :

Example 1 with SailConnection

use of org.eclipse.rdf4j.sail.SailConnection in project incubator-rya by apache.

the class AccumuloBatchUpdatePCJ method updatePCJResults.

private void updatePCJResults(final String ryaInstanceName, final String pcjId) throws InstanceDoesNotExistException, PCJDoesNotExistException, RyaClientException {
    // Things that have to be closed before we exit.
    Sail sail = null;
    SailConnection sailConn = null;
    CloseableIteration<? extends BindingSet, QueryEvaluationException> results = null;
    try (final PrecomputedJoinStorage pcjStorage = new AccumuloPcjStorage(super.getConnector(), ryaInstanceName)) {
        // Create an instance of Sail backed by the Rya instance.
        sail = connectToRya(ryaInstanceName);
        // 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();
        final SPARQLParser parser = new SPARQLParser();
        final ParsedQuery parsedQuery = parser.parseQuery(sparql, null);
        // Execute the query.
        sailConn = sail.getConnection();
        results = sailConn.evaluate(parsedQuery.getTupleExpr(), null, null, false);
        // Load the results into the PCJ table.
        final List<VisibilityBindingSet> batch = new ArrayList<>(1000);
        while (results.hasNext()) {
            final VisibilityBindingSet result = new VisibilityBindingSet(results.next(), "");
            batch.add(result);
            if (batch.size() == 1000) {
                pcjStorage.addResults(pcjId, batch);
                batch.clear();
            }
        }
        if (!batch.isEmpty()) {
            pcjStorage.addResults(pcjId, batch);
            batch.clear();
        }
    } catch (final MalformedQueryException | PCJStorageException | SailException | QueryEvaluationException e) {
        throw new RyaClientException("Fail to batch load new results into the PCJ with ID '" + pcjId + "'.", e);
    } finally {
        if (results != null) {
            try {
                results.close();
            } catch (final QueryEvaluationException e) {
                log.warn(e.getMessage(), e);
            }
        }
        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);
            }
        }
    }
}
Also used : SPARQLParser(org.eclipse.rdf4j.query.parser.sparql.SPARQLParser) RyaClientException(org.apache.rya.api.client.RyaClientException) VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) AccumuloPcjStorage(org.apache.rya.indexing.pcj.storage.accumulo.AccumuloPcjStorage) ParsedQuery(org.eclipse.rdf4j.query.parser.ParsedQuery) ArrayList(java.util.ArrayList) SailException(org.eclipse.rdf4j.sail.SailException) SailConnection(org.eclipse.rdf4j.sail.SailConnection) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) Sail(org.eclipse.rdf4j.sail.Sail) PrecomputedJoinStorage(org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException) PcjMetadata(org.apache.rya.indexing.pcj.storage.PcjMetadata) PCJStorageException(org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage.PCJStorageException)

Example 2 with SailConnection

use of org.eclipse.rdf4j.sail.SailConnection in project incubator-rya by apache.

the class AccumuloAddUserIT method userNotAddedCanNotInsert.

/**
 * Ensure a user that has not been added to the Rya instance can not interact with it.
 */
@Test
public void userNotAddedCanNotInsert() throws Exception {
    final String user = testInstance.createUniqueUser();
    final SecurityOperations secOps = super.getConnector().securityOperations();
    final RyaClient userAClient = AccumuloRyaClientFactory.build(new AccumuloConnectionDetails(ADMIN_USER, ADMIN_USER.toCharArray(), getInstanceName(), getZookeepers()), super.getClusterInstance().getCluster().getConnector(ADMIN_USER, ADMIN_USER));
    // Install the instance of Rya.
    userAClient.getInstall().install(getRyaInstanceName(), InstallConfiguration.builder().build());
    // Create the user that will not be added to the instance of Rya, but will try to scan it.
    secOps.createLocalUser(user, new PasswordToken(user));
    // Try to add a statement the Rya instance with the unauthorized user. This should fail.
    boolean securityExceptionThrown = false;
    Sail sail = null;
    SailConnection sailConn = null;
    try {
        final AccumuloRdfConfiguration userCConf = makeRyaConfig(getRyaInstanceName(), user, user, getInstanceName(), getZookeepers());
        sail = RyaSailFactory.getInstance(userCConf);
        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);
}
Also used : SecurityOperations(org.apache.accumulo.core.client.admin.SecurityOperations) RyaClient(org.apache.rya.api.client.RyaClient) ValueFactory(org.eclipse.rdf4j.model.ValueFactory) AccumuloRdfConfiguration(org.apache.rya.accumulo.AccumuloRdfConfiguration) PasswordToken(org.apache.accumulo.core.client.security.tokens.PasswordToken) SailConnection(org.eclipse.rdf4j.sail.SailConnection) Sail(org.eclipse.rdf4j.sail.Sail) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException) Test(org.junit.Test)

Example 3 with SailConnection

use of org.eclipse.rdf4j.sail.SailConnection in project incubator-rya by apache.

the class AccumuloAddUserIT method userAddedCanInsert.

/**
 * Ensure a user that has been added to the Rya instance can interact with it.
 */
@Test
public void userAddedCanInsert() throws Exception {
    final String user = testInstance.createUniqueUser();
    final SecurityOperations secOps = super.getConnector().securityOperations();
    final RyaClient userAClient = AccumuloRyaClientFactory.build(new AccumuloConnectionDetails(ADMIN_USER, ADMIN_USER.toCharArray(), getInstanceName(), getZookeepers()), super.getClusterInstance().getCluster().getConnector(ADMIN_USER, ADMIN_USER));
    // Create the user that will not be added to the instance of Rya, but will try to scan it.
    secOps.createLocalUser(user, new PasswordToken(user));
    // Install the instance of Rya.
    userAClient.getInstall().install(getRyaInstanceName(), InstallConfiguration.builder().build());
    // Add the user.
    userAClient.getAddUser().get().addUser(getRyaInstanceName(), user);
    // Try to add a statement to the Rya instance. This should succeed.
    Sail sail = null;
    SailConnection sailConn = null;
    try {
        final AccumuloRdfConfiguration userDConf = makeRyaConfig(getRyaInstanceName(), user, user, getInstanceName(), getZookeepers());
        sail = RyaSailFactory.getInstance(userDConf);
        sailConn = sail.getConnection();
        final ValueFactory vf = sail.getValueFactory();
        sailConn.begin();
        sailConn.addStatement(vf.createIRI("urn:Alice"), vf.createIRI("urn:talksTo"), vf.createIRI("urn:Bob"));
        sailConn.close();
    } finally {
        if (sailConn != null) {
            sailConn.close();
        }
        if (sail != null) {
            sail.shutDown();
        }
    }
}
Also used : PasswordToken(org.apache.accumulo.core.client.security.tokens.PasswordToken) SailConnection(org.eclipse.rdf4j.sail.SailConnection) Sail(org.eclipse.rdf4j.sail.Sail) SecurityOperations(org.apache.accumulo.core.client.admin.SecurityOperations) RyaClient(org.apache.rya.api.client.RyaClient) ValueFactory(org.eclipse.rdf4j.model.ValueFactory) AccumuloRdfConfiguration(org.apache.rya.accumulo.AccumuloRdfConfiguration) Test(org.junit.Test)

Example 4 with SailConnection

use of org.eclipse.rdf4j.sail.SailConnection in project incubator-rya by apache.

the class AccumuloBatchUpdatePCJIT method batchUpdate.

@Test
public void batchUpdate() throws Exception {
    // Setup a Rya Client.
    final AccumuloConnectionDetails connectionDetails = new AccumuloConnectionDetails(super.getUsername(), super.getPassword().toCharArray(), super.getInstanceName(), super.getZookeepers());
    final RyaClient ryaClient = AccumuloRyaClientFactory.build(connectionDetails, super.getConnector());
    // Install an instance of Rya on the mini accumulo cluster.
    ryaClient.getInstall().install(RYA_INSTANCE_NAME, InstallConfiguration.builder().setEnablePcjIndex(true).build());
    Sail sail = null;
    try (final PrecomputedJoinStorage pcjStorage = new AccumuloPcjStorage(super.getConnector(), RYA_INSTANCE_NAME)) {
        // Get a Sail connection backed by the installed Rya instance.
        final AccumuloRdfConfiguration ryaConf = new AccumuloRdfConfiguration();
        ryaConf.setTablePrefix(RYA_INSTANCE_NAME);
        ryaConf.set(ConfigUtils.CLOUDBASE_USER, super.getUsername());
        ryaConf.set(ConfigUtils.CLOUDBASE_PASSWORD, super.getPassword());
        ryaConf.set(ConfigUtils.CLOUDBASE_ZOOKEEPERS, super.getZookeepers());
        ryaConf.set(ConfigUtils.CLOUDBASE_INSTANCE, super.getInstanceName());
        ryaConf.set(ConfigUtils.USE_PCJ, "true");
        ryaConf.set(ConfigUtils.PCJ_STORAGE_TYPE, PrecomputedJoinStorageType.ACCUMULO.toString());
        ryaConf.set(ConfigUtils.PCJ_UPDATER_TYPE, PrecomputedJoinUpdaterType.NO_UPDATE.toString());
        sail = RyaSailFactory.getInstance(ryaConf);
        // Load some statements into the Rya instance.
        final ValueFactory vf = sail.getValueFactory();
        final SailConnection sailConn = sail.getConnection();
        sailConn.begin();
        sailConn.addStatement(vf.createIRI("urn:Alice"), vf.createIRI("urn:likes"), vf.createIRI("urn:icecream"));
        sailConn.addStatement(vf.createIRI("urn:Bob"), vf.createIRI("urn:likes"), vf.createIRI("urn:icecream"));
        sailConn.addStatement(vf.createIRI("urn:Charlie"), vf.createIRI("urn:likes"), vf.createIRI("urn:icecream"));
        sailConn.addStatement(vf.createIRI("urn:David"), vf.createIRI("urn:likes"), vf.createIRI("urn:icecream"));
        sailConn.addStatement(vf.createIRI("urn:Eve"), vf.createIRI("urn:likes"), vf.createIRI("urn:icecream"));
        sailConn.addStatement(vf.createIRI("urn:Frank"), vf.createIRI("urn:likes"), vf.createIRI("urn:icecream"));
        sailConn.addStatement(vf.createIRI("urn:George"), vf.createIRI("urn:likes"), vf.createIRI("urn:icecream"));
        sailConn.addStatement(vf.createIRI("urn:Hillary"), vf.createIRI("urn:likes"), vf.createIRI("urn:icecream"));
        sailConn.addStatement(vf.createIRI("urn:Alice"), vf.createIRI("urn:hasEyeColor"), vf.createIRI("urn:blue"));
        sailConn.addStatement(vf.createIRI("urn:Bob"), vf.createIRI("urn:hasEyeColor"), vf.createIRI("urn:blue"));
        sailConn.addStatement(vf.createIRI("urn:Charlie"), vf.createIRI("urn:hasEyeColor"), vf.createIRI("urn:blue"));
        sailConn.addStatement(vf.createIRI("urn:David"), vf.createIRI("urn:hasEyeColor"), vf.createIRI("urn:blue"));
        sailConn.addStatement(vf.createIRI("urn:Eve"), vf.createIRI("urn:hasEyeColor"), vf.createIRI("urn:blue"));
        sailConn.addStatement(vf.createIRI("urn:Frank"), vf.createIRI("urn:hasEyeColor"), vf.createIRI("urn:blue"));
        sailConn.addStatement(vf.createIRI("urn:George"), vf.createIRI("urn:hasEyeColor"), vf.createIRI("urn:green"));
        sailConn.addStatement(vf.createIRI("urn:Hillary"), vf.createIRI("urn:hasEyeColor"), vf.createIRI("urn:brown"));
        sailConn.commit();
        sailConn.close();
        // Create a PCJ for a SPARQL query.
        final String sparql = "SELECT ?name WHERE { ?name <urn:likes> <urn:icecream> . ?name <urn:hasEyeColor> <urn:blue> . }";
        final String pcjId = pcjStorage.createPcj(sparql);
        // Run the test.
        ryaClient.getBatchUpdatePCJ().batchUpdate(RYA_INSTANCE_NAME, pcjId);
        // Verify the correct results were loaded into the PCJ table.
        final Set<BindingSet> expectedResults = new HashSet<>();
        MapBindingSet bs = new MapBindingSet();
        bs.addBinding("name", vf.createIRI("urn:Alice"));
        expectedResults.add(bs);
        bs = new MapBindingSet();
        bs.addBinding("name", vf.createIRI("urn:Bob"));
        expectedResults.add(bs);
        bs = new MapBindingSet();
        bs.addBinding("name", vf.createIRI("urn:Charlie"));
        expectedResults.add(bs);
        bs = new MapBindingSet();
        bs.addBinding("name", vf.createIRI("urn:David"));
        expectedResults.add(bs);
        bs = new MapBindingSet();
        bs.addBinding("name", vf.createIRI("urn:Eve"));
        expectedResults.add(bs);
        bs = new MapBindingSet();
        bs.addBinding("name", vf.createIRI("urn:Frank"));
        expectedResults.add(bs);
        final Set<BindingSet> results = new HashSet<>();
        try (CloseableIterator<BindingSet> resultsIt = pcjStorage.listResults(pcjId)) {
            while (resultsIt.hasNext()) {
                results.add(resultsIt.next());
            }
        }
        assertEquals(expectedResults, results);
    } finally {
        if (sail != null) {
            sail.shutDown();
        }
    }
}
Also used : BindingSet(org.eclipse.rdf4j.query.BindingSet) MapBindingSet(org.eclipse.rdf4j.query.impl.MapBindingSet) AccumuloPcjStorage(org.apache.rya.indexing.pcj.storage.accumulo.AccumuloPcjStorage) RyaClient(org.apache.rya.api.client.RyaClient) ValueFactory(org.eclipse.rdf4j.model.ValueFactory) AccumuloRdfConfiguration(org.apache.rya.accumulo.AccumuloRdfConfiguration) SailConnection(org.eclipse.rdf4j.sail.SailConnection) Sail(org.eclipse.rdf4j.sail.Sail) PrecomputedJoinStorage(org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage) MapBindingSet(org.eclipse.rdf4j.query.impl.MapBindingSet) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with SailConnection

use of org.eclipse.rdf4j.sail.SailConnection in project incubator-rya by apache.

the class RyaSinkTaskTest method flushBetweenPuts.

@Test
public void flushBetweenPuts() {
    // 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 with flushes between them.
        task.put(Collections.singleton(new SinkRecord("topic", 1, null, "key", null, batch1, 0)));
        task.flush(new HashMap<>());
        task.put(Collections.singleton(new SinkRecord("topic", 1, null, "key", null, batch2, 1)));
        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();
    }
}
Also used : HashMap(java.util.HashMap) Statement(org.eclipse.rdf4j.model.Statement) ValueFactory(org.eclipse.rdf4j.model.ValueFactory) SimpleValueFactory(org.eclipse.rdf4j.model.impl.SimpleValueFactory) SailException(org.eclipse.rdf4j.sail.SailException) SinkRecord(org.apache.kafka.connect.sink.SinkRecord) SailConnection(org.eclipse.rdf4j.sail.SailConnection) Sail(org.eclipse.rdf4j.sail.Sail) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

SailConnection (org.eclipse.rdf4j.sail.SailConnection)12 Sail (org.eclipse.rdf4j.sail.Sail)11 ValueFactory (org.eclipse.rdf4j.model.ValueFactory)9 Test (org.junit.Test)7 AccumuloRdfConfiguration (org.apache.rya.accumulo.AccumuloRdfConfiguration)5 RyaClient (org.apache.rya.api.client.RyaClient)5 Statement (org.eclipse.rdf4j.model.Statement)5 SailException (org.eclipse.rdf4j.sail.SailException)5 HashSet (java.util.HashSet)4 HashMap (java.util.HashMap)3 SecurityOperations (org.apache.accumulo.core.client.admin.SecurityOperations)3 PasswordToken (org.apache.accumulo.core.client.security.tokens.PasswordToken)3 SinkRecord (org.apache.kafka.connect.sink.SinkRecord)3 PrecomputedJoinStorage (org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage)3 SimpleValueFactory (org.eclipse.rdf4j.model.impl.SimpleValueFactory)3 QueryEvaluationException (org.eclipse.rdf4j.query.QueryEvaluationException)3 ArrayList (java.util.ArrayList)2 AccumuloSecurityException (org.apache.accumulo.core.client.AccumuloSecurityException)2 RyaClientException (org.apache.rya.api.client.RyaClientException)2 VisibilityBindingSet (org.apache.rya.api.model.VisibilityBindingSet)2