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();
}
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);
}
}
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);
}
}
}
}
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;
}
}
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 */
}
}
}
}
}
Aggregations