relpipe-data/examples/relpipe-in-sparql.sh
author František Kučera <franta-hg@frantovo.cz>
Fri, 26 Nov 2021 22:14:18 +0100
branchv_0
changeset 328 cc60c8dd7924
parent 310 aeda3cb4528d
permissions -rwxr-xr-x
relpipe-in-sparql: update to new relpipe-in-csv CLI API
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
310
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
#!/bin/bash
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
# Relational pipes
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
# Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
# 
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
# This program is free software: you can redistribute it and/or modify
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
# it under the terms of the GNU General Public License as published by
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
# the Free Software Foundation, version 3 of the License.
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
# 
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
# This program is distributed in the hope that it will be useful,
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
# GNU General Public License for more details.
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
# 
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
# You should have received a copy of the GNU General Public License
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
# along with this program. If not, see <http://www.gnu.org/licenses/>.
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
relpipe_in_sparql_help() {
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
	cat <<-EOF
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
		SPARQL query is expected on the STDIN. Exapmple:
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
		  echo 'SELECT * WHERE { ?subject ?predicate ?object . } LIMIT 3' \
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
| relpipe-in-sparql | relpipe-out-tabular
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
		
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
		Namespace prefixes are part of the query.
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
		But because they are usually constant, they can be set as the 
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
		RELPIPE_IN_SPARQL_PREFIXES environmental variable.
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
		Then they are prepended to the query.
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
		
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
		SPARQL endpoint can be set ad-hoc by the --endpoint option.
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
		
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
		To configure default SPARQL endpoint, add something like this to your environment:
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
		  export RELPIPE_IN_SPARQL_ENDPOINT="https://query.wikidata.org/sparql"
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
		  export RELPIPE_IN_SPARQL_ENDPOINT="https://dbpedia.org/sparql"
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
		  export RELPIPE_IN_SPARQL_ENDPOINT="https://data.gov.cz/sparql"
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
		  export RELPIPE_IN_SPARQL_ENDPOINT="https://data.cssz.cz/sparql"
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
		The relation name defaults to "rdf". Custom name can be set using the --relation option.
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
EOF
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
}
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
relation="rdf";
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
endpoint="${RELPIPE_IN_SPARQL_ENDPOINT:-https://dbpedia.org/sparql}";
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
while [[ $# -gt 0 ]]; do
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
	argument="$1";
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	case "$argument" in
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
		"--relation") relation="$2"; shift; shift; ;;
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
		"--endpoint") endpoint="$2"; shift; shift; ;;
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
		"--help") relpipe_in_sparql_help; exit; ;;
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
	esac
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
done
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
[[ -n "$RELPIPE_IN_SPARQL_PREFIXES" ]] && query="$RELPIPE_IN_SPARQL_PREFIXES"; query+=$'\n\n';
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
query+="$(</dev/stdin)";
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
# Simple implementation that utilizes the CSV output of the SPARQL endpoint.
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
relpipe_in_sparql_implementation_csv() {
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
	curl \
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
		--header "Accept: text/csv" \
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
		--data-urlencode query="$query" \
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
		--fail \
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
		--silent \
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
		--show-error \
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
		"$endpoint" \
328
cc60c8dd7924 relpipe-in-sparql: update to new relpipe-in-csv CLI API
František Kučera <franta-hg@frantovo.cz>
parents: 310
diff changeset
    67
		| relpipe-in-csv --relation "$relation"
310
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
}
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
# More powerful implementation based on XML.
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
# Can be customized through XSLT.
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
# But: has more dependencies and avoids streaming.
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
relpipe_in_sparql_implementation_xml() {
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
	DIR="$(dirname $(realpath "$0"))";
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
	curl \
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
		--header "Accept: application/sparql-results+xml" \
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
		--data-urlencode query="$query" \
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
		--fail \
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
		--silent \
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
		--show-error \
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
		"$endpoint" \
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
		| xsltproc --stringparam "relation" "$relation" "$DIR/relpipe-in-sparql.xsl" - \
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
		| relpipe-in-xml
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
}
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
aeda3cb4528d examples: Querying an RDF triplestore using SPARQL
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
relpipe_in_sparql_implementation_${RELPIPE_IN_SPARQL_IMPLEMENTATION:-csv}