Giter Club home page Giter Club logo

sparql-java's Introduction

Gradle Package

SPARQL-JAVA

Client SPARQL 1.1 (very simple) for Java

Usage with Gradle :

apply plugin: 'java'
apply plugin: 'application'

mainClassName = 'Main'

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}
dependencies {
    compile "com.bordercloud:SPARQL-JAVA:[1.0,)"
}

Example 1 with Wikidata :

import com.bordercloud.sparql.*;

import java.net.URI;
import java.net.URISyntaxException;

public class Main {
    public static void main(String[] args) {
        try {
            URI endpoint = new URI("https://query.wikidata.org/sparql");
            String querySelect  =
                    "PREFIX wd: <http://www.wikidata.org/entity/> \n"
                            + "PREFIX wdt: <http://www.wikidata.org/prop/direct/> \n"
                            + "select  ?population \n"
                            + "where { \n"
                            // wd:Q142{France} wdt:P1082{population} ?population .
                            + "        wd:Q142 wdt:P1082 ?population . \n"
                            + "} ";
            SparqlClient sc = new SparqlClient(false);
            sc.setEndpointRead(endpoint);
            SparqlResult sr = sc.query(querySelect);
            //sc.printLastQueryAndResult();

            SparqlResultModel rows_queryPopulationInFrance = sr.getModel();
            if (rows_queryPopulationInFrance.getRowCount() > 0) {
                System.out.print("Result population in France: " + rows_queryPopulationInFrance.getRows().get(0).get("population"));
            }
        } catch (URISyntaxException | SparqlClientException e) {
            System.out.println(e);
            e.printStackTrace();
        }
    }
}

Example 2 with Wikidata :

import com.bordercloud.sparql.*;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;

public class Main {

    public static void main(String[] args) {
        try {
            URI endpoint = new URI("https://query.wikidata.org/sparql");
            String querySelect  = "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>  \n"
                    + "PREFIX psv: <http://www.wikidata.org/prop/statement/value/>  \n"
                    + "PREFIX p: <http://www.wikidata.org/prop/>  \n"
                    + "PREFIX bd: <http://www.bigdata.com/rdf#>  \n"
                    + "PREFIX wikibase: <http://wikiba.se/ontology#>  \n"
                    + "PREFIX wd: <http://www.wikidata.org/entity/>   \n"
                    + "PREFIX wdt: <http://www.wikidata.org/prop/direct/>  \n"
                    + "\n"
                    + "select distinct  \n"
                    + "?lat ?long ?siteLabel ?siteDescription  \n"
                    + "?site \n"
                    + "#(replace(xsd:string(?site),  \n"
                    + "(concat(xsd:string(?image),\'?width=200\') as ?newimage) \n"
                    + "where { \n"
                    + "        ?site wdt:P31/wdt:P279* wd:Q839954 .  \n"
                    + "        ?site wdt:P17 wd:Q142 . \n"
                    + "        ?site wdt:P18 ?image . \n"
                    + "   ?site p:P625 ?coord . \n"
                    + "\n"
                    + "          ?coord   psv:P625 ?coordValue . \n"
                    + "\n"
                    + "          ?coordValue a wikibase:GlobecoordinateValue ; \n"
                    + "                        wikibase:geoLatitude ?lat ; \n"
                    + "                        wikibase:geoLongitude ?long . \n"
                    + "\n"
                    + "        SERVICE wikibase:label { \n"
                    + "             bd:serviceParam wikibase:language \"en,fr\" . \n"
                    + "        } \n"
                    + "      } \n" +
                    "LIMIT 10";
            SparqlClient sc = new SparqlClient(false);
            sc.setEndpointRead(endpoint);
            SparqlResult sr = sc.query(querySelect);
            //sc.printLastQueryAndResult();

            printResult(sr.getModel(),30);
        } catch (URISyntaxException | SparqlClientException e) {
            System.out.println(e);
            e.printStackTrace();
        }
    }

    public static void printResult(SparqlResultModel rs , int size) {

        for (String variable : rs.getVariables()) {
            System.out.print(String.format("%-"+size+"."+size+"s", variable ) + " | ");
        }
        System.out.print("\n");
        for (HashMap<String, Object> row : rs.getRows()) {
            for (String variable : rs.getVariables()) {
                System.out.print(String.format("%-"+size+"."+size+"s", row.get(variable)) + " | ");
            }
            System.out.print("\n");
        }
    }
}

Example for writing in a Virtuoso database :

endpoint = new URI("http://172.17.0.2:8890/sparql-auth/");
client= new SparqlClient(false);
client.setLogin("dba");
client.setPassword("dba");
client.setEndpointRead(endpoint);
client.setEndpointWrite(endpoint);
//check delete
String q = 
        "PREFIX a: <http://example.com/test/a/>\n" +
        "PREFIX b: <http://example.com/test/b/>\n" +
        "DELETE DATA {\n" +
        "   GRAPH <http://truc.fr/> {\n" +
        "     a:A b:Name \"Test1\" .\n" +
        "     a:A b:Name \"Test2\" .\n" +
        "     a:A b:Name \"Test3\" .\n" +
        "}}";

SparqlResult sr1 = client.query(q);

See more examples with DBpedia and Wikidata in the tests.

Update

  • 2020-05-17 Version 1.0
  • 2019-11-23 Fix bugs and add debug tools

sparql-java's People

Contributors

bordercloud avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.