Skip to content

How to Use the Write Service

narphorium edited this page Sep 13, 2010 · 1 revision

Simple Write Query

Load a parametrized MQL query from a file, set the parameter values, authenticate and write the data to Freebase. This example adds the Country type to the Canada topic.

MQL Query

{
  "topic_id:id" : null,
  "type" : {
    "connect" : "insert",
    "topic_type:id" : null
  }
}

Java Code

try {
	WriteService writeService = new WriteService();
	writeService.authenticate("johnsmith", "abc123");
	QueryParser queryParser = new QueryParser();
	Query query = queryParser.parse(new File("add_type_to_topic.mql"));
	query.setParameterValue("topic_id", "/en/canada");
	query.setParameterValue("topic_type", "/location/country");
	writeService.write(query);
} catch (FreebaseServiceException e) {
	e.printStackTrace();
}

Make sure that you use your own username and password in your own code. It’s also not a good idea to hard-code the password into the source like that, I just did it to make the example easier to read.

Writing to the Sandbox

Its always a good idea to test your writes on the Freebase sandbox before running them on the main graph. To set this up in your code all you have to do is point your WriteService at the sandbox URL like this:

WriteService writeService = new WriteService(new URL("http://sandbox.freebase.com/api"));
Clone this wiki locally