简体   繁体   English

开发AWS Lambda函数时无法理解要使用哪个Java SDK

[英]Unable to understand which Java SDK to use when developing AWS Lambda Function

When I was following the tutorial on deploying an AWS Lambda Function, I saw in it's sample that it uses an AWS Java SDK containing the interface Speechlet. 当我按照有关部署AWS Lambda函数的教程进行操作时,在示例中看到它使用了包含接口Speechlet的AWS Java SDK。

Here's a screenshot of the sample: 这是示例的屏幕截图:

在此处输入图片说明

And the official documentation also states that I should use the Speechlet interface here: https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/handling-requests-sent-by-alexa 官方文档还指出,我应该在此处使用Speechlet界面: https : //developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/handling-requests-sent-by-alexa

But unfortunately, when I installed the AWS Toolkit for Eclipse, there is no such interface in its toolkit. 但是不幸的是,当我安装了适用于Eclipse的AWS Toolkit时,其工具包中没有这样的界面。 I also downloaded the AWS Java SDK from its official site but no Speechlet interface could be found. 我还从其官方站点下载了AWS Java SDK,但是找不到Speechlet界面。

And so, I searched again for some tutorials on how to create a Lambda Function using java then I came up with this: http://docs.aws.amazon.com/toolkit-for-eclipse/v1/user-guide/lambda-tutorial.html 因此,我再次搜索了一些有关如何使用java创建Lambda函数的教程,然后我想到了这一点: http : //docs.aws.amazon.com/toolkit-for-eclipse/v1/user-guide/lambda -tutorial.html

This tutorial uses the Java SDK with no Speechlet interface! 本教程使用没有Speechlet接口的Java SDK! This really confuses me, because in its other documentation it states there clearly that it has a Speechlet interface and now it doesn't. 这确实让我感到困惑,因为在其他文档中,它清楚地指出它具有Speechlet接口,而现在却没有。 I don't know how to continue this. 我不知道该如何继续。

The minimum functionality your AWS Lambda function needs to support in Java is to have a class that implements a method with the following signature: AWS Lambda函数需要在Java中支持的最低功能是拥有一个类,该类实现具有以下签名的方法:

outputType handler-name(inputType input) {
   ...
}

If your signature looks something like this, no third-party libraries are necessary. 如果您的签名看起来像这样,则无需第三方库。 You could have a Lambda handler that looks like this and it would work fine. 您可能会有一个看起来像这样的Lambda处理程序,它将可以正常工作。

package LambdaExample;

public class Hello {
    public String lambdaHandler(String name) {
        return String.format("Hello %s!", name);
    }
}

In non-trivial usage, you'll likely see handler signatures that look something more like this: 在非平凡的用法中,您可能会看到处理程序签名,看起来更像这样:

outputType handler-name(inputType input, Context context) {
   ...
}

The Context object can be used to retrieve information about your Lambda handler, as well as a logger you can use to log information about your handler to Cloudwatch. Context对象可用于检索有关Lambda处理程序的信息,以及可用于将有关处理程序的信息记录到Cloudwatch的记录器。

The Context type is defined in com.amazonaws.services.lambda.runtime.Context , which is provided by aws-lambda-java-core , part of a set of libraries that AWS publish for working with Lambda using Java/JVM languages. Context类型在com.amazonaws.services.lambda.runtime.Context定义,该com.amazonaws.services.lambda.runtime.Contextaws-lambda-java-core ,AWS aws-lambda-java-core是AWS发布的一组库的一部分,以使用Java / JVM语言与Lambda一起使用。 aws-lambda-java-core is separate from the AWS SDK for Java. aws-lambda-java-core与适用于Java的AWS开发工具包分开。

What you're running into seems to be a case of using very specific resources. 您遇到的问题似乎是使用非常具体的资源的情况。 Your image shows code for building a custom Alexa skill and deploying it to Lambda. 您的图像显示了用于构建自定义Alexa技能并将其部署到Lambda的代码。 If you are building an Alexa skill, then you will need to also include the alexa-skills-kit SDK , which is what provides the Speechlet interface you see. 如果您要构建Alexa技能,则还需要包括alexa-skills-kit SDK ,该alexa-skills-kit SDK提供了您所看到的Speechlet界面。 The Alexa Skills Kit SDK is also separate from the AWS SDK for Java. Alexa Skills Kit SDK也与适用于Java的AWS开发工具包分开。

Assuming your project uses Maven for project management/dependencies, adding the following to the <dependencies> section should enable you to work with Lambda and Alexa: 假设您的项目使用Maven进行项目管理/依赖关系,则在<dependencies>部分中添加以下内容将使您能够使用Lambda和Alexa:

<dependency>
  <groupId>com.amazon.alexa</groupId>
  <artifactId>alexa-skills-kit</artifactId>
  <version>1.2</version>
  <scope>compile</scope>
</dependency>
<dependency>
  <groupId>com.amazonaws</groupId>
  <artifactId>aws-lambda-java-core</artifactId>
  <version>1.1.0</version>
</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-log4j</artifactId>
  <version>1.0.0</version>
</dependency>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM