You can use various patterns for Java Lambda input / output types.
In the official documentation, it is written here [https://docs.aws.amazon.com/ja_jp/lambda/latest/dg/java-handler.html#java-handler-types).
First of all, from the Map method.
This is the input JSON to pass to the Lambda function.
{
"name": "orange juice",
"price": 1000,
"releaseDate": 1601606387939,
"rate": 4.5,
"rawMaterial": ["orange", "fragrance"],
"size": {
"height": 10,
"width": 20,
"depth": 30
}
}
The code below treats this input JSON as a Map, writes it to standard output, and also uses Map to output another JSON.
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MapFunction implements RequestHandler<Map<String, Object>, Map<String, Object>> {
@Override
public Map<String, Object> handleRequest(Map<String, Object> event, Context context) {
String name = (String) event.get("name");
System.out.println(name); // orange juice
Integer price = (Integer) event.get("price");
System.out.println(price); // 1000
Long releaseDate = (Long) event.get("releaseDate");
System.out.println(releaseDate); // 1601606387939
Double rate = (Double) event.get("rate");
System.out.println(rate); // 4.5
List<String> rawMaterial = (List<String>) event.get("rawMaterial");
System.out.println(rawMaterial); // [orange, fragrance]
Map<String, Integer> size = (Map<String, Integer>) event.get("size");
Integer height = size.get("height");
System.out.println(height); // 10
Integer width = size.get("width");
System.out.println(width); // 20
Integer depth = size.get("depth");
System.out.println(depth); // 30
Map<String, Object> response = new HashMap<>();
response.put("name", "coffee");
response.put("price", Integer.valueOf(500));
response.put("releaseDate", System.currentTimeMillis());
response.put("rate", Double.valueOf(4.2));
response.put("rawMaterial", Arrays.asList("coffee", "sugar"));
Map<String, Integer> responseSize = new HashMap<>();
responseSize.put("height", Integer.valueOf(100));
responseSize.put("width", Integer.valueOf(200));
responseSize.put("depth", Integer.valueOf(300));
response.put("size", responseSize);
return response;
}
}
Each value is converted to String, Integer, Long, Double according to the JSON type. Arrays are converted to Lists and associative arrays to Maps.
When I execute this Lambda function, this JSON is output.
{
"size": {
"depth": 300,
"width": 200,
"height": 100
},
"releaseDate": 1601615397265,
"rate": 4.2,
"price": 500,
"rawMaterial": [
"coffee",
"sugar"
],
"name": "coffee"
}
Since I'm using HashMap, the order is out of order. If you use a TreeMap instead of a HashMap, for example, the order will be maintained. However, it is desirable to have an implementation that can be interpreted independently of the order by the recipient of this JSON.
As you can see, Java Lambda allows you to work with JSON by using the Map type. You can write code without adding more classes, but you'll end up with a cast storm and it's not type-safe. In the next article, I will introduce a POJO method that can solve this problem.
I tried Java Lambda input / output type ~ POJO edition ~ https://qiita.com/kazfuku/items/095b4fb9c37638f57457
Recommended Posts