我正在使用Google Spreadsheet API來更新包含大量數據(數百行和大約20列)的電子表格。什麼是通過電子表格api更新大量數據的谷歌電子表格的最快方法?
我測試了一個批量調用來更新2500個單元格。通話需要大約40秒才能完成,請求約爲1mb,響應爲2mb。
有什麼辦法讓它更快的工作嗎?
我正在使用Google Spreadsheet API來更新包含大量數據(數百行和大約20列)的電子表格。什麼是通過電子表格api更新大量數據的谷歌電子表格的最快方法?
我測試了一個批量調用來更新2500個單元格。通話需要大約40秒才能完成,請求約爲1mb,響應爲2mb。
有什麼辦法讓它更快的工作嗎?
如果要更新整個行,你可以嘗試用基於列表的飼料工作:
http://code.google.com/intl/fr-FR/apis/spreadsheets/data/3.0/developers_guide.html#UpdatingListRows
它可以讓你更新值(不是公式)。
如果仍然有性能問題,你應該(如果您正在使用谷歌應用程序引擎的工作)切換到像關係數據庫服務器或谷歌的數據存儲
謝謝。但我明白沒有辦法對行進行批量更新,這意味着數百個請求.http://www.google.com/support/forum/p/apps-apis/thread?tid = 5db19d43cb454e7e&hl = en 使用不同的數據存儲不適合我,因爲我的應用程序是爲了操縱谷歌電子表格而不是存儲這些數據而構建的。 – Daniel
我能夠加快在提供的批量要求官方API http://code.google.com/apis/spreadsheets/data/3.0/developers_guide.html#SendingBatchRequests通過在UPDATE之前跳過QUERY部分。因此,這是他們在例如:
// Prepare the update
// getCellEntryMap is what makes the update fast.
Map cellEntries = getCellEntryMap(ssSvc, cellFeedUrl, cellAddrs);
CellFeed batchRequest = new CellFeed();
for (CellAddress cellAddr : cellAddrs) {
URL entryUrl = new URL(cellFeedUrl.toString() + "/" + cellAddr.idString);
CellEntry batchEntry = new CellEntry(cellEntries.get(cellAddr.idString));
batchEntry.changeInputValueLocal(cellAddr.idString);
BatchUtils.setBatchId(batchEntry, cellAddr.idString);
BatchUtils.setBatchOperationType(batchEntry, BatchOperationType.UPDATE);
batchRequest.getEntries().add(batchEntry);
}
// Submit the update
Link batchLink = cellFeed.getLink(Link.Rel.FEED_BATCH, Link.Type.ATOM);
CellFeed batchResponse = ssSvc.batch(new URL(batchLink.getHref()), batchRequest);
,這就是我把它改成
CellFeed batchRequest = new CellFeed();
for (CellInfo cellAddr : cellsInfo) {
CellEntry batchEntry = new CellEntry(cellAddr.row, cellAddr.col, cellAddr.idString);
batchEntry.setId(String.format("%s/%s", worksheet.getCellFeedUrl().toString(), cellAddr.idString));
BatchUtils.setBatchId(batchEntry, cellAddr.idString);
BatchUtils.setBatchOperationType(batchEntry, BatchOperationType.UPDATE);
batchRequest.getEntries().add(batchEntry);
}
CellFeed cellFeed = ssSvc.getFeed(worksheet.getCellFeedUrl(), CellFeed.class);
Link batchLink = cellFeed.getLink(Link.Rel.FEED_BATCH, Link.Type.ATOM);
ssSvc.setHeader("If-Match", "*");
CellFeed batchResponse = ssSvc.batch(new URL(batchLink.getHref()), batchRequest);
ssSvc.setHeader("If-Match", null);
公告,標題應該改變,使其工作。
很棒的回答。謝謝。它減少了插入時間到4.5秒。 –
If-Match標題是關鍵!謝謝。 –
加速:張貼由大衛Tolioupov - 它的作品。一些額外的信息,幫助。
的如何使用CellFeed示例,請參見CellDemo.java http://gdata-java-client.googlecode.com/svn-history/r51/trunk/java/sample/spreadsheet/cell/CellDemo.java
的例子有更多的細節,足夠的細節,它幫我優化我的代碼。
如前所述由大衛Tolioupov,創建CellEntry這樣:
CellEntry batchEntry = new CellEntry(cellAddr.row, cellAddr.col, cellAddr.idString);
batchEntry.setId(String.format("%s/%s", cellFeedUrl.toString(), cellAddr.idString));
從例如:
/**
* Returns a CellEntry with batch id and operation type that will tell the
* server to update the specified cell with the given value. The entry is
* fetched from the server in order to get the current edit link (for
* optimistic concurrency).
*
* @param row the row number of the cell to operate on
* @param col the column number of the cell to operate on
* @param value the value to set in case of an update the cell to operate on
*
* @throws ServiceException when the request causes an error in the Google
* Spreadsheets service.
* @throws IOException when an error occurs in communication with the Google
* Spreadsheets service.
*/
private CellEntry createUpdateOperation(int row, int col, String value)
throws ServiceException, IOException {
String batchId = "R" + row + "C" + col;
URL entryUrl = new URL(cellFeedUrl.toString() + "/" + batchId);
CellEntry entry = service.getEntry(entryUrl, CellEntry.class);
entry.changeInputValueLocal(value);
BatchUtils.setBatchId(entry, batchId);
BatchUtils.setBatchOperationType(entry, BatchOperationType.UPDATE);
return entry;
}
所有需要的是cellFeedUrl,然後創建請求和發送。
您使用的是單元格源或列表源? – Simon
我正在使用細胞飼料 – Daniel