public String handleRequest()

in quick-start/java/RedshiftDataAPI.java [25:86]


    public String handleRequest(Map<String, String> event, Context context) {
        LambdaLogger logger = context.getLogger();
        // Cluster identifier for the Amazon Redshift cluster
        String redshiftClusterId = event.get("redshift_cluster_id");
        // Database name for the Amazon Redshift cluster
        String redshiftDatabaseName = event.get("redshift_database");
        // Database user in the Amazon Redshift cluster with access to execute relevant
        // SQL queries
        String redshiftUser = event.get("redshift_user");
        // IAM Role of Amazon Redshift cluster having access to S3
        String redshiftIamRole = event.get("redshift_iam_role");
        // run_type can be either asynchronous or synchronous; try tweaking based on
        // your requirement
        String run_type = event.get("run_type");

        Map<String, String> sqlStatements = new LinkedHashMap<String, String>();
        Map<String, String> responses = new LinkedHashMap<String, String>();

        logger.log(event.toString() + "\n");

        if (!run_type.equals("synchronous") && !run_type.equals("asynchronous")) {
            logger.log("Invalid Event run_type. \n run_type has to be synchronous or asynchronous.");
            System.exit(1);
        }

        boolean isSynchronous = (run_type.equals("synchronous")) ? true : false;

        // Initiate AWSRedshiftDataAPI client
        AWSRedshiftDataAPI redshiftDataApiClient = AWSRedshiftDataAPIClient.builder().build();

        sqlStatements.put("CREATE", "CREATE TABLE IF NOT EXISTS public.region (\n" + "  R_REGIONKEY bigint NOT NULL,\n"
                + "  R_NAME varchar(25),\n" + "  R_COMMENT varchar(152))\n" + "diststyle all;");

        sqlStatements.put("COPY", "COPY region FROM 's3://redshift-immersionday-labs/data/region/region.tbl.lzo'\n"
                + "iam_role '" + redshiftIamRole + "' \n" + "region 'us-west-2' lzop delimiter '|' COMPUPDATE PRESET;");

        sqlStatements.put("UPDATE", "UPDATE public.region SET r_regionkey= 5 WHERE r_name ='AFRICA';");

        sqlStatements.put("DELETE", "DELETE FROM public.region where r_name = 'MIDDLE EAST';");

        sqlStatements.put("SELECT", "SELECT r_regionkey, r_name FROM public.region;");

        logger.log(String.format("Running sql queries in %s mode. \n", run_type));

        sqlStatements.forEach((command, query) -> {
            try {
                logger.log(String.format("Example of %s mode!\n", command));
                responses.put(command + " STATUS: ", executeSqlDataApi(redshiftDataApiClient, redshiftClusterId,
                        redshiftDatabaseName, redshiftUser, command, query, isSynchronous, logger));

            } catch (InterruptedException e) {

                logger.log(e.getStackTrace().toString() + "\n");
                logger.log(e.getMessage() + "\n");
                logger.log(String.format("%s has failed. \n", command));
                System.exit(1);
            }
        });

        return responses.toString();

    }