简体   繁体   English

Google的Freebase API:Java中的基本MQL查询和JSON解析示例?

[英]Google’s Freebase API: Basic MQL Query and JSON Parse Example in Java?

Question

What would a basic example of querying Freebase with MQL from the Java Freebase API (google-api-services-freebase) look like and what is the recommended way for processing the resulting data? 从Java Freebase API(google-api-services-freebase)查询使用MQL的Freebase的基本示例是什么样的,以及处理结果数据的推荐方法是什么?

I'm especially wondering how to “correctly” use the com.google.api.services.freebase.Freebase class . 我特别想知道如何“正确”使用com.google.api.services.freebase.Freebase

Background – What I Have So Far 背景 - 我到目前为止

I have these dependencies in my project: 我的项目中有这些依赖项:

<dependency>
  <groupId>com.google.apis</groupId>
  <artifactId>google-api-services-freebase</artifactId>
  <version>v1-rev42-1.17.0-rc</version>
</dependency>
<dependency>
  <groupId>com.google.http-client</groupId>
  <artifactId>google-http-client-jackson2</artifactId>
  <version>1.17.0-rc</version>
</dependency>

Now with the following code I can get unparsed JSON results to stdout: 现在使用以下代码,我可以将未解析的JSON结果转换为stdout:

Freebase freebase = new Freebase(
    GoogleNetHttpTransport.newTrustedTransport(),
    JacksonFactory.getDefaultInstance(),
    null);
Mqlread mqlread = freebase.mqlread(
    "[{ \"limit\": 5, \"name\": null, \"type\": \"/medicine/disease\" }]");
mqlread.executeAndDownloadTo(System.out);

But what is the recommended way of parsing the returned JSON? 但是解析返回的JSON的推荐方法是什么? Why would I want to use the Freebase API at all, if I should have to manually parse a JSON result stream anyway? 如果我不得不手动解析JSON结果流,为什么我要使用Freebase API呢?

NB: eventually I would like to query for larger amounts of data; 注意:最终我想查询更多的数据; the above query is just a simple example. 上面的查询只是一个简单的例子。

You're correct, as it currently stands the standard Google Java client library doesn't offer a lot of benefit over just making the Freebase API calls yourself. 你是对的,因为它目前只有标准的Google Java客户端库,并不能提供比自己进行Freebase API调用更多的好处。 This is because the Freebase MQL API can return arbitrarily complex JSON data which makes it difficult for the client library to do anything fancy with it. 这是因为Freebase MQL API可以返回任意复杂的JSON数据,这使得客户端库很难对它做任何奇怪的事情。 I like the json-simple library for parsing the results. 我喜欢用于解析结果的json-simple库。

The following snippet uses the Google HTTP client library and json-simple to issue a Freebase query and parse the result. 以下代码段使用Google HTTP客户端库和json-simple发出Freebase查询并解析结果。

HttpTransport httpTransport = new NetHttpTransport();
HttpRequestFactory requestFactory = httpTransport.createRequestFactory();
JSONParser parser = new JSONParser();
String query = "[{\"limit\": 5,\"name\":null,\"type\":\"/medicine/disease\"}]";
GenericUrl url = new GenericUrl("https://www.googleapis.com/freebase/v1/mqlread");
url.put("key", "YOUR-API-KEY-GOES-HERE");
url.put("query", query);
HttpRequest request = requestFactory.buildGetRequest(url);
HttpResponse httpResponse = request.execute();
JSONObject response = (JSONObject)parser.parse(httpResponse.parseAsString());
JSONArray results = (JSONArray)response.get("result");
for (Object result : results) {
  System.out.println(result.get("name").toString());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM