How to query Freebase from c#…

Freebase is a great source of open data,we can query it using
MQL.I will try and show how we can quickly query it to get some data.

Firstly I used .net 4 ,visual studio sp1, nuget(Awesome package management) and two cool packages(details about the packages follow).

The objective is to retrieve a set of albums of “The Police”.


class Album
 {
public String type {get;set;}
public String name {get;set;}
public String[] album { get; set; }
}

Notice that we need to get a set of “albums” for the artist specified by “name”.The search criteria also needs a domain to look into which we will specify through “type”.

Next,We need to set up a Json Object and send it across as a parameter over HTTP.We can consume the Rest based service through a wonderful nuget packageRestSharp.


var client = new RestClient();
client.BaseUrl = ”https://www.freebase.com/api/service/mqlread”;
var request = new RestRequest(Method.GET);

Now,we need to create a album object to pass along with the request.


Album thepolice = new Album{type = ”/music/artist”,name = ”The Police” };

Next we need to serialize the object,so we will use NewtonSoft(which you won’t need to install since it is dependency for the RestSharp package and nuget will fetch it beforehand).

Freebase follows the fill in the blanks approach to return data i.e. for the following MQL query

Freebase will return results where ever it finds null(name,guid) or [](/location/location/containedby,/location/place_with_neighborhoods/neighborhoods).
The places with null will be substituted with a single result and [] will be substituted with a set of results.

So we now have to convert our album object into a Json Object and attach it to our request.Initially we add the following method to the Album class.


class Album {
public String type {get;set;}
public String name {get;set;}
public String[] album { get; set; }
public String ToJsonString()
{
  return "{" +  JsonConvert.SerializeObject(“query”, Formatting.Indented) + ":" +
  JsonConvert.SerializeObject(this, Formatting.Indented) + "\n" +   "}";}
}

Album thepolice = new Album{type = ”/music/artist”,name = ”The Police” };

Console.WriteLine(thepolice.ToJsonString());//Just for debug purposes
request.AddParameter(“query”, thepolice.ToJsonString());
request.RequestFormat = DataFormat.Json;
RestResponse response = client.Execute(request);
Console.WriteLine(“My output :” +”\n”+response.Content);

The output we get is as follows:


{“query”:{
“type”: “/music/artist”,
“name”: “The Police”,
“album”: null
}}
My output :
{
“code”: “/api/status/error”,
“messages”: [
{
“code”: “/api/status/error/mql/result”,
“info”: {
“count”: 31,
“result”: [
“Outlandos d’Amour”,“Reggatta de Blanc”,“Zenyatt\u00e0 Mondatta”,
“Ghost in the Machine”,“Synchronicity”,“Every Breath You Take: The Singles”,
“Greatest Hits”,“Message in a Box: The Complete Recordings”,
“Live!”,“Every Breath You Take: The Classics”,
“Their Greatest Hits”,“Can’t Stand Losing You”,
“Roxanne ‘97 (Puff Daddy remix)”,
“Roxanne ‘97”,“The Police”,“Greatest Hits”,
]
},
“message”: “Unique query may have at most one result. Got 31”,
“path”: “album”,
“query”: {
“album”: null,
“error_inside”: “album”,
“name”: “The Police”,
“type”: “/music/artist”}
}
],
“status”: “200 OK”,
“transaction_id”: “cache;cache04.p01.sjc1:8101;2011-09-06T20:48:46Z;0011”
}

Note carefully two things:-

1. The value of null instead of [] for album,this justifies the message we get in the output

2.If your query is wrong the status code will change to 400,here the code is 200 indicating the request is good.

Now let us try a quick fix of the problem

The method will be tweaked to make way for the [] instead of null to capture a set of albums instead of a single album.


public String ToJsonString()
{
return ”{“ +
JsonConvert.SerializeObject(“query”, Formatting.Indented) + ”:” +
”{“ + ”\n” +
JsonConvert.SerializeObject(“type”, Formatting.Indented) + ”:” +
JsonConvert.SerializeObject(this.type, Formatting.Indented) + ”,” + ”\n” +
JsonConvert.SerializeObject(“name”, Formatting.Indented) + ”:” +
JsonConvert.SerializeObject(this.name, Formatting.Indented) + ”,” + ”\n” +
JsonConvert.SerializeObject(“album”, Formatting.Indented) + ”:” +
”[]” + ”\n” + ”}” + ”\n” + ”}”;
}

The output is as follows:-


{“query”:{
“type”:”/music/artist”,
“name”:”The Police”,
“album”:[]}
}
My output :
{
“code”: “/api/status/ok”,
“result”: {
“album”: [
“Outlandos d’Amour”, “Reggatta de Blanc”,“Zenyatt\u00e0 Mondatta”,
“Ghost in the Machine”, “Synchronicity”,“Every Breath You Take: The Singles”,
“Greatest Hits”,“Message in a Box: The Complete Recordings”,
“Live!”,“Every Breath You Take: The Classics”,
“Their Greatest Hits”,“Can’t Stand Losing You”,
“Roxanne ‘97 (Puff Daddy remix)”,“Roxanne ‘97”,
“The Police”,“Greatest Hits”,
],
“name”: “The Police”,
“type”: “/music/artist”
},
“status”: “200 OK”,
“transaction_id”: “cache;cache04.p01.sjc1:8101;2011-09-06T21:02:11Z;0035”
}

Notice how everything becomes well and the error message goes away and the code in the first line changes from error to OK.

The next logical step would be to generate a helper method to give us a JsonString for all properties including lists,dictionary etc. I will try and address that in the next blog post.Hope this helps you start with Freebase.

I have omitted some set of results(not 31 if you count them :P) to keep it easy on the eye.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s