Search in sources :

Example 21 with RepositoryConnection

use of org.openrdf.repository.RepositoryConnection in project incubator-rya by apache.

the class RdfCloudTripleStoreConnectionTest method testUpdateWAuthOnConfig.

// Set the persistence visibilites on the config
public void testUpdateWAuthOnConfig() throws Exception {
    String sparqlUpdate = getSparqlUpdate();
    RdfCloudTripleStore tstore = new MockRdfCloudStore();
    NamespaceManager nm = new NamespaceManager(tstore.getRyaDAO(), tstore.getConf());
    tstore.setNamespaceManager(nm);
    SailRepository repo = new SailRepository(tstore);
    tstore.getRyaDAO().getConf().setCv("1|2");
    repo.initialize();
    RepositoryConnection conn = repo.getConnection();
    Update u = conn.prepareUpdate(QueryLanguage.SPARQL, sparqlUpdate);
    u.execute();
    String query = "PREFIX  ex:  <http://www.example.org/exampleDocument#>\n" + "PREFIX  voc:  <http://www.example.org/vocabulary#>\n" + "PREFIX  foaf:  <http://xmlns.com/foaf/0.1/>\n" + "PREFIX  rdfs:  <http://www.w3.org/2000/01/rdf-schema#>\n" + "\n" + "SELECT * \n" + // "FROM NAMED <http://www.example.org/exampleDocument#G1>\n" +
    "WHERE\n" + "{\n" + "  GRAPH ex:G1\n" + "  {\n" + "    ?m voc:name ?name ;\n" + "           voc:homepage ?hp .\n" + "  } .\n" + " GRAPH ex:G2\n" + "  {\n" + "    ?m voc:hasSkill ?skill .\n" + "  } .\n" + "}";
    TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
    tupleQuery.setBinding(RdfCloudTripleStoreConfiguration.CONF_QUERY_AUTH, vf.createLiteral("2"));
    CountTupleHandler tupleHandler = new CountTupleHandler();
    tupleQuery.evaluate(tupleHandler);
    assertEquals(1, tupleHandler.getCount());
    // no auth
    tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
    tupleHandler = new CountTupleHandler();
    tupleQuery.evaluate(tupleHandler);
    assertEquals(0, tupleHandler.getCount());
    conn.close();
    repo.shutDown();
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) RdfCloudTripleStore(org.apache.rya.rdftriplestore.RdfCloudTripleStore) NamespaceManager(org.apache.rya.rdftriplestore.namespace.NamespaceManager) RyaSailRepository(org.apache.rya.rdftriplestore.RyaSailRepository) SailRepository(org.openrdf.repository.sail.SailRepository) TupleQuery(org.openrdf.query.TupleQuery) Update(org.openrdf.query.Update)

Example 22 with RepositoryConnection

use of org.openrdf.repository.RepositoryConnection in project incubator-rya by apache.

the class RdfCloudTripleStoreConnectionTest method testEvaluateMultiLine.

public void testEvaluateMultiLine() throws Exception {
    RepositoryConnection conn = repository.getConnection();
    URI loadPerc = vf.createURI(litdupsNS, "loadPerc");
    URI uri1 = vf.createURI(litdupsNS, "uri1");
    URI pred2 = vf.createURI(litdupsNS, "pred2");
    URI uri2 = vf.createURI(litdupsNS, "uri2");
    conn.add(cpu, loadPerc, uri1);
    conn.add(cpu, pred2, uri2);
    conn.commit();
    String query = "select * where {" + "?x <" + loadPerc.stringValue() + "> ?o1." + "?x <" + pred2.stringValue() + "> ?o2." + "}";
    TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
    tupleQuery.setBinding(RdfCloudTripleStoreConfiguration.CONF_QUERYPLAN_FLAG, RdfCloudTripleStoreConstants.VALUE_FACTORY.createLiteral(true));
    CountTupleHandler cth = new CountTupleHandler();
    tupleQuery.evaluate(cth);
    conn.close();
    assertEquals(cth.getCount(), 1);
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) TupleQuery(org.openrdf.query.TupleQuery) URI(org.openrdf.model.URI)

Example 23 with RepositoryConnection

use of org.openrdf.repository.RepositoryConnection in project incubator-rya by apache.

the class PcjVisibilityIT method visibilitySimplified.

@Test
public void visibilitySimplified() throws Exception {
    // Create a PCJ index within Rya.
    final String sparql = "SELECT ?customer ?worker ?city " + "{ " + "?customer <" + TALKS_TO + "> ?worker. " + "?worker <" + LIVES_IN + "> ?city. " + "?worker <" + WORKS_AT + "> <" + BURGER_JOINT + ">. " + "}";
    final Connector accumuloConn = super.getAccumuloConnector();
    final String instanceName = super.getMiniAccumuloCluster().getInstanceName();
    final String zookeepers = super.getMiniAccumuloCluster().getZooKeepers();
    final RyaClient ryaClient = AccumuloRyaClientFactory.build(createConnectionDetails(), accumuloConn);
    final String pcjId = ryaClient.getCreatePCJ().createPCJ(getRyaInstanceName(), sparql);
    // Grant the root user the "u" authorization.
    super.getAccumuloConnector().securityOperations().changeUserAuthorizations(getUsername(), new Authorizations("u"));
    // Setup a connection to the Rya instance that uses the "u" authorizations. This ensures
    // any statements that are inserted will have the "u" authorization on them and that the
    // PCJ updating application will have to maintain visibilities.
    final AccumuloRdfConfiguration ryaConf = new AccumuloRdfConfiguration();
    ryaConf.setTablePrefix(getRyaInstanceName());
    // Accumulo connection information.
    ryaConf.setAccumuloUser(getUsername());
    ryaConf.setAccumuloPassword(getPassword());
    ryaConf.setAccumuloInstance(super.getAccumuloConnector().getInstance().getInstanceName());
    ryaConf.setAccumuloZookeepers(super.getAccumuloConnector().getInstance().getZooKeepers());
    ryaConf.set(ConfigUtils.CLOUDBASE_AUTHS, "u");
    ryaConf.set(RdfCloudTripleStoreConfiguration.CONF_CV, "u");
    // PCJ configuration information.
    ryaConf.set(ConfigUtils.USE_PCJ, "true");
    ryaConf.set(ConfigUtils.USE_PCJ_UPDATER_INDEX, "true");
    ryaConf.set(ConfigUtils.FLUO_APP_NAME, super.getFluoConfiguration().getApplicationName());
    ryaConf.set(ConfigUtils.PCJ_STORAGE_TYPE, PrecomputedJoinIndexerConfig.PrecomputedJoinStorageType.ACCUMULO.toString());
    ryaConf.set(ConfigUtils.PCJ_UPDATER_TYPE, PrecomputedJoinIndexerConfig.PrecomputedJoinUpdaterType.FLUO.toString());
    Sail sail = null;
    RyaSailRepository ryaRepo = null;
    RepositoryConnection ryaConn = null;
    try {
        sail = RyaSailFactory.getInstance(ryaConf);
        ryaRepo = new RyaSailRepository(sail);
        ryaConn = ryaRepo.getConnection();
        // Load a few Statements into Rya.
        ryaConn.add(VF.createStatement(ALICE, TALKS_TO, BOB));
        ryaConn.add(VF.createStatement(BOB, LIVES_IN, HAPPYVILLE));
        ryaConn.add(VF.createStatement(BOB, WORKS_AT, BURGER_JOINT));
        // Wait for Fluo to finish processing.
        super.getMiniFluo().waitForObservers();
        // Fetch the exported result and show that its column visibility has been simplified.
        final String pcjTableName = new PcjTableNameFactory().makeTableName(getRyaInstanceName(), pcjId);
        final Scanner scan = accumuloConn.createScanner(pcjTableName, new Authorizations("u"));
        scan.fetchColumnFamily(new Text("customer;worker;city"));
        final Entry<Key, Value> result = scan.iterator().next();
        final Key key = result.getKey();
        assertEquals(new Text("u"), key.getColumnVisibility());
    } finally {
        if (ryaConn != null) {
            try {
                ryaConn.close();
            } finally {
            }
        }
        if (ryaRepo != null) {
            try {
                ryaRepo.shutDown();
            } finally {
            }
        }
        if (sail != null) {
            try {
                sail.shutDown();
            } finally {
            }
        }
    }
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) Connector(org.apache.accumulo.core.client.Connector) Scanner(org.apache.accumulo.core.client.Scanner) Authorizations(org.apache.accumulo.core.security.Authorizations) RyaSailRepository(org.apache.rya.rdftriplestore.RyaSailRepository) Text(org.apache.hadoop.io.Text) RyaClient(org.apache.rya.api.client.RyaClient) PcjTableNameFactory(org.apache.rya.indexing.pcj.storage.accumulo.PcjTableNameFactory) AccumuloRdfConfiguration(org.apache.rya.accumulo.AccumuloRdfConfiguration) Sail(org.openrdf.sail.Sail) Value(org.apache.accumulo.core.data.Value) Key(org.apache.accumulo.core.data.Key) Test(org.junit.Test)

Example 24 with RepositoryConnection

use of org.openrdf.repository.RepositoryConnection in project incubator-rya by apache.

the class ITBase method shutdownMiniResources.

@AfterClass
public static void shutdownMiniResources() throws RepositoryException {
    for (final RyaSailRepository repo : ryaRepos) {
        repo.shutDown();
    }
    for (final RepositoryConnection conn : ryaConns) {
        conn.close();
    }
    for (final MongoClient client : clients) {
        client.close();
    }
    ryaRepos.clear();
    ryaConns.clear();
    clients.clear();
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) MongoClient(com.mongodb.MongoClient) RyaSailRepository(org.apache.rya.rdftriplestore.RyaSailRepository) AfterClass(org.junit.AfterClass)

Example 25 with RepositoryConnection

use of org.openrdf.repository.RepositoryConnection in project stanbol by apache.

the class SesameYard method findReferences.

@Override
public QueryResultList<String> findReferences(FieldQuery parsedQuery) throws YardException, IllegalArgumentException {
    if (parsedQuery == null) {
        throw new IllegalArgumentException("The parsed query MUST NOT be NULL!");
    }
    final SparqlFieldQuery query = SparqlFieldQueryFactory.getSparqlFieldQuery(parsedQuery);
    RepositoryConnection con = null;
    TupleQueryResult results = null;
    try {
        con = repository.getConnection();
        con.begin();
        // execute the query
        int limit = QueryUtils.getLimit(query, getConfig().getDefaultQueryResultNumber(), getConfig().getMaxQueryResultNumber());
        results = executeSparqlFieldQuery(con, query, limit, false);
        // parse the results
        List<String> ids = limit > 0 ? new ArrayList<String>(limit) : new ArrayList<String>();
        while (results.hasNext()) {
            BindingSet result = results.next();
            Value value = result.getValue(query.getRootVariableName());
            if (value instanceof Resource) {
                ids.add(value.stringValue());
            }
        }
        con.commit();
        return new QueryResultListImpl<String>(query, ids, String.class);
    } catch (RepositoryException e) {
        throw new YardException("Unable to execute findReferences query", e);
    } catch (QueryEvaluationException e) {
        throw new YardException("Unable to execute findReferences query", e);
    } finally {
        if (results != null) {
            // close the result if present
            try {
                results.close();
            } catch (QueryEvaluationException ignore) {
            /* ignore */
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (RepositoryException ignore) {
            /* ignore */
            }
        }
    }
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) BindingSet(org.openrdf.query.BindingSet) Resource(org.openrdf.model.Resource) RepositoryException(org.openrdf.repository.RepositoryException) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) SparqlFieldQuery(org.apache.stanbol.entityhub.query.sparql.SparqlFieldQuery) Value(org.openrdf.model.Value) QueryResultListImpl(org.apache.stanbol.entityhub.core.query.QueryResultListImpl) TupleQueryResult(org.openrdf.query.TupleQueryResult)

Aggregations

RepositoryConnection (org.openrdf.repository.RepositoryConnection)71 TupleQuery (org.openrdf.query.TupleQuery)27 URI (org.openrdf.model.URI)24 RepositoryException (org.openrdf.repository.RepositoryException)20 Test (org.junit.Test)15 Statement (org.openrdf.model.Statement)11 YardException (org.apache.stanbol.entityhub.servicesapi.yard.YardException)10 Literal (org.openrdf.model.Literal)9 RdfRepresentation (org.apache.stanbol.entityhub.model.sesame.RdfRepresentation)6 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)6 StatementImpl (org.openrdf.model.impl.StatementImpl)6 Update (org.openrdf.query.Update)6 InputStream (java.io.InputStream)5 Value (org.openrdf.model.Value)5 Repository (org.openrdf.repository.Repository)5 SailRepository (org.openrdf.repository.sail.SailRepository)5 RyaSailRepository (org.apache.rya.rdftriplestore.RyaSailRepository)4 Resource (org.openrdf.model.Resource)4 InputStreamReader (java.io.InputStreamReader)3 SparqlFieldQuery (org.apache.stanbol.entityhub.query.sparql.SparqlFieldQuery)3