You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kotlin中如何为AWS DynamoDB Enhanced Java客户端V2的WriteBatch Builder批量添加Put请求

How to create a BatchWriteItemEnhancedRequest with a list of put requests in Kotlin (AWS DynamoDB Enhanced Client V2)

I'm using the AWS DynamoDB Enhanced Java Client V2 in Kotlin and need to create a BatchWriteItemEnhancedRequest that includes multiple put requests. According to the official docs, the standard usage looks like this:

val batchWriteItemEnhancedRequest = BatchWriteItemEnhancedRequest.builder()
    .addWriteBatch(
        WriteBatch.builder(MyClass::class.java)
            .mappedTableResource(myMappedTable)
            .addPutItem(putRequest1)
            .addPutItem(putRequest2)
            .addPutItem(putRequest3)
            .build()
    )
    .build()

Obviously this approach can't handle a list of put requests directly—it only lets you add discrete, known requests one by one. After checking the docs, it seems the WriteBatch.Builder doesn't support adding a list of requests directly.

The only implementation I can think of right now uses a for loop:

var writeBatchBuilder = WriteBatch.builder(MyClass::class.java)
    .mappedTableResource(myMappedTable)
for (request in putItemRequests){
    writeBatchBuilder = writeBatchBuilder.addPutItem(request)
}
val writeBatch = writeBatchBuilder.build()

But this feels inelegant to me. Is there a more idiomatic Kotlin way to do this?


Great question—Kotlin has several neat ways to clean this up and avoid that mutable var and explicit loop. Here are a few options:

1. Use apply with forEach

The apply function lets you configure an object inline, and you can chain a forEach to add all your put requests without reassigning the builder:

val writeBatch = WriteBatch.builder(MyClass::class.java)
    .mappedTableResource(myMappedTable)
    .apply {
        putItemRequests.forEach { addPutItem(it) }
    }
    .build()

This keeps everything in a single expression, avoids mutable variables, and reads very naturally—you're setting up the builder, applying the list of requests to it, then building.

2. Create an extension function (for reusability)

If you find yourself doing this often, you can add an extension function to WriteBatch.Builder to accept a list directly. This makes your code even cleaner:

fun <T> WriteBatch.Builder<T>.addPutItems(items: List<T>): WriteBatch.Builder<T> {
    items.forEach { addPutItem(it) }
    return this
}

Then you can use it like this:

val writeBatch = WriteBatch.builder(MyClass::class.java)
    .mappedTableResource(myMappedTable)
    .addPutItems(putItemRequests)
    .build()

This turns the list addition into a single, readable method call—just like the built-in addPutItem but for lists.

3. Use fold (functional style)

If you prefer a more functional approach, fold can accumulate the builder state without mutable variables:

val writeBatch = putItemRequests.fold(
    initial = WriteBatch.builder(MyClass::class.java).mappedTableResource(myMappedTable),
    operation = { builder, item -> builder.addPutItem(item) }
).build()

This starts with the initial configured builder, then folds over each item to add it to the builder, finally calling build() on the accumulated result.

All of these approaches are more idiomatic than the original for loop with a mutable variable—they're concise, immutable (where possible), and fit with Kotlin's style of expressive, inline configuration.

内容的提问来源于stack exchange,提问作者Paul Eaten

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 13:52:49