0

Docs for using the Java API for Amazon Neptune SPARQL SELECT queries only show how to iterate through the results of a TupleQuery. amazon docs

Is there a way to get the raw JSON response, so it's compatible with code that used to use the REST interface calls?

John Conde
  • 217,595
  • 99
  • 455
  • 496
Paul Cuddihy
  • 477
  • 6
  • 12
  • 2
    Not clear what you are asking. To get a raw JSON response, one can use [the REST API](https://docs.aws.amazon.com/neptune/latest/userguide/access-graph-sparql-http-rest.html) a.k.a. SPARQL Protocol. What exactly is the question? – cygri May 16 '19 at 21:13
  • 1
    The Java code is just using that REST API and providing a Java interface over the results. – cygri May 16 '19 at 21:14
  • Just to clarify, I'm happy using REST but the authentication protocols are non-trivial (Retrieving the credentials in a standard way and building the signed key). I love that the API handles this for me, but raw JSON works better for my app. The ideal solution would actually be figuring out the credentials manager and signers for apache http calls then doing REST. I couldn't find examples so figured the API is more standard & maintainable. – Paul Cuddihy May 17 '19 at 12:24
  • The Java documentation you linked to doesn’t handle authentication AFAICT. It just uses RDF4J’s standard SPARQL client. I think the docs assume that your Java code is running on an EC2 instance in the same virtual private cloud as Neptune, so no authentication necessary? – cygri May 17 '19 at 13:59

1 Answers1

1

Like explained in the comments, the Java API just uses the JSON REST response and presents it to you as a TupleQueryResult object.

If you wish to somehow receive raw JSON, you can just call the Neptune REST endpoint directly using any Java REST/HTTP client library.

Alternatively, can also re-convert the result you get back through the Rdf4j API to JSON (or any other serialized format), as follows:

public static void main( String[] args )
    {
        String sparqlEndpoint = "https://your-neptune-endpoint:port/sparql";
        Repository repo = new SPARQLRepository(sparqlEndpoint);
        repo.initialize();

        try (RepositoryConnection conn = repo.getConnection()) {
           String queryString = "SELECT ?s ?p ?o WHERE { ?s ?p ?o } limit 10";

           TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);

           TupleQueryResultHandler jsonWriter = new SPARQLResultsJSONWriter(System.out);
           tupleQuery.evaluate(jsonWriter);
        }
    }

Of course this is less efficient than using a REST client directly, as you're basically de-serializing and then immediately re-serializing the result, but it's a minimal code change if you're already using Rdf4j anyway.

Jeen Broekstra
  • 21,642
  • 4
  • 51
  • 73
  • This would have been the answer if I had known enough to ask the right question. I do think the REST calls will be better. I have re-framed my question to get some help on how to do those calls here: https://stackoverflow.com/questions/56224130/how-to-use-awsrequestsigningapacheinterceptor-with-aws-sdk2 – Paul Cuddihy May 20 '19 at 15:39