使用 jena 在 java 中构建维基百科查询表单
#!/usr/local/bin/python
import sys
sys.path.append('/usr/home/bobd/lib/python/') # needed for hosted version
from SPARQLWrapper import SPARQLWrapper, JSON
import string
import urllib
import cgi
def main():
form = cgi.FieldStorage()
dir1name = form.getvalue('dir1')
dir2name = form.getvalue('dir2')
sparql = SPARQLWrapper("http://data.linkedmdb.org/sparql")
queryString = """
PREFIX m: <http://data.linkedmdb.org/resource/movie/>
SELECT DISTINCT ?actorName WHERE {
?dir1 m:director_name "DIR1-NAME".
?dir2 m:director_name "DIR2-NAME".
?dir1film m:director ?dir1;
m:actor ?actor.
?dir2film m:director ?dir2;
m:actor ?actor.
?actor m:actor_name ?actorName.
}
"""
queryString = queryString.replace("DIR1-NAME",dir1name)
queryString = queryString.replace("DIR2-NAME",dir2name)
sparql.setQuery(queryString)
sparql.setReturnFormat(JSON)
try:
ret = sparql.query()
results = ret.convert()
requestGood = True
except Exception, e:
results = str(e)
requestGood = False
print """Content-type: text/html
<html>
<head>
<title>results</title>
<link href="simple.css" type="text/css" rel="stylesheet" />
</head>
<body>
"""
if requestGood == False:
print "<h1>Problem communicating with the server</h1>"
print "<p>" + results + "</p>"
elif (len(results["results"]["bindings"]) == 0):
print "<p>No results found.</p>"
else:
for result in results["results"]["bindings"]:
print "<p>" + result["actorName"]["value"] + "</p>"
print "</body></html>"
main()
我从 http://www.ibm.com/ 获取上述代码developerworks/xml/library/x-wikiquery/#download
现在,我想用 servlet 而不是 cgi 脚本在 java 中做同样的事情。
那么,我如何将查询从 servlet 传递到 http://data.linkedmdb.org/sparql 端点使用耶拿?
我应该如何获取结果并以 html 形式显示它?
请帮忙
#!/usr/local/bin/python
import sys
sys.path.append('/usr/home/bobd/lib/python/') # needed for hosted version
from SPARQLWrapper import SPARQLWrapper, JSON
import string
import urllib
import cgi
def main():
form = cgi.FieldStorage()
dir1name = form.getvalue('dir1')
dir2name = form.getvalue('dir2')
sparql = SPARQLWrapper("http://data.linkedmdb.org/sparql")
queryString = """
PREFIX m: <http://data.linkedmdb.org/resource/movie/>
SELECT DISTINCT ?actorName WHERE {
?dir1 m:director_name "DIR1-NAME".
?dir2 m:director_name "DIR2-NAME".
?dir1film m:director ?dir1;
m:actor ?actor.
?dir2film m:director ?dir2;
m:actor ?actor.
?actor m:actor_name ?actorName.
}
"""
queryString = queryString.replace("DIR1-NAME",dir1name)
queryString = queryString.replace("DIR2-NAME",dir2name)
sparql.setQuery(queryString)
sparql.setReturnFormat(JSON)
try:
ret = sparql.query()
results = ret.convert()
requestGood = True
except Exception, e:
results = str(e)
requestGood = False
print """Content-type: text/html
<html>
<head>
<title>results</title>
<link href="simple.css" type="text/css" rel="stylesheet" />
</head>
<body>
"""
if requestGood == False:
print "<h1>Problem communicating with the server</h1>"
print "<p>" + results + "</p>"
elif (len(results["results"]["bindings"]) == 0):
print "<p>No results found.</p>"
else:
for result in results["results"]["bindings"]:
print "<p>" + result["actorName"]["value"] + "</p>"
print "</body></html>"
main()
i got the above code from http://www.ibm.com/developerworks/xml/library/x-wikiquery/#download
Now,i want to do the same thing in java with servlets instead of cgi script.
so,how do i pass the query from servlet to http://data.linkedmdb.org/sparql endpoint using jena ?
and how should i get the result back and display it in a html form ?
PLease,HELP
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一些示例代码。请注意,我实际上不会这样做:演示代码和后端查询处理都混合在一个类中。真正的应用程序不会这样做!该示例忠实于您的示例,只是我使用 Jena 的工具来预绑定一些查询变量,而不是使用 string-bash 查询(因此
DIR1-NAME
变为?dir- 1-名称
)。我认为这更简洁,并且可以让您更灵活地选择从客户端传入哪些查询参数。据我所知,这段代码有效,但我不知道您使用的哪些查询应该返回一些结果。
Here's some example code. Note that I wouldn't actually do it this way for real: the presentation code and the back-end query handling are all mixed up in one class. Real apps don't do this! The example is faithful to your sample, except that rather than string-bash the query, I use Jena's facility to pre-bind some of the query variables (so
DIR1-NAME
becomes?dir-1-name
). I think this is much cleaner, and gives you more flexibility as to which of the query parameters are passed in from the client.This code works as far as I can tell, but I don't know which queries you're using that should return some results.