【发布时间】:2015-03-22 22:45:28
【问题描述】:
我正在尝试 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 文档,但我找不到关于这个文件的任何线索。这个文件可以下载吗?我将不胜感激有关此文件的信息的链接。
【问题讨论】:
标签: java semantic-web freebase mql