Search in sources :

Example 1 with RDFParseException

use of org.openrdf.rio.RDFParseException in project wikidata-query-rdf by wikimedia.

the class WikibaseRepository method fetchRdfForEntity.

/**
 * Fetch the RDF for some entity.
 *
 * @throws RetryableException thrown if there is an error communicating with
 *             wikibase
 */
public Collection<Statement> fetchRdfForEntity(String entityId) throws RetryableException {
    // TODO handle ?flavor=dump or whatever parameters we need
    URI uri = uris.rdf(entityId);
    long start = System.currentTimeMillis();
    log.debug("Fetching rdf from {}", uri);
    RDFParser parser = Rio.createParser(RDFFormat.TURTLE);
    StatementCollector collector = new StatementCollector();
    parser.setRDFHandler(new NormalizingRdfHandler(collector));
    HttpGet request = new HttpGet(uri);
    request.setConfig(configWithTimeout);
    try {
        try (CloseableHttpResponse response = client.execute(request)) {
            if (response.getStatusLine().getStatusCode() == 404) {
                // A delete/nonexistent page
                return Collections.emptyList();
            }
            if (response.getStatusLine().getStatusCode() >= 300) {
                throw new ContainedException("Unexpected status code fetching RDF for " + uri + ":  " + response.getStatusLine().getStatusCode());
            }
            parser.parse(new InputStreamReader(response.getEntity().getContent(), Charsets.UTF_8), uri.toString());
        }
    } catch (UnknownHostException | SocketException | SSLHandshakeException e) {
        // We want to bail on this, since it happens to be sticky for some reason
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RetryableException("Error fetching RDF for " + uri, e);
    } catch (RDFParseException | RDFHandlerException e) {
        throw new ContainedException("RDF parsing error for " + uri, e);
    }
    log.debug("Done in {} ms", System.currentTimeMillis() - start);
    return collector.getStatements();
}
Also used : SocketException(java.net.SocketException) InputStreamReader(java.io.InputStreamReader) UnknownHostException(java.net.UnknownHostException) StatementCollector(org.openrdf.rio.helpers.StatementCollector) HttpGet(org.apache.http.client.methods.HttpGet) ContainedException(org.wikidata.query.rdf.tool.exception.ContainedException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) RDFParser(org.openrdf.rio.RDFParser) URI(java.net.URI) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) NormalizingRdfHandler(org.wikidata.query.rdf.tool.rdf.NormalizingRdfHandler) RetryableException(org.wikidata.query.rdf.tool.exception.RetryableException) RDFHandlerException(org.openrdf.rio.RDFHandlerException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) RDFParseException(org.openrdf.rio.RDFParseException)

Example 2 with RDFParseException

use of org.openrdf.rio.RDFParseException in project incubator-rya by apache.

the class KafkaLoadStatements method fromFile.

@Override
public void fromFile(final Path statementsPath, final String visibilities) throws RyaStreamsException {
    requireNonNull(statementsPath);
    requireNonNull(visibilities);
    if (!statementsPath.toFile().exists()) {
        throw new RyaStreamsException("Could not load statements at path '" + statementsPath + "' because that " + "does not exist. Make sure you've entered the correct path.");
    }
    // Create an RDF Parser whose format is derived from the statementPath's file extension.
    final RDFFormat format = RDFFormat.forFileName(statementsPath.getFileName().toString());
    final RDFParser parser = Rio.createParser(format);
    // Set a handler that writes the statements to the specified kafka topic.
    parser.setRDFHandler(new RDFHandlerBase() {

        @Override
        public void startRDF() throws RDFHandlerException {
            log.trace("Starting loading statements.");
        }

        @Override
        public void handleStatement(final Statement stmnt) throws RDFHandlerException {
            final VisibilityStatement visiStatement = new VisibilityStatement(stmnt, visibilities);
            producer.send(new ProducerRecord<>(topic, visiStatement));
        }

        @Override
        public void endRDF() throws RDFHandlerException {
            producer.flush();
            log.trace("Done.");
        }
    });
    // Do the parse and load.
    try {
        parser.parse(Files.newInputStream(statementsPath), "");
    } catch (RDFParseException | RDFHandlerException | IOException e) {
        throw new RyaStreamsException("Could not load the RDF file's Statements into Rya Streams.", e);
    }
}
Also used : RDFHandlerException(org.openrdf.rio.RDFHandlerException) RyaStreamsException(org.apache.rya.streams.api.exception.RyaStreamsException) Statement(org.openrdf.model.Statement) VisibilityStatement(org.apache.rya.api.model.VisibilityStatement) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) RDFHandlerBase(org.openrdf.rio.helpers.RDFHandlerBase) IOException(java.io.IOException) RDFParser(org.openrdf.rio.RDFParser) VisibilityStatement(org.apache.rya.api.model.VisibilityStatement) RDFFormat(org.openrdf.rio.RDFFormat) RDFParseException(org.openrdf.rio.RDFParseException)

Example 3 with RDFParseException

use of org.openrdf.rio.RDFParseException in project incubator-rya by apache.

the class MongoLoadStatementsFile method loadStatements.

@Override
public void loadStatements(final String ryaInstanceName, final Path statementsFile, final RDFFormat format) throws InstanceDoesNotExistException, RyaClientException {
    requireNonNull(ryaInstanceName);
    requireNonNull(statementsFile);
    requireNonNull(format);
    // Ensure the Rya Instance exists.
    if (!instanceExists.exists(ryaInstanceName)) {
        throw new InstanceDoesNotExistException(String.format("There is no Rya instance named '%s'.", ryaInstanceName));
    }
    Sail sail = null;
    SailRepositoryConnection sailRepoConn = null;
    try {
        // Get a Sail object that is connected to the Rya instance.
        final MongoDBRdfConfiguration ryaConf = connectionDetails.build(ryaInstanceName);
        sail = RyaSailFactory.getInstance(ryaConf);
        final SailRepository sailRepo = new SailRepository(sail);
        sailRepoConn = sailRepo.getConnection();
        // Load the file.
        sailRepoConn.add(statementsFile.toFile(), null, format);
    } catch (SailException | RyaDAOException | InferenceEngineException | AccumuloException | AccumuloSecurityException e) {
        throw new RyaClientException("Could not load statements into Rya because of a problem while creating the Sail object.", e);
    } catch (RDFParseException | RepositoryException | IOException e) {
        throw new RyaClientException("Could not load the statements into Rya.", e);
    } finally {
        // Close the resources that were opened.
        if (sailRepoConn != null) {
            try {
                sailRepoConn.close();
            } catch (final RepositoryException e) {
                log.error("Couldn't close the SailRepositoryConnection object.", e);
            }
        }
        if (sail != null) {
            try {
                sail.shutDown();
            } catch (final SailException e) {
                log.error("Couldn't close the Sail object.", e);
            }
        }
    }
}
Also used : AccumuloException(org.apache.accumulo.core.client.AccumuloException) RyaClientException(org.apache.rya.api.client.RyaClientException) SailRepository(org.openrdf.repository.sail.SailRepository) InferenceEngineException(org.apache.rya.rdftriplestore.inference.InferenceEngineException) RepositoryException(org.openrdf.repository.RepositoryException) InstanceDoesNotExistException(org.apache.rya.api.client.InstanceDoesNotExistException) SailException(org.openrdf.sail.SailException) IOException(java.io.IOException) SailRepositoryConnection(org.openrdf.repository.sail.SailRepositoryConnection) Sail(org.openrdf.sail.Sail) RyaDAOException(org.apache.rya.api.persist.RyaDAOException) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException) MongoDBRdfConfiguration(org.apache.rya.mongodb.MongoDBRdfConfiguration) RDFParseException(org.openrdf.rio.RDFParseException)

Example 4 with RDFParseException

use of org.openrdf.rio.RDFParseException in project incubator-rya by apache.

the class RyaSailRepositoryConnection method add.

@Override
public void add(InputStream in, String baseURI, RDFFormat dataFormat, Resource... contexts) throws IOException, RDFParseException, RepositoryException {
    OpenRDFUtil.verifyContextNotNull(contexts);
    CombineContextsRdfInserter rdfInserter = new CombineContextsRdfInserter(this);
    rdfInserter.enforceContext(contexts);
    boolean localTransaction = startLocalTransaction();
    try {
        RDFLoader loader = new RDFLoader(getParserConfig(), getValueFactory());
        loader.load(in, baseURI, dataFormat, rdfInserter);
        conditionalCommit(localTransaction);
    } catch (RDFHandlerException e) {
        conditionalRollback(localTransaction);
        throw ((RepositoryException) e.getCause());
    } catch (RDFParseException e) {
        conditionalRollback(localTransaction);
        throw e;
    } catch (IOException e) {
        conditionalRollback(localTransaction);
        throw e;
    } catch (RuntimeException e) {
        conditionalRollback(localTransaction);
        throw e;
    }
}
Also used : RDFHandlerException(org.openrdf.rio.RDFHandlerException) CombineContextsRdfInserter(org.apache.rya.rdftriplestore.utils.CombineContextsRdfInserter) RepositoryException(org.openrdf.repository.RepositoryException) IOException(java.io.IOException) RDFLoader(org.openrdf.repository.util.RDFLoader) RDFParseException(org.openrdf.rio.RDFParseException)

Example 5 with RDFParseException

use of org.openrdf.rio.RDFParseException in project stanbol by apache.

the class RdfResourceImporter method importResource.

@Override
public ResourceState importResource(InputStream is, String resourceName) throws IOException {
    log.info("> importing {}:", resourceName);
    RDFFormat rdfFormat = Rio.getParserFormatForFileName(resourceName);
    if (rdfFormat == null) {
        log.info("  ... unable to detect RDF format for {}", resourceName);
        log.info("  ... resource '{}' will not be imported", resourceName);
        return ResourceState.IGNORED;
    } else {
        RepositoryConnection con = null;
        try {
            con = repository.getConnection();
            con.begin();
            con.add(new InputStreamReader(is, UTF8), baseUri, rdfFormat, contexts);
            con.commit();
            return ResourceState.LOADED;
        } catch (RDFParseException e) {
            log.error("  ... unable to parser RDF file " + resourceName + " (format: " + rdfFormat + ")", e);
            return ResourceState.ERROR;
        } catch (RepositoryException e) {
            throw new IllegalArgumentException("Repository Exception while " + resourceName + "!", e);
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (RepositoryException e1) {
                /* ignore */
                }
            }
        }
    }
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) InputStreamReader(java.io.InputStreamReader) RepositoryException(org.openrdf.repository.RepositoryException) RDFFormat(org.openrdf.rio.RDFFormat) RDFParseException(org.openrdf.rio.RDFParseException)

Aggregations

RDFParseException (org.openrdf.rio.RDFParseException)14 IOException (java.io.IOException)13 RDFHandlerException (org.openrdf.rio.RDFHandlerException)10 RepositoryException (org.openrdf.repository.RepositoryException)6 RDFParser (org.openrdf.rio.RDFParser)5 InputStreamReader (java.io.InputStreamReader)4 RDFFormat (org.openrdf.rio.RDFFormat)4 Statement (org.openrdf.model.Statement)3 SailRepository (org.openrdf.repository.sail.SailRepository)3 SocketException (java.net.SocketException)2 UnknownHostException (java.net.UnknownHostException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)2 AccumuloException (org.apache.accumulo.core.client.AccumuloException)2 AccumuloSecurityException (org.apache.accumulo.core.client.AccumuloSecurityException)2 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)2 HttpGet (org.apache.http.client.methods.HttpGet)2 InstanceDoesNotExistException (org.apache.rya.api.client.InstanceDoesNotExistException)2 RyaClientException (org.apache.rya.api.client.RyaClientException)2