gpt4 book ai didi

java - Google Apps Admin Java API 中的批处理操作

转载 作者:太空狗 更新时间:2023-10-29 22:58:14 26 4
gpt4 key购买 nike

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

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

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

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

我不想逐一执行这些操作,而是希望将它们分批执行。谁能告诉我怎么做?

最佳答案

看这里: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();

请记住:

The body of each part is itself a complete HTTP request, with its own verb, URL, headers, and body. The HTTP request must only contain the path portion of the URL; full URLs are not allowed in batch requests.

更新

另请参阅此处,了解如何使用 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)

关于java - Google Apps Admin Java API 中的批处理操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29789167/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com