简体   繁体   English

将新的Freebase API与Jav​​a客户端一起使用

[英]Using the new Freebase API with the java client

So far, I've been playing around with the google-api for java so I could get some data from freebase. 到目前为止,我一直在使用Java的google-api,以便可以从freebase获取一些数据。 I've used mqlread successfully. 我已经成功使用了mqlread。 Now, I'd like to try out the text services. 现在,我想尝试一下文字服务。 Trouble is, I seem to be missing something in the documentation. 麻烦的是,我似乎在文档中丢失了一些东西。 I can't figure out how to use Freebase.Text.get(List id). 我不知道如何使用Freebase.Text.get(List id)。

What should I input on the List id parameter? 我应该在List id参数上输入什么? I've tried out the following: 我已经尝试了以下方法:

    HttpTransport httpTransport = new NetHttpTransport();
    JsonFactory jsonFactory = new JacksonFactory();

    JsonHttpRequestInitializer requestInitializer = new JsonHttpRequestInitializer() {
        public void initialize(JsonHttpRequest jsonHttpRequest) throws IOException {
            FreebaseRequest freebaseRequest = (FreebaseRequest) jsonHttpRequest;
            freebaseRequest.setPrettyPrint(true);
        }
    };

    Freebase freebase = Freebase.builder(httpTransport, jsonFactory).setJsonHttpRequestInitializer(requestInitializer).build();
    ArrayList<String> list = new ArrayList<String>();

    JSONObject j = new JSONObject();
    j.put("id", "en/bob_dylan");
    list.add(j.toJSONString());
    Freebase.Text.Get text = freebase.text().get(list);
    ContentserviceGet get = text.execute();
    System.out.println(get.toPrettyString());

and

list.add("en/linux_kernel");

Both cases returned 404. Any help is appreciated. 这两种情况下返回404。任何帮助表示赞赏。

The Google Freebase API for Java is buggy. 适用于Java的Google Freebase API有问题。 I found the cause of the problem and entered the following issue: http://code.google.com/p/google-api-java-client/issues/detail?id=493 我找到了问题的原因,并输入了以下问题: http : //code.google.com/p/google-api-java-client/issues/detail?id=493

The intended way of using the API is like this, but it doesn't work: 使用API​​的预期方式是这样的,但它不起作用:

ContentserviceGet result = freebase.text().get(Arrays.asList("en", "avenue_q")).execute();

You don't have to pass a JSON string through List, id as a simple string type will do the job. 您不必通过List传递JSON字符串,因为简单的字符串类型即可完成工作,因此id可以完成。 Following is the code to get the text: 以下是获取文本的代码:

    HttpTransport httpTransport = new NetHttpTransport();
    JsonFactory jsonFactory = new JacksonFactory();
    HttpRequestInitializer httpRequestInitializer = new HttpRequestInitializer() {
        @Override
        public void initialize(HttpRequest arg0) throws IOException {
            // TODO Auto-generated method stub
        }
    };
    JsonHttpRequestInitializer requestInitializer = new JsonHttpRequestInitializer() {
        public void initialize(JsonHttpRequest jsonHttpRequest) throws IOException {
            FreebaseRequest freebaseRequest = (FreebaseRequest) jsonHttpRequest;
            freebaseRequest.setPrettyPrint(true);
        }
    };
    Freebase.Builder fbb = new  Freebase.Builder(httpTransport, jsonFactory, httpRequestInitializer);
    Freebase freebase = fbb.setJsonHttpRequestInitializer(requestInitializer).build();
    ArrayList<String> list = new ArrayList<String>();
    try {
        list.add("/en/bob_dylan");
        Freebase.Text.Get text = freebase.text().get(list);
        ContentserviceGet get = text.execute();
        System.out.println(get.toPrettyString());
    } catch (Exception e) {
        e.printStackTrace();
    } 

I haven't played with the Java api, but here are two examples that might help you. 我还没有玩过Java api,但是这里有两个示例可能会对您有所帮助。

API Explorer http://code.google.com/apis/explorer/#_s=freebase&_v=v1-sandbox&_m=text.get&id=en/bob_dylan API资源管理器http://code.google.com/apis/explorer/#_s=freebase&_v=v1-sandbox&_m=text.get&id=zh-CN/bob_dylan

Example of batch request (which is a json example) http://wiki.freebase.com/wiki/ApiText#Batch_Requests 批处理请求示例(这是一个json示例) http://wiki.freebase.com/wiki/ApiText#Batch_Requests

Notice that the id field does not start with a / - eg id=en/bob_dylan in the above example. 请注意,在上面的示例中,id字段不以/开头。例如id = en / bob_dylan。

Alternatively, you can pass it as an array id=["en", "bob_dylan"] 或者,可以把它作为一个数组ID = [“EN”,“bob_dylan”]

Hope this works. 希望这行得通。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM