I tried the input / output type of Java Lambda ~ Map edition ~

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

I tried the input / output type of Java Lambda ~ Map edition ~
I tried Java Lambda input / output type ~ POJO edition ~
I tried Java Lambda input / output type ~ Stream version ~
[Java] Be careful of the key type of Map
I tried to summarize the basics of kotlin and java
I tried the Java framework "Quarkus"
The origin of Java lambda expressions
I tried using GoogleHttpClient of Java
I tried to summarize the methods of Java String and StringBuilder
I tried to summarize Java lambda expressions
[day: 5] I summarized the basics of Java
Output of the book "Introduction to Java"
I tried to visualize the access of Lambda → Athena with AWS X-Ray
I want to output the day of the week
I compared the characteristics of Java and .NET
I tried to output multiplication table in Java
I touched on the new features of Java 15
I tried using the profiler of IntelliJ IDEA
Try the free version of Progate [Java I]
I can't remember the text file input / output in Java, so I summarized it.
I tried using the Server Push function of Servlet 4.0
I tried to summarize the state transition of docker
05. I tried to stub the source of Spring Boot
Use Java lambda expressions outside of the Stream API
I tried to reduce the capacity of Spring Boot
I tried the new feature profiler of IntelliJ IDEA 2019.2.
I summarized the types and basics of Java exceptions
[WIP] I tried the configuration of Docker + Streama + NFS
paiza skill check standard input / output summary [Java edition]
I tried to implement the Euclidean algorithm in Java
I finished watching The Rose of Versailles, so I tried to reproduce the ending song in Java
A Java user over a dozen years ago tried to study the functions of Java8 (Lambda expression).
I want to return a type different from the input element with Java8 StreamAPI reduce ()
[Note] Java Output of the sum of odd and even elements
I didn't understand the behavior of Java Scanner and .nextLine ().
[JDBC] I tried to access the SQLite3 database from Java.
Object-oriented child !? I tried Deep Learning in Java (trial edition)
I want to change the log output settings of UtilLoggingJdbcLogger
[Swift] I tried to implement the function of the vending machine
I tried JAX-RS and made a note of the procedure
[Java] To know the type information of type parameters at runtime
I tried to summarize the basic grammar of Ruby briefly
I tried to convert a string to a LocalDate type in Java
I tried to build the environment of WSL2 + Docker + VSCode
I tried to make a client of RESAS-API in Java
I tried using the CameraX library with Android Java Fragment
[Java] I want to test standard input & standard output with JUnit
[Java] I thought about the merits and uses of "interface"
I tried Drools (Java, InputStream)
I tried the Docker tutorial!
I tried the VueJS tutorial!
I tried using Java REPL
I tried the FizzBuzz problem
[Java] I implemented the combination.
I studied the constructor (java)
I tried metaprogramming in Java
Input to the Java console
[Java] I felt as a java beginner. "Why is the first letter of the String type capitalized?" I faced the String type.
I tried to solve the problem of "multi-stage selection" with Ruby
Examine the system information of AWS Lambda operating environment in Java
I tried to summarize what was asked at the site-java edition-