【问题标题】:Cannot insert into DynamoDB table from Lambda Function无法从 Lambda 函数插入 DynamoDB 表
【发布时间】:2020-10-13 14:55:30
【问题描述】:

我在运行我的 lambda 函数时遇到了以下错误,该函数应该将一个项目放入我的 DynamoDB 表中。

      "errorMessage": "com/amazonaws/services/dynamodbv2/AmazonDynamoDBClientBuilder",
      "errorType": "java.lang.NoClassDefFoundError",
      "stackTrace": [
        "dao.EventDAO.insertEvent(EventDAO.java:15)",
        "service.PostEventService.postEvent(PostEventService.java:12)",
        "handler.PostEventHandler.handle(PostEventHandler.java:11)",
        "java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)",
        "java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)",
        "java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)",
        "java.base/java.lang.reflect.Method.invoke(Unknown Source)"
      ]

这是我试图插入表格的代码:

    package dao;
    
    import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
    import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
    import com.amazonaws.services.dynamodbv2.document.DynamoDB;
    import com.amazonaws.services.dynamodbv2.document.Item;
    import com.amazonaws.services.dynamodbv2.document.PutItemOutcome;
    import com.amazonaws.services.dynamodbv2.document.Table;
    import request.PostEventRequest;
    import response.PostEventResponse;
    
    public class EventDAO {
    
        public PostEventResponse insertEvent(PostEventRequest request) {
            AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build();
            DynamoDB dynamoDB = new DynamoDB(client);
            Table table = dynamoDB.getTable("Event");
            PostEventResponse response = new PostEventResponse();
    
            // Build the item
            Item item = new Item()
                    .withPrimaryKey("eventId", request.eventId)
                    .withString("username", request.username)
                    .withString("image", request.image)
                    .withString("description", request.description)
                    .withString("time", request.time);
    
            // Write the item to the table
            PutItemOutcome outcome = table.putItem(item);
    
            return response;
        }
    }

这是我的 pom.xml,因为我怀疑这可能很重要:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <dependencies>
            <dependency>
                <groupId>com.amazonaws</groupId>
                <artifactId>aws-java-sdk</artifactId>
                <version>1.11.327</version>
            </dependency>
            <dependency>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
            </dependency>
        </dependencies>
    
        <properties>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
        </properties>
    
        <groupId>earnshawsfinest</groupId>
        <artifactId>rally-backend</artifactId>
        <version>1.0-SNAPSHOT</version>
    </project>

由于某种原因,当它在 lambda 中时,它无法识别 AmazonDynamoDbClientBuilder 类,即使在我的 IntelliJ 项目中它在那里并且很重要。我的 IntelliJ 项目构建没有问题,它导出了一个我上传到 lambda 的 jar(带有模块和依赖项)。如果我取出 DynamoDB 的东西,lambda 会正常工作。

我觉得我可能在某处缺少一些下载或导入,但我不知道是什么。我已经按照亚马逊的各种教程进行操作,我什至以前也这样做过,但我没有遇到这个问题。有什么帮助吗?提前致谢!!

【问题讨论】:

    标签: amazon-web-services aws-lambda amazon-dynamodb noclassdeffounderror


    【解决方案1】:

    默认情况下,依赖项不捆绑在 jar 文件中。您需要这样做或在单独的 lambda 层中提供它们

    检查链接Building a deployment package with Maven 并查看使用maven-shade-plugin,它应该在目标jar 文件中构建所有依赖项。

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.2.2</version>
        <configuration>
          <createDependencyReducedPom>false</createDependencyReducedPom>
        </configuration>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-19
      • 1970-01-01
      • 1970-01-01
      • 2016-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多