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

File not found error for sample program in Java on Freebase API documentation

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

这是程序

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();
    }
  }
}

我也用我自己生成的密钥替换了 "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 的完整文档,但我找不到关于此文件的任何线索。这个文件可以下载吗?如果能提供有关此文件的信息,我将不胜感激 link。

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

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

API_KEY = <your actual API key>