04 May 2007

Hello World in MQL/ Freebase.

OK, here are my very first tests in freebase.
The MQL query language is used to send the query to the server. It is based on the JSON format.

The following MQL query asks for the date/place of birth, the gender, for all the biologists in freebase.


{
"query":[{
"/people/person/date_of_birth":null,
"/people/person/gender":null,
"/people/person/place_of_birth":null,
"/people/person/profession":"biologist",
"name":null
}]
}


This query can be copied in the freebase query editor (http://www.freebase.com/view/queryeditor/) or can be send via http (see below my example in java).

Here is the result formated in JSON as it appears in the editor:


{
"q1": {
"status": "/mql/status/ok",
"result": [
{
"/people/person/date_of_birth": "1809-12-02",
"/people/person/profession": "Biologist",
"/people/person/gender": "Male",
"name": "Charles Darwin",
"/people/person/place_of_birth": "Shrewsbury"
},
{
"/people/person/date_of_birth": "1859",
"/people/person/profession": "Biologist",
"/people/person/gender": "Male",
"name": "Jacques Loeb",
"/people/person/place_of_birth": null
}
]
},
"status": "200 OK"
}


Only two records ! :-) because as far as I could see, Freebase was automatically generated from wikipedia (name, description ...) whereas most fields (gender, date of birth etc...) remain empty and are waiting for the users to be filled.

Iv' also tried to send this query via JAVA.


import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;





public class Test01 {
static final String BASE_URL="http://www.freebase.com";
static final String MQLREADURL=BASE_URL+"/api/service/mqlread";


void query(String json) throws IOException
{
String envelope = "{\"qname\":{\"query\":" +json+ "}}";
String urlStr =MQLREADURL+"?queries="+URLEncoder.encode(envelope, "UTF-8");

// Now place the query in JSON envelope objects, and URL encode the envelopes
URL url= new URL(urlStr);
URLConnection con=url.openConnection();
//set the cookie
con.setRequestProperty("Cookie","metaweb-user="+###YOUR_COOKIE_VALUE###);
con.connect();
InputStream in=con.getInputStream();
int c;
//echo the result to stdout
while((c=in.read())!=-1) System.out.write(c);
in.close();
}


public static void main(String[] args)
{
try {
Test01 t= new Test01();
t.query("[{ \"/people/person/date_of_birth\":null, \"/people/person/gender\":null, \"/people/person/place_of_birth\":null, \"/people/person/profession\":\"biologist\", \"name\":null }] ");

} catch (Exception e) {
e.printStackTrace();
}
}

}


This source uses a URLConnection object to send the query. You may notice that the value of a cookie called metaweb-user must be provided. This cookie identifies your account on freebase and its value can be found in the "preferences panel: Privacy" in firefox.

Pierre.

5 comments:

Abhay said...

Do you have a freebase invite ? Could you please give me one ?

Pierre Lindenbaum said...

Sure, I think I got 5, just send me your email at plindenbaum on yahoo

Gilad Goren said...

I will be happy to have an invitation. Tx. Gilad.Goren@gmail.com

Anonymous said...

great post, thanks! Do you have a similar simple example for the write service of freebase?

Pierre Lindenbaum said...

Not yet, sorry :-)