如何定义AWS Lambda的函数以Java启动?

时间:2017-07-27 18:24:11

标签: java amazon-web-services aws-lambda

我使用此文档作为教程 http://docs.aws.amazon.com/lambda/latest/dg/get-started-step4-optional.html

他们为AWS lamda提供的输入方法/功能如下所示:

public String myHandler(int myCount, Context context) {
    LambdaLogger logger = context.getLogger();
    logger.log("received : " + myCount);
    return String.valueOf(myCount);
}

我的问题是我不知道我可以定义什么参数以及AWS lambda如何知道要传递给我的信息。很高兴看到所有潜在的方法签名我能想出来。也许我只是没有找到适合这个文档的地方,但我很乐意做一些像这样的事情

public String myHandler(String json) {
   MyJsonValidatorClass validator = new ...;
boolean isValid = validator.isValidJson(json);
    return String.valueOf(isValid);
}

我真的不确定我能在AWS Lamdas做些什么。在编写java主应用程序时,我知道我有String [] args来处理而没有别的。我在这里错过了什么吗?或者我只是想到这完全错了?

1 个答案:

答案 0 :(得分:3)

lambda运行时使用反射来查看方法所需的类型作为第一个参数,然后尝试根据该规范解析原始输入数据。它支持的类型列在here

  
      
  • 简单Java类型(AWS Lambda支持String,Integer,Boolean,Map和List类型)
  •   
  • POJO(Plain Old Java Object)类型
  •   
  • 流类型(如果您不想使用POJO或Lambda的序列化方法不能满足您的需求,您可以使用字节流实现。[..])
  •   

处理程序方法的示例是

import random

def array_to_int(values, bitwidth):
    mask = 2**bitwidth - 1
    shift = bitwidth * (len(values)-1)
    integer = 0
    for value in values:
        integer |= (value & mask) << shift
        shift -= bitwidth
    return integer

# In Python 2.7 int and long don't have the "to_bytes" method found in Python 3.x,
# so here's one way to do the same thing.
def to_bytes(n, length):
    return ('%%0%dx' % (length << 1) % n).decode('hex')[-length:]

BITWIDTH = 6
#values = [random.randint(0, 2**BITWIDTH - 1) for _ in range(10)]
values = [0b000001 for _ in range(10)]  # create fixed pattern for debugging
values[9] = 0b011111  # make last one different so it can be spotted

# just for debug/information: bit string representation
bitstring = "".join(map(lambda x: bin(x)[2:].zfill(BITWIDTH), values));
print(bitstring)

bigint = array_to_int(values, BITWIDTH)
width = BITWIDTH * len(values)
print('{:0{width}b}'.format(bigint, width=width))  # show integer's value in binary

num_bytes = (width+8 - (width % 8)) // 8  # round to whole number of 8-bit bytes
with open('data.bin', 'wb') as file:
    file.write(to_bytes(bigint, num_bytes))

为方便起见并帮助您防止错误,有// you could do your own json parsing in here String handler(String input, Context context) // lambda parses json for you JoinResponsePojo handler(JoinRequestPojo request, Context context) // when even String is not enough void handler(InputStream inputStream, OutputStream outputStream, Context context) RequestHandler接口可以捕获上面的方法签名(docs)。我通常使用那些而不是自由式实现处理程序方法。

通常最方便的方法是直接使用POJO,因为通常输入 json。您可以使用aws-lambda-java-events中的常见事件预定义POJO。或者您可以像"Example: Using POJOs for Handler Input/Output (Java)"

中所述编写自己的内容

js回调用于返回数据,因此您的示例是

RequestStreamHandler

或使用像

这样的pojo
public class ExampleHandler1 implements RequestHandler<String, String> {
    @Override
    public String handleRequest(String input, Context context) {
        // would preferably use some other way to generate json
        return "{\"speech\": \"hello theres\"}";
    }
}