无需JDBC驱动查询Amazon Redshift的技术方法咨询
Can I Execute SQL Queries with the AmazonRedshift Client?
Hey there! Let's break this down clearly for you:
- Short answer: No, the
AmazonRedshiftclient you’ve built is designed for Redshift cluster management operations only—it cannot run SQL queries directly. TheDescribeEventsRequestyou found is part of the management API, meant to fetch cluster-related events (like maintenance alerts or configuration changes) and has nothing to do with executing SQL against your Redshift database.
What's the Alternative (No JDBC Required)?
If you want to run SQL without relying on the JDBC driver, the right tool for the job is the Redshift Data API. This API is purpose-built for executing SQL statements against Redshift clusters, and it works entirely through AWS SDK calls (no JDBC setup needed).
Here’s a quick example using your existing AWS credentials:
// Initialize the Redshift Data client RedshiftDataClient redshiftDataClient = RedshiftDataClient.builder() .credentialsProvider(awsCredentials) .build(); // Define your target SQL query String sqlQuery = "SELECT * FROM your_target_table LIMIT 10"; // Create a request to execute the query ExecuteStatementRequest executeRequest = ExecuteStatementRequest.builder() .clusterIdentifier("your-redshift-cluster-id") .database("your-database-name") .dbUser("your-db-username") .sql(sqlQuery) .build(); // Execute the query and get the statement ID ExecuteStatementResponse executeResponse = redshiftDataClient.executeStatement(executeRequest); String statementId = executeResponse.id(); // Wait for the query to complete and fetch results GetStatementResultRequest resultRequest = GetStatementResultRequest.builder() .id(statementId) .build(); GetStatementResultResponse resultResponse = redshiftDataClient.getStatementResult(resultRequest); // Process the returned records for (List<Field> row : resultResponse.records()) { row.forEach(field -> System.out.println(field.stringValue())); }
Key Notes:
- The
RedshiftDataClientis part of the AWS SDK for Java v2—make sure you have the correct dependencies added to your project. - Your IAM credentials will need permissions for Redshift Data API actions, like
redshift-data:ExecuteStatementandredshift-data:GetStatementResult.
内容的提问来源于stack exchange,提问作者Siva
相关产品推荐
相关产品推荐

