dynamodbmapper batchdelete方法在Java中不起作用

发布于 2025-02-04 07:14:19 字数 1952 浏览 2 评论 0原文

您好,我正在使用以下依赖性来读取/写作对Spring Boot中的DynamoDB。 在内部,此API调用DynamoDBMapper执行其任何操作。 DynamoDbmapper是AWS SDK。 我相信,DynampMapper批次delete Call in AWS SDK

<dependency>
            <groupId>com.github.derjust</groupId>
            <artifactId>spring-data-dynamodb</artifactId>
            <version>5.1.0</version>
        </dependency>

java.lang.reflect.InaccessibleObjectException: Unable to make final void java.lang.Throwable.setCause(java.lang.Throwable) accessible: module java.base does not "opens java.lang" to unnamed module @1b083826

调用代码看起来像

 List<Matchup> matchups = (List<Matchup>) matchupRepository.findAll();

        DynamoDBMapper dynamoDBMapper = new DynamoDBMapper(amazonDynamoDBClient);
        try {
            dynamoDBMapper.batchDelete(matchups);
        }

        catch(Exception e) {
            log.error(e.toString());
        }

实体类的样子,因此

@Data
@NoArgsConstructor
@AllArgsConstructor
@DynamoDBTable(tableName = "Sportsbook")
public class Matchup {

    @DynamoDBAttribute(attributeName = "MatchupId")
    private String matchupId;

    @DynamoDBAttribute(attributeName = "SportsKey")
    private String sportsKey;

    @DynamoDBAttribute(attributeName = "CommenceTime")
    private String commenceTime;

    @DynamoDBAttribute(attributeName = "HomeTeam")
    private String homeTeam;

    @DynamoDBAttribute(attributeName = "AwayTeam")
    private String awayTeam;


    @DynamoDBHashKey(attributeName = "PK")
    public String getPK() {
        return "MATCHUP#"+ matchupId;
    }

    public void setPK(String pk) {
        //do nothing
    }

}

我注意到除了BatchDelete之外,DynamoDBMapper工作的所有其他方法。它具有以下方法签名

List<FailedBatch> batchDelete(Iterable<? extends Object> var1);

Hello I am using the following dependency for reading/writing to DynamoDB in spring boot. Internally this api calls DynamoDBMapper for performing any of its operations. DynamoDBMapper is apart of the AWS SDK. I BELIEVE THERE IS AN ISSUE WITH THE DYNAMODBMAPPER BATCH DELETE CALL IN AWS SDK

<dependency>
            <groupId>com.github.derjust</groupId>
            <artifactId>spring-data-dynamodb</artifactId>
            <version>5.1.0</version>
        </dependency>

However, when I invoke the DynamoDBMapper batchDelete method I get the following error

java.lang.reflect.InaccessibleObjectException: Unable to make final void java.lang.Throwable.setCause(java.lang.Throwable) accessible: module java.base does not "opens java.lang" to unnamed module @1b083826

Here is what the invoking code looks like

 List<Matchup> matchups = (List<Matchup>) matchupRepository.findAll();

        DynamoDBMapper dynamoDBMapper = new DynamoDBMapper(amazonDynamoDBClient);
        try {
            dynamoDBMapper.batchDelete(matchups);
        }

        catch(Exception e) {
            log.error(e.toString());
        }

The entity class looks like so

@Data
@NoArgsConstructor
@AllArgsConstructor
@DynamoDBTable(tableName = "Sportsbook")
public class Matchup {

    @DynamoDBAttribute(attributeName = "MatchupId")
    private String matchupId;

    @DynamoDBAttribute(attributeName = "SportsKey")
    private String sportsKey;

    @DynamoDBAttribute(attributeName = "CommenceTime")
    private String commenceTime;

    @DynamoDBAttribute(attributeName = "HomeTeam")
    private String homeTeam;

    @DynamoDBAttribute(attributeName = "AwayTeam")
    private String awayTeam;


    @DynamoDBHashKey(attributeName = "PK")
    public String getPK() {
        return "MATCHUP#"+ matchupId;
    }

    public void setPK(String pk) {
        //do nothing
    }

}

I noticed all other methods of DynamoDBMapper work except batchDelete. It has the following method signature

List<FailedBatch> batchDelete(Iterable<? extends Object> var1);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

亢潮 2025-02-11 07:14:19

您没有使用最新的AWS Java API执行此任务。 dynamodbmapper 是Java V1的 AWS SDK的一部分,不再被视为最佳实践 。这是DynamoDBMAPPER,它是V1 Dev指南的一部分=“ nofollow noreferrer”> https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/examples-dynamodb-items.htmls.html

Amazon States:

Java 2.x的AWS SDK是版本1.x代码库的主要重写。它建在Java 8+之上,并添加了几个经常要求的功能。其中包括对非阻滞I/O的支持以及在运行时插入其他HTTP实现的能力。

执行此用例如 aws java v2 dev指南

https://docs.aws.aws.amazon.com/sdk-for-java/latest/latest/deeverice-guide-guide/dynamodb-enhanced-client .html

这是一个代码示例,使用增强的客户端显示H OW ow 。首先 - 假设您有一个带有此数据的客户表,并且要删除批处理记录。

这是增强端客户端的完整代码示例。

package com.example.dynamodb;

// snippet-start:[dynamodb.java2.mapping.batchdelete.import]

import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable;
import software.amazon.awssdk.enhanced.dynamodb.TableSchema;
import software.amazon.awssdk.enhanced.dynamodb.model.BatchWriteItemEnhancedRequest;
import software.amazon.awssdk.enhanced.dynamodb.model.DeleteItemEnhancedRequest;
import software.amazon.awssdk.enhanced.dynamodb.model.WriteBatch;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.DynamoDbException;
import software.amazon.awssdk.enhanced.dynamodb.Key;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
// snippet-end:[dynamodb.java2.mapping.batchdelete.import]

/*
 * Before running this code example, create an Amazon DynamoDB table named Customer with these columns:
 *   - id - the id of the record that is the key
 *   - custName - the customer name
 *   - email - the email value
 *   - registrationDate - an instant value when the item was added to the table
 *
 * Also, ensure that you have set up your development environment, including your credentials.
 *
 * For information, see this documentation topic:
 *
 * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
 */
public class EnhancedBatchDeleteItems {

    public static void main(String[] args) {

        ProfileCredentialsProvider credentialsProvider = ProfileCredentialsProvider.create();
        Region region = Region.US_EAST_1;
        DynamoDbClient ddb = DynamoDbClient.builder()
                .region(region)
                .credentialsProvider(credentialsProvider)
                .build();

        DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder()
                .dynamoDbClient(ddb)
                .build();

        deleteBatchRecords(enhancedClient);
        ddb.close();
    }

    // snippet-start:[dynamodb.java2.mapping.batchdelete.main]
    public static void deleteBatchRecords(DynamoDbEnhancedClient enhancedClient) {

        try {
            DynamoDbTable<Customer> mappedTable = enhancedClient.table("Customer", TableSchema.fromBean(Customer.class));
            Key key1 = Key.builder()
                    .partitionValue("id110")
                    .build();

            Key key2 = Key.builder()
                    .partitionValue("id120")
                    .build();

            BatchWriteItemEnhancedRequest request =  BatchWriteItemEnhancedRequest.builder()
                    .writeBatches(WriteBatch.builder(Customer.class)
                                    .mappedTableResource(mappedTable)
                                    .addDeleteItem(DeleteItemEnhancedRequest.builder()
                                            .key(key1)
                                            .build())
                                    .build(),
                            WriteBatch.builder(Customer.class)
                                    .mappedTableResource(mappedTable)
                                    .addDeleteItem(DeleteItemEnhancedRequest.builder()
                                            .key(key2)
                                            .build())
                                    .build())
                    .build();

            // Delete these two items from the table.
            enhancedClient.batchWriteItem(request);
            System.out.println("Records deleted");

        } catch (DynamoDbException e) {
            System.err.println(e.getMessage());
            System.exit(1);
        }
    }
    // snippet-end:[dynamodb.java2.mapping.batchdelete.main]
}

More information here:

https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/enhanced/dynamodb/DynamoDbEnhancedClient.html#batchWriteItem-software.amazon.awssdk 。

​=“ https://i.sstatic.net/sc7yr.png” alt =“在此处输入图像说明”>

最后,您可以找到 customer class class,pom file等在

You are NOT using the most recent AWS Java API to perform this task. The DynamoDBMapper is part of AWS SDK for Java V1 and no longer considered best practice. Here is the DynamoDBMapper that is part of the V1 DEV Guide https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/examples-dynamodb-items.html

Amazon States:

The AWS SDK for Java 2.x is a major rewrite of the version 1.x code base. It’s built on top of Java 8+ and adds several frequently requested features. These include support for non-blocking I/O and the ability to plug in a different HTTP implementation at run time.

To perform this use case, upgrade from AWS SDK for Java V1 to V2 and use and the Enhanced Client as explained in the AWS Java V2 DEV Guide:

https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/dynamodb-enhanced-client.html

Here is a code example that shows HOW TO Delete batch items using the Enhanced Client. First - assume you have a Customer table with this data and you want to delete batch records.

enter image description here

Here is the full code example with the Enhanced Client.

package com.example.dynamodb;

// snippet-start:[dynamodb.java2.mapping.batchdelete.import]

import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable;
import software.amazon.awssdk.enhanced.dynamodb.TableSchema;
import software.amazon.awssdk.enhanced.dynamodb.model.BatchWriteItemEnhancedRequest;
import software.amazon.awssdk.enhanced.dynamodb.model.DeleteItemEnhancedRequest;
import software.amazon.awssdk.enhanced.dynamodb.model.WriteBatch;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.DynamoDbException;
import software.amazon.awssdk.enhanced.dynamodb.Key;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
// snippet-end:[dynamodb.java2.mapping.batchdelete.import]

/*
 * Before running this code example, create an Amazon DynamoDB table named Customer with these columns:
 *   - id - the id of the record that is the key
 *   - custName - the customer name
 *   - email - the email value
 *   - registrationDate - an instant value when the item was added to the table
 *
 * Also, ensure that you have set up your development environment, including your credentials.
 *
 * For information, see this documentation topic:
 *
 * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
 */
public class EnhancedBatchDeleteItems {

    public static void main(String[] args) {

        ProfileCredentialsProvider credentialsProvider = ProfileCredentialsProvider.create();
        Region region = Region.US_EAST_1;
        DynamoDbClient ddb = DynamoDbClient.builder()
                .region(region)
                .credentialsProvider(credentialsProvider)
                .build();

        DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder()
                .dynamoDbClient(ddb)
                .build();

        deleteBatchRecords(enhancedClient);
        ddb.close();
    }

    // snippet-start:[dynamodb.java2.mapping.batchdelete.main]
    public static void deleteBatchRecords(DynamoDbEnhancedClient enhancedClient) {

        try {
            DynamoDbTable<Customer> mappedTable = enhancedClient.table("Customer", TableSchema.fromBean(Customer.class));
            Key key1 = Key.builder()
                    .partitionValue("id110")
                    .build();

            Key key2 = Key.builder()
                    .partitionValue("id120")
                    .build();

            BatchWriteItemEnhancedRequest request =  BatchWriteItemEnhancedRequest.builder()
                    .writeBatches(WriteBatch.builder(Customer.class)
                                    .mappedTableResource(mappedTable)
                                    .addDeleteItem(DeleteItemEnhancedRequest.builder()
                                            .key(key1)
                                            .build())
                                    .build(),
                            WriteBatch.builder(Customer.class)
                                    .mappedTableResource(mappedTable)
                                    .addDeleteItem(DeleteItemEnhancedRequest.builder()
                                            .key(key2)
                                            .build())
                                    .build())
                    .build();

            // Delete these two items from the table.
            enhancedClient.batchWriteItem(request);
            System.out.println("Records deleted");

        } catch (DynamoDbException e) {
            System.err.println(e.getMessage());
            System.exit(1);
        }
    }
    // snippet-end:[dynamodb.java2.mapping.batchdelete.main]
}

More information here:

https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/enhanced/dynamodb/DynamoDbEnhancedClient.html#batchWriteItem-software.amazon.awssdk.enhanced.dynamodb.model.BatchWriteItemEnhancedRequest-

Now we are left with these items:

enter image description here

Finally, you can find the Customer class, the POM file, etc in AWS Java V2 Github here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文