Search in sources :

Example 1 with RedshiftDataClient

use of software.amazon.awssdk.services.redshiftdata.RedshiftDataClient in project aws-doc-sdk-examples by awsdocs.

the class RedshiftService method addRecord.

// Add a new record to the Amazon Redshift table.
public String addRecord(String author, String title, String body) {
    try {
        RedshiftDataClient redshiftDataClient = getClient();
        UUID uuid = UUID.randomUUID();
        String id = uuid.toString();
        // Date conversion.
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        String sDate1 = dtf.format(now);
        Date date1 = new SimpleDateFormat("yyyy/MM/dd").parse(sDate1);
        java.sql.Date sqlDate = new java.sql.Date(date1.getTime());
        // Inject an item into the system.
        String sqlStatement = "INSERT INTO blog (idblog, date, title, body, author) VALUES( '" + uuid + "' ,'" + sqlDate + "','" + title + "' , '" + body + "', '" + author + "');";
        ExecuteStatementRequest statementRequest = ExecuteStatementRequest.builder().clusterIdentifier(clusterId).database(database).dbUser(dbUser).sql(sqlStatement).build();
        redshiftDataClient.executeStatement(statementRequest);
        return id;
    } catch (RedshiftDataException | ParseException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    return null;
}
Also used : LocalDateTime(java.time.LocalDateTime) Date(java.util.Date) RedshiftDataClient(software.amazon.awssdk.services.redshiftdata.RedshiftDataClient) ParseException(java.text.ParseException) UUID(java.util.UUID) DateTimeFormatter(java.time.format.DateTimeFormatter) SimpleDateFormat(java.text.SimpleDateFormat)

Example 2 with RedshiftDataClient

use of software.amazon.awssdk.services.redshiftdata.RedshiftDataClient in project aws-doc-sdk-examples by awsdocs.

the class RedshiftService method getClient.

private RedshiftDataClient getClient() {
    Region region = Region.US_WEST_2;
    RedshiftDataClient redshiftDataClient = RedshiftDataClient.builder().credentialsProvider(EnvironmentVariableCredentialsProvider.create()).region(region).build();
    return redshiftDataClient;
}
Also used : RedshiftDataClient(software.amazon.awssdk.services.redshiftdata.RedshiftDataClient) Region(software.amazon.awssdk.regions.Region)

Example 3 with RedshiftDataClient

use of software.amazon.awssdk.services.redshiftdata.RedshiftDataClient in project aws-doc-sdk-examples by awsdocs.

the class InsertData method main.

public static void main(String[] args) {
    try {
        String clusterId = "redshift-cluster-1";
        String database = "dev";
        String dbUser = "awsuser";
        RedshiftDataClient redshiftDataClient = getClient();
        UUID uuid = UUID.randomUUID();
        String id = uuid.toString();
        // Date conversion
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        String sDate1 = dtf.format(now);
        Date date1 = new SimpleDateFormat("yyyy/MM/dd").parse(sDate1);
        java.sql.Date sqlDate = new java.sql.Date(date1.getTime());
        String title = "Aug Weather";
        String body = "Aug is suppose to be mild";
        String author = "user";
        // Inject an item into the system
        String sqlStatement = "INSERT INTO blog (idblog, date, title, body, author) VALUES( '" + uuid + "' ,'" + sqlDate + "','" + title + "' , '" + body + "', '" + author + "');";
        ExecuteStatementRequest statementRequest = ExecuteStatementRequest.builder().clusterIdentifier(clusterId).database(database).dbUser(dbUser).sql(sqlStatement).build();
        ExecuteStatementResponse response = redshiftDataClient.executeStatement(statementRequest);
        System.out.println("The id is " + response.id());
    } catch (RedshiftDataException | ParseException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) Date(java.util.Date) RedshiftDataClient(software.amazon.awssdk.services.redshiftdata.RedshiftDataClient) ParseException(java.text.ParseException) UUID(java.util.UUID) SimpleDateFormat(java.text.SimpleDateFormat) DateTimeFormatter(java.time.format.DateTimeFormatter)

Example 4 with RedshiftDataClient

use of software.amazon.awssdk.services.redshiftdata.RedshiftDataClient in project aws-doc-sdk-examples by awsdocs.

the class RetrieveData method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "    RetrieveData <database> <dbUser> <sqlStatement> <clusterId> \n\n" + "Where:\n" + "    database - the name of the database (for example, dev) \n" + "    dbUser - the master user name \n" + "    sqlStatement - the sql statement to use (for example, select * from information_schema.tables;) \n" + "    clusterId - the id of the Redshift cluster (for example, redshift-cluster) \n";
    if (args.length != 4) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String database = args[0];
    String dbUser = args[1];
    String sqlStatement = args[2];
    String clusterId = args[3];
    Region region = Region.US_WEST_2;
    RedshiftDataClient redshiftDataClient = RedshiftDataClient.builder().region(region).build();
    String id = performSQLStatement(redshiftDataClient, database, dbUser, sqlStatement, clusterId);
    System.out.println("The identifier of the statement is " + id);
    checkStatement(redshiftDataClient, id);
    getResults(redshiftDataClient, id);
    redshiftDataClient.close();
}
Also used : RedshiftDataClient(software.amazon.awssdk.services.redshiftdata.RedshiftDataClient) Region(software.amazon.awssdk.regions.Region)

Example 5 with RedshiftDataClient

use of software.amazon.awssdk.services.redshiftdata.RedshiftDataClient in project aws-doc-sdk-examples by awsdocs.

the class RedshiftService method getPosts.

// Returns a collection that returns 5, 10, or all posts from the Redshift table.
public String getPosts(String lang, int num) {
    try {
        RedshiftDataClient redshiftDataClient = getClient();
        String sqlStatement = "";
        if (num == 5)
            sqlStatement = "SELECT TOP 5 * FROM blog ORDER BY date DESC";
        else if (num == 10)
            sqlStatement = "SELECT TOP 10 * FROM blog ORDER BY date DESC";
        else
            sqlStatement = "SELECT * FROM blog ORDER BY date DESC";
        ExecuteStatementRequest statementRequest = ExecuteStatementRequest.builder().clusterIdentifier(clusterId).database(database).dbUser(dbUser).sql(sqlStatement).build();
        ExecuteStatementResponse response = redshiftDataClient.executeStatement(statementRequest);
        String myId = response.id();
        checkStatement(redshiftDataClient, myId);
        List<Post> posts = getResults(redshiftDataClient, myId, lang);
        return convertToString(toXml(posts));
    } catch (RedshiftDataException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    return "";
}
Also used : RedshiftDataClient(software.amazon.awssdk.services.redshiftdata.RedshiftDataClient)

Aggregations

RedshiftDataClient (software.amazon.awssdk.services.redshiftdata.RedshiftDataClient)7 Region (software.amazon.awssdk.regions.Region)4 ParseException (java.text.ParseException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 LocalDateTime (java.time.LocalDateTime)2 DateTimeFormatter (java.time.format.DateTimeFormatter)2 Date (java.util.Date)2 UUID (java.util.UUID)2