java - Freebase API 文档中 Java 示例程序的文件未找到错误

标签 java semantic-web freebase mql

我正在尝试给出 here 的 Java 示例程序在 Freebase 文档中。

这是程序

import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.jayway.jsonpath.JsonPath;
import java.io.FileInputStream;
import java.util.Properties;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class TopicSample {
  public static Properties properties = new Properties();
  public static void main(String[] args) {
    try {
      properties.load(new FileInputStream("freebase.properties"));
      HttpTransport httpTransport = new NetHttpTransport();
      HttpRequestFactory requestFactory = httpTransport.createRequestFactory();
      JSONParser parser = new JSONParser();
      String query = "[{\"id\":null,\"name\":null,\"type\":\"/astronomy/planet\"}]";
      GenericUrl url = new GenericUrl("https://www.googleapis.com/freebase/v1/mqlread");
      url.put("query", query);
      url.put("key", properties.get("API_KEY"));
      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(JsonPath.read(result,"$.name").toString());
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
}

我还用我自己生成的 key 替换了“API_KEY”

当我运行这个程序时,我收到一个错误

java.io.FileNotFoundException: freebase.properties (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.io.FileInputStream.<init>(FileInputStream.java:93)
    at code.TopicSample.main(TopicSample.java:29)
BUILD SUCCESSFUL (total time: 0 seconds)

我必须创建“freebase.properties”文件吗?该文件的内容是什么?我浏览了几乎完整的 Freebase API 文档,但找不到有关此文件的任何线索。这个文件可以下载吗?我希望获得有关此文件的信息的链接。

最佳答案

是的,从外观上看。您会注意到您调用了 properties.get("API_KEY") - 所以大概您需要将 API_KEY 放入属性文件中。

因此,一个名为 freebase.properties 的文件包含:

API_KEY = <your actual API key>

关于java - Freebase API 文档中 Java 示例程序的文件未找到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29200769/

相关文章:

java - 在字段值无效的情况下如何更改Struts2验证错误消息?

rdf - N3/Turtle本体语言创建类有没有捷径

jquery - 删除 freebase 建议列表中的类型

java - 将 Freebase Unicode 代码点转换为 Java 字符串

java - HIbernate-MySQL5Dialect 想要删除外键而不是索引

java - Java 中的线程可以缓存 final 字段吗?

java - 如何使用 JTable 使用 MessageFormat 打印多个标题行

RDF - 分发 rdf :type to all items in the list

java - AllegroGraph 客户端 HTTPException

php - Freebase 上 MQL 中的多个查询