-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
FreebaseClient.java
46 lines (36 loc) · 1.39 KB
/
FreebaseClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.knowledgebooks.info_spiders;
// From an example by: Pierre Lindenbaum http://plindenbaum.blogspot.com/2007/05/hello-world-in-mql-freebase.html
import com.freebase.json.JSON;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
/**
* Copyright Mark Watson 2008-2010. All Rights Reserved.
* License: LGPL version 3 (http://www.gnu.org/licenses/lgpl-3.0.txt)
*/
public class FreebaseClient {
static final String URL = "https://www.googleapis.com/freebase/v1/mqlread";
public JSON query(String json) throws Exception {
String urlStr = URL + "?query=" + URLEncoder.encode(json, "UTF-8");
URL url = new URL(urlStr);
URLConnection con = url.openConnection();
con.connect();
InputStream in = con.getInputStream();
JSON results = JSON.parse(new InputStreamReader(in));
in.close();
return results;
}
public static void main(String[] args) {
try {
FreebaseClient t = new FreebaseClient();
JSON r = t.query("[{ \"/people/person/date_of_birth\":null, \"/people/person/gender\":null, \"/people/person/place_of_birth\":null, \"/people/person/profession\":\"biologist\", \"name\":null }] ");
System.out.println(r);
} catch (Exception e) {
e.printStackTrace();
}
}
}