Skip to content

Instantly share code, notes, and snippets.

@sualeh
Created April 22, 2023 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sualeh/3aed62b41ec6da460b5af268be031ee2 to your computer and use it in GitHub Desktop.
Save sualeh/3aed62b41ec6da460b5af268be031ee2 to your computer and use it in GitHub Desktop.
SchemaCrawler example code for AWS Athena
package com.example;
import java.sql.Connection;
import java.util.logging.Level;
import schemacrawler.crawl.ConnectionInfoBuilder;
import schemacrawler.schema.Catalog;
import schemacrawler.schema.Column;
import schemacrawler.schema.Schema;
import schemacrawler.schema.Table;
import schemacrawler.schemacrawler.LimitOptionsBuilder;
import schemacrawler.schemacrawler.LoadOptionsBuilder;
import schemacrawler.schemacrawler.SchemaCrawlerOptions;
import schemacrawler.schemacrawler.SchemaCrawlerOptionsBuilder;
import schemacrawler.schemacrawler.SchemaInfoLevelBuilder;
import schemacrawler.tools.utility.SchemaCrawlerUtility;
import us.fatehi.utility.LoggingConfig;
import us.fatehi.utility.datasource.DatabaseConnectionSource;
import us.fatehi.utility.datasource.DatabaseConnectionSources;
import us.fatehi.utility.datasource.MultiUseUserCredentials;
public final class ApiExampleAthena {
private static final String CONNECTION_URL =
"jdbc:awsathena://AwsRegion=us-east-1;"
+ "User=AKIA2W3DBUSERNAMEF67;"
+ "Password=NXAnAMyPassword+NoyQSjR2/AEpwdNFpwdv81;"
+ "S3OutputLocation=s3://my-athena-bucket/;";
public static void main(final String[] args) throws Exception {
// Set log level
new LoggingConfig(Level.OFF);
// Create the options
final LimitOptionsBuilder limitOptionsBuilder =
LimitOptionsBuilder.builder()
.tableTypes(
"EXTERNAL", "EXTERNAL_TABLE", "MANAGED_TABLE", "TABLE", "VIEW", "VIRTUAL_VIEW");
final LoadOptionsBuilder loadOptionsBuilder =
LoadOptionsBuilder.builder()
// Set what details are required in the schema - this affects the
// time taken to crawl the schema
.withSchemaInfoLevel(SchemaInfoLevelBuilder.standard());
final SchemaCrawlerOptions options =
SchemaCrawlerOptionsBuilder.newSchemaCrawlerOptions()
.withLimitOptions(limitOptionsBuilder.toOptions())
.withLoadOptions(loadOptionsBuilder.toOptions());
// Get the schema definition
final DatabaseConnectionSource dataSource = getDataSource();
final Catalog catalog = SchemaCrawlerUtility.getCatalog(dataSource, options);
try (Connection connection = dataSource.get()) {
final ConnectionInfoBuilder connectionInfoBuilder = ConnectionInfoBuilder.builder(connection);
System.out.println(connectionInfoBuilder.buildDatabaseInfo());
System.out.println(connectionInfoBuilder.buildJdbcDriverInfo());
}
for (final Schema schema : catalog.getSchemas()) {
System.out.printf("%n%s%n", schema);
for (final Table table : catalog.getTables(schema)) {
System.out.printf("o--> %s (%s)%n", table.getName(), table.getType());
for (final Column column : table.getColumns()) {
System.out.printf(" o--> %s (%s)%n", column.getName(), column.getType());
}
}
}
}
private static DatabaseConnectionSource getDataSource() {
final String connectionUrl = CONNECTION_URL;
return DatabaseConnectionSources.newDatabaseConnectionSource(
connectionUrl, new MultiUseUserCredentials());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment