Phi*_*lip 15 java google-apps-for-education google-admin-sdk
我编写了一个Java应用程序,用于在我们的Google Apps for Education域上同步Google网上论坛(功能类似于Google Apps School Directory Sync,但针对我们的某些特定需求进行了定制).
同步有效,但速度很慢,因为它正在单独执行每个任务.我知道有批处理操作的 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
}
Run Code Online (Sandbox Code Playgroud)
我不是一个一个地执行这些操作,而是想要批量处理它们.谁能告诉我怎么样?
看这里:批处理 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();
Run Code Online (Sandbox Code Playgroud)
记住,那:
每个部分的主体本身就是一个完整的 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();
Run Code Online (Sandbox Code Playgroud)
从这里:使用 Google Storage Json Api (JAVA) 进行批量请求
| 归档时间: |
|
| 查看次数: |
478 次 |
| 最近记录: |