Skip to content
Lib Java for SPARQL 1.1
Java
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
dist
lib
nbproject
src/com/bordercloud/sparql
.classpath
.gitignore
LICENSE
README.md
build.xml

README.md

SPARQL-JAVA

Lib Java for SPARQL 1.1 (Very simple...)

Usage Read :

import com.bordercloud.sparql.Endpoint;

          try {
            String endpoint3 = "https://query.wikidata.org/sparql";
            Endpoint sp3 = new Endpoint(endpoint3, false);
            sp3.setMethodHTTPRead("GET");

            String queryPopulationInFrance
                    = "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"
                    + "} ";
            HashMap<String, HashMap> rs3_queryPopulationInFrance = sp3.query(queryPopulationInFrance);

            ArrayList<HashMap<String, Object>> rows_queryPopulationInFrance = (ArrayList) rs3_queryPopulationInFrance.get("result").get("rows");
            if (rows_queryPopulationInFrance.size() > 0) {
                System.out.print("Result population in France: " + rows_queryPopulationInFrance.get(0).get("population"));
                //printResult(rs3_queryPopulationInFrance,30);
            }

        } catch (EndpointException eex) {
            System.out.println(eex);
            eex.printStackTrace();
        }

Usage Write with Virtuoso :

import com.bordercloud.sparql.Endpoint;

          try {
                      String queryInsert = "INSERT DATA "+
              " {GRAPH <http://example.com/MyGraph>  "+
              "   { "+
              "       <http://example.com/test> <http://example.com/prop> \"text...\""+
              "    }   "+
              "}";
            // with Virtuoso
            String endpoint = "http://example.com/sparql-auth";
            Endpoint sp1 = new Endpoint( endpoint, false);

            sp1.setLogin("login");
            sp1.setPassword("password");

            HashMap<String, HashMap> rs1 =sp1.query(queryInsert);
            printResult(rs1,150);

        } catch (EndpointException eex) {
            System.out.println(eex);
            eex.printStackTrace();
        }

Test with DBpedia and Wikidata :

cd dist/
java -classpath  lib/*:SPARQL-JAVA.jar com.bordercloud.sparql.Main

Source of Main Class

package com.bordercloud.sparql;

import java.util.ArrayList;
import java.util.HashMap;

public class Main {

    public static void main(String[] args) {
        try {
/*
            String queryInsert = "INSERT DATA "+
              " {GRAPH <http://example.com/MyGraph>  "+
              "   {             "+
              "       <http://example.com/test> <http://example.com/prop> \"ontology_bis\""+
              "    }   "+
              "}";
            // with Virtuoso
            String endpoint = "http://example.com/sparql-auth";
            Endpoint sp1 = new Endpoint( endpoint, false);

            sp1.setLogin("login");
            sp1.setPassword("password");
            HashMap<String, HashMap> rs1 =sp1.query(queryInsert);
            printResult(rs1,150);
*/
            String querySelect = "SELECT * "+
              //"FROM <http://linkedgadget.com/wiki/Data:Jarvis_2016#personal>"+
              "where {"+
              "  ?x ?y ?z . "+
              "} LIMIT 5";
                                    
            System.out.println("Query : ");
            System.out.println(querySelect); 
            
            String endpoint2 = "http://dbpedia.org/sparql";
            System.out.println("");            
            System.out.println("Endpoint : "+endpoint2);       
            System.out.println("");
            System.out.println("Result : ");
            Endpoint sp2 = new Endpoint(endpoint2, true);
            //sp2.setUserAgentRequestHeader("My client SPARQL 1.0");
            HashMap<String, HashMap> rs2 = sp2.query(querySelect);
            printResult(rs2,30);          
            
            String endpoint3 = "https://query.wikidata.org/sparql";
            System.out.println("");            
            System.out.println("Endpoint : "+endpoint3);       
            System.out.println(""); 
            System.out.println("Result : ");
            Endpoint sp3 = new Endpoint( endpoint3, true);
            sp3.setMethodHTTPRead("GET");
            HashMap<String, HashMap> rs3 =  sp3.query(querySelect);

            printResult(rs3,30);
            
        }catch(EndpointException eex) {
            System.out.println(eex);
            eex.printStackTrace();
        }
    }

    public static void printResult(HashMap<String, HashMap> rs , int size) {

      for (String variable : (ArrayList<String>) rs.get("result").get("variables")) {
        System.out.print(String.format("%-"+size+"."+size+"s", variable ) + " | ");
      }    
      System.out.print("\n");
      for (HashMap<String, Object> value : (ArrayList<HashMap<String, Object>>) rs.get("result").get("rows")) {
        //System.out.print(value);
        /* for (String key : value.keySet()) {
         System.out.println(value.get(key));            
         }*/
        for (String variable : (ArrayList<String>) rs.get("result").get("variables")) {            
          //System.out.println(value.get(variable));
          System.out.print(String.format("%-"+size+"."+size+"s", value.get(variable)) + " | ");
        }
        System.out.print("\n");
      }
    }
}

Update

  • 2019-11-23 Fix bugs and add debug tools
You can’t perform that action at this time.