使用 jena 在 java 中构建维基百科查询表单

发布于 2024-10-29 12:25:13 字数 2155 浏览 0 评论 0原文

#!/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽清梦 2024-11-05 12:25:13

这是一些示例代码。请注意,我实际上不会这样做:演示代码和后端查询处理都混合在一个类中。真正的应用程序不会这样做!该示例忠实于您的示例,只是我使用 Jena 的工具来预绑定一些查询变量,而不是使用 string-bash 查询(因此 DIR1-NAME 变为 ?dir- 1-名称)。我认为这更简洁,并且可以让您更灵活地选择从客户端传入哪些查询参数。

package example;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.*;

import com.hp.hpl.jena.query.*;
import com.hp.hpl.jena.rdf.model.*;

public class ServletExample
    extends HttpServlet
{
    /***********************************/
    /* Constants                       */
    /***********************************/

    private static final long serialVersionUID = 1L;

    public static final String SPARQL_ENDPOINT = "http://data.linkedmdb.org/sparql";

    public static final String QUERY = "PREFIX m: <http://data.linkedmdb.org/resource/movie/>\n" +
            "SELECT DISTINCT ?actorName WHERE {\n" +
            "  ?dir1     m:director_name %dir_name_1%.\n" +
            "  ?dir2     m:director_name %dir_name_2%.\n" +
            "  ?dir1film m:director ?dir1;\n" +
            "            m:actor ?actor.\n" +
            "  ?dir2film m:director ?dir2;\n" +
            "            m:actor ?actor.\n" +
            "  ?actor    m:actor_name ?actorName.\n" +
            "}\n" +
            "";

    private static final String HEADER = "<html>\n" +
            "      <head>\n" +
            "        <title>results</title>\n" +
            "          <link href=\"simple.css\" type=\"text/css\" rel=\"stylesheet\" />\n" +
            "      </head>\n" +
            "      <body>\n" +
            "";

    private static final String FOOTER = "</body></html>";

    /**
     * Respond to HTTP GET request. Will need to be mounted against some URL
     * pattern in web.xml
     */
    @Override
    protected void doGet( HttpServletRequest req, HttpServletResponse resp )
        throws ServletException, IOException
    {
        String dir1 = req.getParameter( "dir1" );
        String dir2 = req.getParameter( "dir2" );

        if (dir1 == null || dir2 == null || dir1.isEmpty() || dir2.isEmpty()) {
            noInput( resp );
        }
        else {
            runQuery( resp, dir1, dir2 );
        }
    }

    protected void noInput( HttpServletResponse resp )
        throws IOException
    {
        header( resp );
        resp.getWriter().println( "<p>Please select director names as query params <code>dir1</code> and <code>dir2</code></p>" );
        footer( resp );
    }

    protected void footer( HttpServletResponse resp ) throws IOException {
        resp.getWriter().println( FOOTER );
    }

    protected void header( HttpServletResponse resp ) throws IOException {
        resp.getWriter().println( HEADER );
    }

    protected void runQuery( HttpServletResponse resp, String dir1, String dir2 )
        throws IOException
    {
        PrintWriter out = resp.getWriter();

        // Set up the query
        String q = QUERY.replace( "%dir_name_1%", "\"" + dir1 + "\"" )
                        .replace( "%dir_name_2%", "\"" + dir2 + "\"" );

        Query query = QueryFactory.create( q ) ;
        QueryExecution qexec = QueryExecutionFactory.sparqlService( SPARQL_ENDPOINT, query );

        // perform the query
        ResultSet results = qexec.execSelect();

        // generate the output
        header( resp );
        if (!results.hasNext()) {
            out.println( "<p>No results, sorry.</p>" );
        }
        else {
            out.println( "<h1>Results</h1>" );
            while (results.hasNext()) {
                QuerySolution qs = results.next();
                String actorName = qs.getLiteral( "actorName" ).getLexicalForm();
                out.println( String.format( "<div>Actor named: %s</div>", actorName ) );
            }
        }
        footer( resp );
    }
}

据我所知,这段代码有效,但我不知道您使用的哪些查询应该返回一些结果。

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.

package example;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.*;

import com.hp.hpl.jena.query.*;
import com.hp.hpl.jena.rdf.model.*;

public class ServletExample
    extends HttpServlet
{
    /***********************************/
    /* Constants                       */
    /***********************************/

    private static final long serialVersionUID = 1L;

    public static final String SPARQL_ENDPOINT = "http://data.linkedmdb.org/sparql";

    public static final String QUERY = "PREFIX m: <http://data.linkedmdb.org/resource/movie/>\n" +
            "SELECT DISTINCT ?actorName WHERE {\n" +
            "  ?dir1     m:director_name %dir_name_1%.\n" +
            "  ?dir2     m:director_name %dir_name_2%.\n" +
            "  ?dir1film m:director ?dir1;\n" +
            "            m:actor ?actor.\n" +
            "  ?dir2film m:director ?dir2;\n" +
            "            m:actor ?actor.\n" +
            "  ?actor    m:actor_name ?actorName.\n" +
            "}\n" +
            "";

    private static final String HEADER = "<html>\n" +
            "      <head>\n" +
            "        <title>results</title>\n" +
            "          <link href=\"simple.css\" type=\"text/css\" rel=\"stylesheet\" />\n" +
            "      </head>\n" +
            "      <body>\n" +
            "";

    private static final String FOOTER = "</body></html>";

    /**
     * Respond to HTTP GET request. Will need to be mounted against some URL
     * pattern in web.xml
     */
    @Override
    protected void doGet( HttpServletRequest req, HttpServletResponse resp )
        throws ServletException, IOException
    {
        String dir1 = req.getParameter( "dir1" );
        String dir2 = req.getParameter( "dir2" );

        if (dir1 == null || dir2 == null || dir1.isEmpty() || dir2.isEmpty()) {
            noInput( resp );
        }
        else {
            runQuery( resp, dir1, dir2 );
        }
    }

    protected void noInput( HttpServletResponse resp )
        throws IOException
    {
        header( resp );
        resp.getWriter().println( "<p>Please select director names as query params <code>dir1</code> and <code>dir2</code></p>" );
        footer( resp );
    }

    protected void footer( HttpServletResponse resp ) throws IOException {
        resp.getWriter().println( FOOTER );
    }

    protected void header( HttpServletResponse resp ) throws IOException {
        resp.getWriter().println( HEADER );
    }

    protected void runQuery( HttpServletResponse resp, String dir1, String dir2 )
        throws IOException
    {
        PrintWriter out = resp.getWriter();

        // Set up the query
        String q = QUERY.replace( "%dir_name_1%", "\"" + dir1 + "\"" )
                        .replace( "%dir_name_2%", "\"" + dir2 + "\"" );

        Query query = QueryFactory.create( q ) ;
        QueryExecution qexec = QueryExecutionFactory.sparqlService( SPARQL_ENDPOINT, query );

        // perform the query
        ResultSet results = qexec.execSelect();

        // generate the output
        header( resp );
        if (!results.hasNext()) {
            out.println( "<p>No results, sorry.</p>" );
        }
        else {
            out.println( "<h1>Results</h1>" );
            while (results.hasNext()) {
                QuerySolution qs = results.next();
                String actorName = qs.getLiteral( "actorName" ).getLexicalForm();
                out.println( String.format( "<div>Actor named: %s</div>", actorName ) );
            }
        }
        footer( resp );
    }
}

This code works as far as I can tell, but I don't know which queries you're using that should return some results.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文