Search code examples
restsparqlgraph-databasesgraphdb

How to create an JDBC SQL view with graphdb REST API in python


I am working with GraphDB on a knowledge graph and would like to dynamically generate JDBC SQL views using the REST API :

https://graphdb.ontotext.com/documentation/10.0/devhub/rest-api/curl-commands.html?%20api#repository-management

http://graph.openbiodiv.net/webapi

It works fine for the get functions (get the SQL view configuration of the active repository, get the names of all the views in the active repository) for example:

import requests
x = requests.get('http://localhost:7200/rest/sql-views/tables')

But when I am now trying to create a new view with the following code:

(viewrqst is really just an example to make sure it works, before using my actual query)

viewrqst="SELECT ?s WHERE {?s ?p ?m1. ?m1 ?pp ?s2. #!filter}"

viewbody={
'name': 'viewpython',
"query": viewrqst,
"columns":[
{
"column_name":"s1",
"column_type":"string",
"nullable":True,
"sparql_type":"string",
"sql_type_precision":0,
"sql_type_scale":0
}
]
}

r = requests.post('http://localhost:7200/rest/sql-views/tables/',json=viewbody)
print(r.text)

I get the following error

{"message":"Syntax error in query:  (Encountered \"<EOF>\" at line 2, column 50.\r\nWas expecting one of:\r\n    \"{\" ...\r\n    \"}\" ...\r\n    \"optional\" ...\r\n    \"graph\" ...\r\n    \"minus\" ...\r\n    \"filter\" ...\r\n    \"bind\" ...\r\n    \"service\" ...\r\n    \"values\" ...\r\n    )."}

It may be because of the #!filter but without it, I get the following error

{"message":"Invalid SQL View:  (No specified !filter optimization clause)."}

I tried doing the same thing directly from GraphDB REST API page which is directly connected to my database and allows to test the methods (http://graph.openbiodiv.net/webapi) but I get similar problems.

the body of the post resquest

the response, with 400 code

Does anyone know how to write the body of the request? Or what could cause the eof error? Is the # comment actually being considered as a comment or as part of the code ?

EDIT: someone suggested the problem may come from the use of 's1' which doesn't appear anywhere else, I thought column_name could be chosen arbitrarily but here are some other examples to make sure :

request and response on graphdb api page

request and response with python

Thank you for your help


Solution

  • By using devtools on GraphDDB and looking at the network activity while creating an sql view I found out I was missing \n before and after the #!filter so at the end I'd have WHERE{ ... \n #!filter \n }

    and as it was mentioned to me by someone it is in fact important to keep the same name in column_name and in the query.