{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n", "SPDX-License-Identifier: Apache-2.0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Using RDF and SPARQL to Access the Graph\n", "\n", "SPARQL is a query language for the Resource Description Framework (RDF), which is a graph data format designed for the web. Amazon Neptune is compatible with SPARQL 1.1. This means that you can connect to a Neptune DB instance and query the graph using the query language described in the [SPARQL 1.1 Query Language](https://www.w3.org/TR/sparql11-query/) specification.\n", "\n", "A query in SPARQL consists of a SELECT clause to specify the variables to return and a WHERE clause to specify which data to match in the graph. If you are unfamiliar with SPARQL queries, see [Writing Simple Queries](https://www.w3.org/TR/sparql11-query/#WritingSimpleQueries) in the [SPARQL 1.1 Query Language](https://www.w3.org/TR/sparql11-query/)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The HTTP endpoint for SPARQL queries to a Neptune DB instance is https://your-neptune-endpoint:port/sparql.\n", "\n", "Issue the below SPARQL UPDATE using the `%%sparql` magic " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%sparql\n", "\n", "INSERT DATA { . }" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Enter the following to submit a SPARQL QUERY using the `%%sparql` magic:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%sparql\n", "SELECT ?s ?p ?o WHERE {?s ?p ?o} LIMIT 10" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The preceding example returns up to 10 of the triples (subject-predicate-object) in the graph by using the ?s ?p ?o query with a limit of 10. To query for something else, replace it with another SPARQL query.\n", "\n", "**Note**\n", "\n", "The default MIME type of a response is `application/sparql-results+json` for any query to Neptune.\n", "\n", "If you wish to use a different MIME type, this can be specified via the `-m`/`--media-type` parameter. For example, to retrieve your results in SPARQL XML format, run:" ] }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [ "%%sparql -m application/sparql-results+xml\n", "\n", "SELECT ?s ?p ?o WHERE {?s ?p ?o} LIMIT 10" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "markdown", "source": [ "For a list of all available MIME types, see [SPARQL HTTP API](https://docs.aws.amazon.com/neptune/latest/userguide/sparql-api-reference.html)." ], "metadata": { "collapsed": false } }, { "cell_type": "markdown", "metadata": {}, "source": [ "## What's next?\n", "\n", "Now that you've tried your hand at SPARQL queries, take your learning to the next level with these datasets:\n", "\n", "[Explore English Premier League using SPARQL](../02-Visualization/EPL-SPARQL.ipynb)\n", "\n", "[Explore global air route data using SPARQL](../02-Visualization/Air-Routes-SPARQL.ipynb)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }