0

I am trying the sample program in Java given here in the Freebase documentation.

Here is the program

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

I have also replaced "API_KEY" with my own generated key.

When I run this program I am getting an error

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)

Do I have to create "freebase.properties" file? What would be the content of that file? I have skimmed through almost the complete documentation of the Freebase API but I could not find any clue about this file. Is this file available for download? I would appreciate a link to the information about this file.

Rajesh Surana
  • 883
  • 1
  • 10
  • 15

1 Answers1

1

Yes, by the looks of it. You'll notice you have a call to properties.get("API_KEY") - so presumably you need to put your API_KEY in the properties file.

So, a file called freebase.properties containing:

API_KEY = <your actual API key>

  • I have already mentioned that in my code I have replaced API_KEY with my genrated key. I can not put my actual key here in the public post. – Rajesh Surana Mar 22 '15 at 23:16
  • No, you need to leave it as is and put it in the freebase.properties file in the format above. – Fluffmeister General Mar 22 '15 at 23:17
  • What I mean is: create a file called freebase.properties and copy the line above but put your real API key instead - do this in **the file** - leave the `properties.get("API_KEY")` exactly as it is in your code. – Fluffmeister General Mar 22 '15 at 23:18