在dynamoDB更新上调用lambda函数时遇到问题。 我检查过 AWS Lambda: ClassNotFoundException 和 AWS Lambda NoClassDefFoundError 但没有成功。
我正在编写这个lambda函数,以便在dynamo中有任何更新时调用。我按照本教程。 https://docs.aws.amazon.com/lambda/latest/dg/with-dynamodb-create-package.html
所有事情都已成功完成,但问题是当我更新dynamodb并检查AWS Cloud的日志时,有一个例外。
Error loading method handleRequest on class com.amazonaws.lambda.demo.LambdaFunctionHandler: java.lang.NoClassDefFoundError
java.lang.NoClassDefFoundError: com/amazonaws/services/lambda/runtime/events/DynamodbEvent
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetPublicMethods(Class.java:2902)
at java.lang.Class.getMethods(Class.java:1615)
Caused by: java.lang.ClassNotFoundException: com.amazonaws.services.lambda.runtime.events.DynamodbEvent
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 4 more
我的java代码是。
package com.amazonaws.lambda.demo;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.DynamodbEvent;
import com.amazonaws.services.lambda.runtime.events.DynamodbEvent.DynamodbStreamRecord;
public class LambdaFunctionHandler implements RequestHandler<DynamodbEvent, String> {
@Override
public String handleRequest(DynamodbEvent ddbEvent, Context context) {
// TODO Auto-generated method stub
for (DynamodbStreamRecord record : ddbEvent.getRecords()){
System.out.println(record.getEventID());
System.out.println(record.getEventName());
System.out.println(record.getDynamodb().toString());
}
return "Successfully processed " + ddbEvent.getRecords().size() + " records.";
}
}
我的pom.xml是
<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>
<groupId>com.amazonaws.lambda</groupId>
<artifactId>demo</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<forceJavacCompilerUse>true</forceJavacCompilerUse>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-bom</artifactId>
<version>1.11.321</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-events</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
</project>
我将我的处理程序命名为com.amazonaws.lambda.demo.LambdaFunctionHandler::handleRequest
我已经检查过所有的事情都很好,但它仍然在com.amazonaws.services.lambda.runtime.events.DynamodbEvent
这个类在aws sdk中给我错误。
答案 0 :(得分:0)
您必须使用以下命令通过Maven构建项目:mvn clean package
。然后转到target
目录,在那里可以找到构建的jar文件。
当您将jar文件上传到AWS Lambda时,您必须上传包含所有依赖项的jar文件(在您的示例中为aws-lambda-java-events
)。请参阅屏幕上的示例:
在您的情况下,您必须上传demo-1.0.0.jar
而不是original-demo-1.0.0.jar
。
答案 1 :(得分:0)
如果一切正常,即使是包名和生成的 JAR 以及所有内容,但仍然有人面临错误,那么您可以将以下配置添加到 maven-plugin..
我使用的是带有以下配置的 spring-boot-maven-plugin 插件,而不是 maven-shade-plugin。
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot.experimental</groupId>
<artifactId>spring-boot-thin-layout</artifactId>
<version>1.0.26.RELEASE</version>
</dependency>
</dependencies>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>aws</shadedClassifierName>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer">
<resource>META-INF/spring.factories</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
</transformers>
</configuration>
然后你就可以运行
./mvnw clean package
或者,如果您使用的是 STS/Eclipse,则运行 Maven build.. with目标 clean package
这将在您的目标文件夹中生成一个以 KB 为单位的 jar 文件,您可以使用此 JAR 在 AWS lambda 代码上进行部署。
附注: