Skip to content

Instantly share code, notes, and snippets.

@chrpr
Created February 24, 2015 22:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrpr/23926c4650ce4363c51b to your computer and use it in GitHub Desktop.
Save chrpr/23926c4650ce4363c51b to your computer and use it in GitHub Desktop.
Example to grab predicate counts from a sparql endpoint

Since @mjg shamed me into posting a gist of this, here's a simple example of something gross.

Query dbpedia endpoint for most highly used properties, get the jq response format, grab the counts and values, format them usefully in sed, filter out the top 50 in awk, and print to the screen:

    curl -H 'Accept: application/sparql-results+json' -d 'query=SELECT+?p+(COUNT+(?p)+AS+?pCount)+WHERE+{+?s+?p+?o+}+GROUP+BY+?p+ORDER+BY+DESC(?pCount)' http://dbpedia.org/sparql? | jq '.results.bindings[] | [.p.value, "|", .pCount.value] | add' | sed 's/\"//g' | awk ' BEGIN { FS = "|" ; OFS = "|" } { if ($2 >= 50) { print $0 } } ' | less
@mjgiarlo
Copy link

👏

@kasei
Copy link

kasei commented Feb 24, 2015

This could also be done without jq or the awk filtering by requesting CSV and using a HAVING clause:

curl -H 'Accept: text/csv' -d 'query=SELECT+(CONCAT(STR(?p),"|",COUNT(?p))+AS+?v)+WHERE+{+?s+?p+?o+}+GROUP+BY+?p+HAVING+(COUNT(?p)+>+50)+ORDER+BY+DESC(COUNT(?p))' 'http://dbpedia.org/sparql?' | sed 's/\"//g'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment