/** * Runs a SPARQL UPDATE query as specified. * * @param sparqlString Query string. * @throws RepositoryException * @throws MalformedQueryException * @throws QueryEvaluationException * @throws UpdateExecutionException */ public void plainSparqlUpdate(String sparqlString) throws RepositoryException, MalformedQueryException, QueryEvaluationException, UpdateExecutionException { RepositoryConnection conn = repo.getConnection(); Update query = conn.prepareUpdate(QueryLanguage.SPARQL, sparqlString); query.execute(); conn.close(); }
private void copyCatalogAttributes( Value graph, Value catalogUri, Value rawGraph, RepositoryConnection connection) throws RepositoryException, MalformedQueryException, UpdateExecutionException { String query = "insert into ?graph { ?catalogUri ?p ?t. }\n" + " where { graph ?graph {\n" + " ?rawCatalogUri ?p ?t." + "}}"; Update u = connection.prepareUpdate(QueryLanguage.SPARQL, query); u.setBinding("catalogUri", catalogUri); u.setBinding("graph", graph); u.setBinding("rawCatalogUri", rawGraph); u.setBinding("p", LODMSPredicates.DCT_PUBLISHER); u.execute(); u.setBinding("p", LODMSPredicates.DCT_DESCRIPTION); u.execute(); u.setBinding("p", LODMSPredicates.DCT_TITLE); u.execute(); }
@Override public void execute() throws LpException { // Create remote repository. final VirtuosoRepository virtuosoRepository = new VirtuosoRepository( configuration.getVirtuosoUrl(), configuration.getUsername(), configuration.getPassword()); try { virtuosoRepository.initialize(); } catch (RepositoryException ex) { throw exceptionFactory.failure("Can't connect to Virtuoso repository.", ex); } cleanUp.addAction( () -> { try { virtuosoRepository.shutDown(); } catch (RepositoryException ex) { LOG.warn("Can't close repository.", ex); } }); // Delete data if set. if (configuration.isClearDestinationGraph()) { RepositoryConnection repositoryConnection = virtuosoRepository.getConnection(); try { final Update update = repositoryConnection.prepareUpdate( QueryLanguage.SPARQL, getClearQuery(configuration.getTargetGraph())); update.execute(); } finally { try { repositoryConnection.close(); } catch (RepositoryException ex) { LOG.warn("Can't close connection.", ex); } } } final Connection connectionForInit = getSqlConnection(); // Insert data to load table. try (PreparedStatement statementLdDir = connectionForInit.prepareStatement(SQL_LD_DIR)) { statementLdDir.setString(1, configuration.getLoadDirectoryPath()); statementLdDir.setString(2, configuration.getLoadFileName()); statementLdDir.setString(3, configuration.getTargetGraph()); // Execute. final ResultSet resultSetLdDir = statementLdDir.executeQuery(); resultSetLdDir.close(); } catch (SQLException ex) { throw exceptionFactory.failure("Can't execute ld_dir query.", ex); } // Check number of files to load. final int filesToLoad; try (PreparedStatement statementStatusCountProcessing = connectionForInit.prepareStatement(SQL_QUERY_WAITING)) { statementStatusCountProcessing.setString(1, configuration.getLoadDirectoryPath() + "%"); try (ResultSet resultSetProcessing = statementStatusCountProcessing.executeQuery()) { resultSetProcessing.next(); filesToLoad = resultSetProcessing.getInt(1); } } catch (SQLException ex) { throw exceptionFactory.failure("Can't query load_list table.", ex); } finally { // Here we can close the connection. try { connectionForInit.close(); } catch (SQLException ex) { LOG.warn("Can't close SQL connection.", ex); } } LOG.info("Load list holds {} files to process", filesToLoad); if (filesToLoad == 0) { LOG.info("Nothing to do. Stopping."); return; } // Start loading, we use only a single thread call here. startLoading(); // Check for status - periodic and final. while (true) { final Connection connectionForStatusCheck = getSqlConnection(); try (PreparedStatement statement = connectionForStatusCheck.prepareStatement(SQL_QUERY_FINISHED)) { statement.setString(1, configuration.getLoadDirectoryPath() + "%"); try (ResultSet resultSetDoneLoop = statement.executeQuery()) { resultSetDoneLoop.next(); int filesLoaded = resultSetDoneLoop.getInt(1); LOG.info("Processing {}/{} files", filesLoaded, filesToLoad); if (filesLoaded == filesToLoad) { break; } } } catch (SQLException ex) { LOG.warn("Can't query status.", ex); } finally { try { connectionForStatusCheck.close(); } catch (SQLException ex) { LOG.warn("Can't close SQL connection.", ex); } } // Wait before next check. try { Thread.sleep(configuration.getStatusUpdateInterval() * 1000); } catch (InterruptedException ex) { // Do nothing here. } } if (configuration.isClearLoadList()) { final Connection connectionForDelete = getSqlConnection(); // Delete from loading table - made optional. try (PreparedStatement delete = connectionForDelete.prepareStatement(SQL_DELETE_LOAD_LIST)) { delete.setString(1, configuration.getLoadDirectoryPath() + "%"); delete.executeUpdate(); } catch (SQLException ex) { } finally { try { connectionForDelete.close(); } catch (SQLException ex) { LOG.warn("Can't close SQL connection.", ex); } } } }