Skip to content

Instantly share code, notes, and snippets.

@the-frey
Created May 29, 2014 10:02
Show Gist options
  • Save the-frey/24ad2c0a366a27f40529 to your computer and use it in GitHub Desktop.
Save the-frey/24ad2c0a366a27f40529 to your computer and use it in GitHub Desktop.
Rake task (needs to be included in the same Rakefile as fuseki_export.rb) to upload a single specified file from a backed up data folder.
task :upload_single_file do
# database connections
fuseki = FusekiConnection.new('glasgow-development', nil, nil, 3030)
stardog = StardogConnection.new('glasgow-development')
file = File.read("#{FusekiExport.template_app_root}/data_backup/postcodes.ttl")
graph_uri_string = "http://linked.glasgow.gov.uk/graph/postcodes"
# begin transaction with basic Stardog superuser creds
begin_transaction_response = RestClient.post "http://#{stardog.username}:#{stardog.password}@localhost:#{stardog.port}/#{stardog.database_name}/transaction/begin", {:accept => :text}
begin
transaction_token = begin_transaction_response.body
puts " Sending data to Stardog..."
# submit data to Stardog endpoint
stardog_graph_post_response = RestClient.post "http://#{stardog.username}:#{stardog.password}@localhost:#{stardog.port}/#{stardog.database_name}/#{transaction_token}/add?graph-uri=#{graph_uri_string}", file, {
:content_type => 'text/turtle'
}
puts " Posting data complete: #{stardog_graph_post_response.code}, ready to commit"
# commit the transaction
RestClient.post "http://#{stardog.username}:#{stardog.password}@localhost:#{stardog.port}/#{stardog.database_name}/transaction/commit/#{transaction_token}", {:accept => :text}
puts " Transaction complete for: #{graph_uri_string}"
rescue => transaction_exception
# posting data to Stardog named graph has failed so we roll back the transaction
begin
RestClient.post "http://#{stardog.username}:#{stardog.password}@localhost:#{stardog.port}/#{stardog.database_name}/transaction/rollback/#{transaction_token}", {:accept => :text}
puts " Data could not be uploaded to Stardog. Transaction rolled back. Error: #{transaction_exception.to_s}"
rescue => rollback_exception
puts " Error: exception in transaction: #{transaction_exception}\n Error: #{rollback_exception.to_s} - rollback failed"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment