Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.
Bagian ini memberikan contoh operasi batch write dan batch get di Amazon DynamoDB menggunakan AWS SDK for Java Document API.
catatan
SDK untuk Java juga menyediakan model persistensi objek, memungkinkan Anda memetakan kelas sisi klien ke tabel DynamoDB. Pendekatan ini dapat mengurangi jumlah kode yang harus Anda tulis. Untuk informasi selengkapnya, lihat Java 1.x: Dinamo DBMapper.
Topik
Contoh: Operasi batch write menggunakan AWS SDK for Java document API
Contoh kode Java berikut menggunakan metode batchWriteItem
untuk melakukan operasi put dan delete berikut:
-
Tempatkan satu item dalam tabel
Forum
. -
Tempatkan satu item dan hapus satu item dari tabel
Thread
.
Anda dapat menentukan sejumlah permintaan put dan delete terhadap satu atau beberapa tabel saat membuat permintaan batch write Anda. Namun, batchWriteItem
membatasi ukuran permintaan penulisan batch dan jumlah operasi put dan delete dalam satu operasi penulisan batch. Jika permintaan Anda melebihi batas ini, permintaan Anda ditolak. Jika tabel Anda tidak memiliki cukup throughput yang disediakan untuk melayani permintaan ini, item permintaan yang belum diproses akan dikembalikan sebagai respons.
Contoh berikut memeriksa respons untuk melihat apakah ada item permintaan yang belum diproses. Jika ya, contoh tersebut akan mengulang kembali dan mengirim ulang permintaan batchWriteItem
dengan item yang belum diproses dalam permintaan tersebut. Jika Anda mengikuti contoh dalam panduan ini, Anda seharusnya sudah membuat Forum
dan Thread
tabel. Anda juga dapat membuat tabel ini dan mengunggah data sampel secara terprogram. Untuk informasi selengkapnya, lihat Membuat tabel contoh dan mengunggah data menggunakan AWS SDK for Java.
Untuk step-by-step instruksi untuk menguji sampel berikut, lihatContoh kode Java.
contoh
package com.amazonaws.codesamples.document;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.amazonaws.services.dynamodbv2.document.BatchWriteItemOutcome;
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import com.amazonaws.services.dynamodbv2.document.Item;
import com.amazonaws.services.dynamodbv2.document.TableWriteItems;
import com.amazonaws.services.dynamodbv2.model.WriteRequest;
public class DocumentAPIBatchWrite {
static AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build();
static DynamoDB dynamoDB = new DynamoDB(client);
static String forumTableName = "Forum";
static String threadTableName = "Thread";
public static void main(String[] args) throws IOException {
writeMultipleItemsBatchWrite();
}
private static void writeMultipleItemsBatchWrite() {
try {
// Add a new item to Forum
TableWriteItems forumTableWriteItems = new TableWriteItems(forumTableName) // Forum
.withItemsToPut(new Item().withPrimaryKey("Name", "Amazon RDS").withNumber("Threads", 0));
// Add a new item, and delete an existing item, from Thread
// This table has a partition key and range key, so need to specify
// both of them
TableWriteItems threadTableWriteItems = new TableWriteItems(threadTableName)
.withItemsToPut(
new Item().withPrimaryKey("ForumName", "Amazon RDS", "Subject", "Amazon RDS Thread 1")
.withString("Message", "ElastiCache Thread 1 message")
.withStringSet("Tags", new HashSet<String>(Arrays.asList("cache", "in-memory"))))
.withHashAndRangeKeysToDelete("ForumName", "Subject", "Amazon S3", "S3 Thread 100");
System.out.println("Making the request.");
BatchWriteItemOutcome outcome = dynamoDB.batchWriteItem(forumTableWriteItems, threadTableWriteItems);
do {
// Check for unprocessed keys which could happen if you exceed
// provisioned throughput
Map<String, List<WriteRequest>> unprocessedItems = outcome.getUnprocessedItems();
if (outcome.getUnprocessedItems().size() == 0) {
System.out.println("No unprocessed items found");
} else {
System.out.println("Retrieving the unprocessed items");
outcome = dynamoDB.batchWriteItemUnprocessed(unprocessedItems);
}
} while (outcome.getUnprocessedItems().size() > 0);
} catch (Exception e) {
System.err.println("Failed to retrieve items: ");
e.printStackTrace(System.err);
}
}
}
Contoh: Operasi batch get menggunakan AWS SDK for Java
document API
Contoh kode Java berikut menggunakan metode batchGetItem
untuk mengambil beberapa item dari tabel Forum
dan Thread
. BatchGetItemRequest
menentukan nama tabel dan daftar kunci untuk setiap item yang akan didapatkan. Contoh tersebut memproses respons dengan mencetak item yang diambil.
catatan
Contoh kode ini mengasumsikan bahwa Anda telah memuat data ke DynamoDB untuk akun Anda dengan mengikuti petunjuk di bagian Membuat tabel dan memuat data untuk contoh kode di DynamoDB.
Untuk step-by-step instruksi untuk menjalankan contoh berikut, lihatContoh kode Java.
contoh
package com.amazonaws.codesamples.document;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.amazonaws.services.dynamodbv2.document.BatchGetItemOutcome;
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import com.amazonaws.services.dynamodbv2.document.Item;
import com.amazonaws.services.dynamodbv2.document.TableKeysAndAttributes;
import com.amazonaws.services.dynamodbv2.model.KeysAndAttributes;
public class DocumentAPIBatchGet {
static AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build();
static DynamoDB dynamoDB = new DynamoDB(client);
static String forumTableName = "Forum";
static String threadTableName = "Thread";
public static void main(String[] args) throws IOException {
retrieveMultipleItemsBatchGet();
}
private static void retrieveMultipleItemsBatchGet() {
try {
TableKeysAndAttributes forumTableKeysAndAttributes = new TableKeysAndAttributes(forumTableName);
// Add a partition key
forumTableKeysAndAttributes.addHashOnlyPrimaryKeys("Name", "Amazon S3", "Amazon DynamoDB");
TableKeysAndAttributes threadTableKeysAndAttributes = new TableKeysAndAttributes(threadTableName);
// Add a partition key and a sort key
threadTableKeysAndAttributes.addHashAndRangePrimaryKeys("ForumName", "Subject", "Amazon DynamoDB",
"DynamoDB Thread 1", "Amazon DynamoDB", "DynamoDB Thread 2", "Amazon S3", "S3 Thread 1");
System.out.println("Making the request.");
BatchGetItemOutcome outcome = dynamoDB.batchGetItem(forumTableKeysAndAttributes,
threadTableKeysAndAttributes);
Map<String, KeysAndAttributes> unprocessed = null;
do {
for (String tableName : outcome.getTableItems().keySet()) {
System.out.println("Items in table " + tableName);
List<Item> items = outcome.getTableItems().get(tableName);
for (Item item : items) {
System.out.println(item.toJSONPretty());
}
}
// Check for unprocessed keys which could happen if you exceed
// provisioned
// throughput or reach the limit on response size.
unprocessed = outcome.getUnprocessedKeys();
if (unprocessed.isEmpty()) {
System.out.println("No unprocessed keys found");
} else {
System.out.println("Retrieving the unprocessed keys");
outcome = dynamoDB.batchGetItemUnprocessed(unprocessed);
}
} while (!unprocessed.isEmpty());
} catch (Exception e) {
System.err.println("Failed to retrieve items.");
System.err.println(e.getMessage());
}
}
}