若要通过 Java JDBC 将表格创建到 Amazon Athena,请使用 DDL 查询语句。以下是创建表格并将其与 Amazon S3 存储桶中的数据源连接的样例代码:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.SQLException;
public class AthenaJDBCEngineDemo {
private static final String athenaUrl = "jdbc:awsathena://AwsRegion=us-west-2";
private static final String s3StagingDir = "s3://your-bucket/path/";
private static final String awsAccessKeyId = "your-aws-access-key-id";
private static final String awsSecretAccessKey = "your-aws-secret-access-key";
public static void main(String[] args) {
// Register the driver using the class name.
// The class name is com.amazonaws.athena.jdbc.AthenaDriver
try {
Class.forName("com.amazonaws.athena.jdbc.AthenaDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.exit(-1);
}
// Establish the connection.
Connection conn = null;
try {
conn = DriverManager.getConnection(athenaUrl,
awsAccessKeyId, awsSecretAccessKey);
} catch (SQLException e) {
e.printStackTrace();
System.exit(-1);
}
// Try to create a statement.
Statement stmt = null;
String createTableDdl = "CREATE EXTERNAL TABLE IF NOT EXISTS " +
"sampledb.elb_logs_athena (" +
" request_timestamp string," +
" elb_name string," +
" request_ip string," +
" request_port int," +
" backend_ip string," +
" backend_port int," +
" request_processing_time double," +
" backend_processing_time double," +
" client_response_time double," +
" elb_response_code string," +
" backend_response_code string," +
" received_bytes bigint," +
" sent_bytes bigint," +
" request_verb string," +
" url string," +
" protocol string," +
" user_agent string,"