我是Amazon DynamoDB的新手。我目前有20000行需要添加到表中。但是,根據我讀過的內容,似乎我最多隻能使用25個WriteRequests的BatchWriteItem類一次寫入25行。可以增加這個嗎?我怎樣才能一次寫超過25行?目前大約需要15分鐘來編寫所有20000行。謝謝。如何爲DynamoDB在表中寫入超過25個項目/行?
2
A
回答
4
只能發送最多25個項目在一個單一的BatchWriteItem請求,但是隻要你想一次就可以發送儘可能多的BatchWriteItem請求。假設你有provisioned enough write throughput,你應該能夠通過在多個線程/進程/主機之間分割這些20k行並將它們並行推送到數據庫來顯着加速。
這也許對小數據集的位重量級的,但你可以使用AWS Data Pipeline從S3提取數據。它基本上自動化了創建Hadoop集羣的過程,以便從S3中抽取數據並通過一堆並行的BatchWriteItem請求將其發送到DynamoDB。
1
我一直在尋找一些代碼使用JavaScript SDK做到這一點。我找不到它,所以我把它放在一起。我希望這可以幫助別人!
function multiWrite(table, data, cb) {
var AWS = require('aws-sdk');
var db = new AWS.DynamoDB.DocumentClient({region: 'us-east-1'});
// Build the batches
var batches = [];
var current_batch = [];
var item_count = 0;
for(var x in data) {
// Add the item to the current batch
item_count++;
current_batch.push({
PutRequest: {
Item: data[x]
}
});
// If we've added 25 items, add the current batch to the batches array
// and reset it
if(item_count%25 == 0) {
batches.push(current_batch);
current_batch = [];
}
}
// Add the last batch if it has records and is not equal to 25
if(current_batch.length > 0 && current_batch.length != 25) batches.push(current_batch);
// Handler for the database operations
var completed_requests = 0;
var errors = false;
function handler(request) {
return function(err, data) {
// Increment the completed requests
completed_requests++;
// Set the errors flag
errors = (errors) ? true : err;
// Log the error if we got one
if(err) {
console.error(JSON.stringify(err, null, 2));
console.error("Request that caused database error:");
console.error(JSON.stringify(request, null, 2));
}
// Make the callback if we've completed all the requests
if(completed_requests == batches.length) {
cb(errors);
}
}
}
// Make the requests
var params;
for(x in batches) {
// Items go in params.RequestItems.id array
// Format for the items is {PutRequest: {Item: ITEM_OBJECT}}
params = '{"RequestItems": {"' + table + '": []}}';
params = JSON.parse(params);
params.RequestItems[table] = batches[x];
// Perform the batchWrite operation
db.batchWrite(params, handler(params));
}
}
0
function putInHistory(data,cb) {
var arrayOfArray25 = _.chunk(data, 25);
async.every(arrayOfArray25, function(arrayOf25, callback) {
var params = {
RequestItems: {
[TABLES.historyTable]: []
}
};
arrayOf25.forEach(function(item){
params.RequestItems[TABLES.historyTable].push({
PutRequest: {
Item: item
}
})
});
docClient.batchWrite(params, function(err, data) {
if (err){
console.log(err);
callback(err);
} else {
console.log(data);
callback(null, true);
};
});
}, function(err, result) {
if(err){
cb(err);
} else {
if(result){
cb(null,{allWritten:true});
} else {
cb(null,{allWritten:false});
}
}
});
}
您可以使用lodash使從陣列的數據塊,然後使用異步庫的每個/所有的方法都是做batchWrite上25種元素的大塊
相關問題
- 1. 如何使用python將項目放入DynamoDB表中?
- 2. DynamoDB ProvisionedReadCapacity超過
- 3. 通過Java代碼將多個項目放入DynamoDB中
- 4. DynamoDB:如何存儲項目列表
- 5. 在sparkoDB表中寫入火花數據幀行作爲項目
- 6. 需要超過25
- 7. DynamoDB寫入操作尖峯行爲
- 8. 如何一次更新DynamoDB表中的多個項目
- 9. 如何在行和下一個數組的相同數組中寫入項目跳過一行並在Python中的下一行寫入項目
- 10. 如何使用boto3有條件地插入一個項目到dynamodb表中
- 11. NSMutableArray只有25個項目
- 12. 如何刪除特定項目在DynamoDB
- 13. 如何編寫SQL過程從列表中一次插入多個項目?
- 14. 將列表項目寫入一行
- 15. 如何在mongodb中迭代超過100個項目
- 16. 如何創建超過2個項目的自定義列表
- 17. 多個項目查詢DynamoDb
- 18. 獲取超過25個Facebook牆貼
- 19. 只從dynamodb表中檢索項目值?
- 20. 如何更新DynamoDB中列表中的項目?
- 21. 如何返回插入的項目中dynamoDB
- 22. 如何根據DynamoDB中插入的順序獲取項目?
- 23. 如何寫不爲空,並在SQL原表達> 25
- 24. 如何檢索最接近DynamoDB中其他項目的項目?
- 25. 如何存儲在SQL表中的單個列超過1項
- 26. dynamodb批量寫入更新現有項目
- 27. 如何在dynamodb中超過1mb的掃描數據限制
- 28. 如何獲取超過25個發佈消息
- 29. dynamodb交易寫入
- 30. 從Spark寫入DynamoDB
謝謝大衛。我會嘗試使用一些並行線程。吞吐量是多少? – codeshark
如果我使用AWS Data Pipeline,是否意味着我應該將所有數據從我的應用程序輸出到S3中?是輸出到S3 ==>數據管道==> DynamoDB與對比的好處。直接寫入DynamoDB將文件寫入S3的速度? – codeshark
我在鏈接中編輯了有關預配置吞吐量的更多信息,但簡短的故事是您在創建表時提前告訴DynamoDB每秒要對其執行的讀/寫數量。如果您發送的請求比這更快,超出的請求將被拒絕。 –