【问题标题】:Batching operations in Google Apps Admin Java APIGoogle Apps Admin Java API 中的批处理操作
【发布时间】:2015-06-29 14:46:00
【问题描述】:

我编写了一个 Java 应用程序,用于在我们的 Google Apps for Education 域上同步 Google 群组(功能类似于 Google Apps School Directory Sync,但针对我们的一些特定需求进行了定制)。

同步有效,但速度很慢,因为它是单独执行每个任务。我知道batching operations 有 API 接口,但我找不到任何关于如何使用 Java API 实现的示例。

我使用的代码与此类似(身份验证和其他设置在其他地方处理):

try
{
    Member m = new Member ();
    m.setEmail (member);
    m.setRole ("MEMBER");
    service.members ().insert (group, m).execute ();
}
catch (Exception e)
{
    // ERROR handling
}

我不想一个接一个地执行这些操作,而是将它们批处理。谁能告诉我怎么做?

【问题讨论】:

    标签: java google-admin-sdk google-apps-for-education


    【解决方案1】:

    看这里:Batch Java API

    例如:

    BatchRequest batch = new BatchRequest(httpTransport, httpRequestInitializer);
    batch.setBatchUrl(new GenericUrl(/*your customized batch URL goes here*/));
    batch.queue(httpRequest1, dataClass, errorClass, callback);
    batch.queue(httpRequest2, dataClass, errorClass, callback);
    batch.execute();
    

    请记住:

    每个部分的body本身就是一个完整的HTTP请求,有自己的 动词、URL、标题和正文。 HTTP 请求只能包含 URL 的路径部分;批处理请求中不允许使用完整的 URL。

    更新

    也请看这里,如何使用 Google Batch API 构建批处理:

    https://github.com/google/google-api-java-client

    更新 2

    试试这样的:

    // Create the Storage service object
    Storage storage = new Storage(httpTransport, jsonFactory, credential);
    
    // Create a new batch request
    BatchRequest batch = storage.batch();
    
    // Add some requests to the batch request
    storage.objectAccessControls().insert("bucket-name", "object-key1",
        new ObjectAccessControl().setEntity("user-123423423").setRole("READER"))
        .queue(batch, callback);
    storage.objectAccessControls().insert("bucket-name", "object-key2",
        new ObjectAccessControl().setEntity("user-guy@example.com").setRole("READER"))
        .queue(batch, callback);
    storage.objectAccessControls().insert("bucket-name", "object-key3",
        new ObjectAccessControl().setEntity("group-foo@googlegroups.com").setRole("OWNER"))
        .queue(batch, callback);
    
    // Execute the batch request. The individual callbacks will be called when requests finish.
    batch.execute();
    

    从这里:Batch request with Google Storage Json Api (JAVA)

    【讨论】:

    • 你能告诉我你将如何修改我的问题中的示例代码以排队并准备好批量执行吗?在您的示例中,我看不到像 httpRequest1 这样的变量在我的代码 sn-p 中来自哪里。
    • httpRequest1 将是来自您的代码的一个完整请求(整个 try 块)。然后,您需要将其包装到 httpRequest 并为所有迭代执行此操作。最后,您可以执行批处理。 Batch Java API 指定 httpRequest 的外观。
    猜你喜欢
    • 2017-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-12
    • 1970-01-01
    • 2014-07-22
    • 1970-01-01
    相关资源
    最近更新 更多