feat(glue): serve Glue catalogs and the federated S3 Tables catalog (#8503)

This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-15 17:44:58 -04:00
1 parent 2b372cc0fc
commit 9f7ac3b6a5
12 files changed
+962 -56

No files matched your search

@@ -0,0 +1,481 @@
package io.floci.aws.services.glue;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.floci.aws.core.common.AwsArnUtils;
import io.floci.aws.core.common.AwsException;
import io.floci.aws.core.common.PaginatedResult;
import io.floci.aws.core.common.Pagination;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.core.storage.RegionScopedStorage;
import io.floci.aws.core.storage.StorageBackend;
import io.floci.aws.core.storage.StorageFactory;
import io.floci.aws.services.glue.iceberg.IcebergGlueTables;
import io.floci.aws.services.glue.model.Database;
import io.floci.aws.services.glue.model.GlueCatalog;
import io.floci.aws.services.glue.model.StorageDescriptor;
import io.floci.aws.services.glue.model.Table;
import io.floci.aws.services.resourcegroupstagging.ResourceGroupsTaggingService;
import io.floci.aws.services.s3.S3Service;
import io.floci.aws.services.s3tables.S3TablesService;
import io.floci.aws.services.s3tables.model.Namespace;
import io.floci.aws.services.s3tables.model.S3Table;
import io.floci.aws.services.s3tables.model.TableBucket;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
import java.io.IOException;
import java.time.Instant;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Pattern;
/**
* The catalogs of the Glue Data Catalog beside an account's default one ({@code CreateCatalog}, {@code GetCatalog},
* {@code GetCatalogs}, {@code UpdateCatalog}, {@code DeleteCatalog}), and the catalogs federated from Amazon S3
* Tables. Integrating table buckets with AWS analytics services creates the federated catalog
* {@code s3tablescatalog} (connection {@code aws:s3tables}); each table bucket of the account is then a child
* catalog, {@code s3tablescatalog/<bucket>}, whose databases are the bucket's namespaces and whose tables are its
* tables, read from S3 Tables. A catalog id may name the account in front: {@code <account>:s3tablescatalog/<bucket>}.
*/
@ApplicationScoped
public class GlueCatalogService {
private static final Logger LOG = Logger.getLogger(GlueCatalogService.class);
/** The connection that federates a catalog from Amazon S3 Tables. */
static final String S3_TABLES_CONNECTION = "aws:s3tables";
private static final Pattern ACCOUNT_ID = Pattern.compile("^\\d{12}$");
private static final Pattern ACCOUNT_QUALIFIER = Pattern.compile("^\\d{12}:");
private static final Pattern CATALOG_NAME = Pattern.compile("^(?!(.*[./\\\\]|aws:)).{1,64}$");
private static final int DEFAULT_PAGE_SIZE = 100;
private static final int MAX_PAGE_SIZE = 1000;
private final StorageBackend<String, GlueCatalog> catalogStore;
private final S3TablesService s3Tables;
private final S3Service s3;
private final RegionResolver regionResolver;
private final ResourceGroupsTaggingService tagging;
private final ObjectMapper mapper;
@Inject
public GlueCatalogService(StorageFactory storageFactory, S3TablesService s3Tables, S3Service s3,
RegionResolver regionResolver, ResourceGroupsTaggingService tagging,
ObjectMapper mapper) {
this(new RegionScopedStorage<>(storageFactory, regionResolver, "glue", "catalogs.json",
new TypeReference<>() {}), s3Tables, s3, regionResolver, tagging, mapper);
}
GlueCatalogService(StorageBackend<String, GlueCatalog> catalogStore, S3TablesService s3Tables, S3Service s3,
RegionResolver regionResolver, ResourceGroupsTaggingService tagging, ObjectMapper mapper) {
this.catalogStore = catalogStore;
this.s3Tables = s3Tables;
this.s3 = s3;
this.regionResolver = regionResolver;
this.tagging = tagging;
this.mapper = mapper;
}
// ─── Catalogs ─────────────────────────────────────────────────────────────────
public synchronized void createCatalog(String name, JsonNode input, Map<String, String> tags, String region) {
if (name == null || !CATALOG_NAME.matcher(name).matches()) {
throw invalid("Catalog name must be 1 to 64 characters with no '.', '/' or '\\\\' and must not start "
+ "with 'aws:': " + name);
}
if (ACCOUNT_ID.matcher(name).matches() || ACCOUNT_QUALIFIER.matcher(name).lookingAt()) {
// A catalog id names the account this way, so such a name could never be read back.
throw invalid("Catalog name must not be an account id or start with one followed by ':': " + name);
}
requireInput(input);
if (catalogStore.get(name).isPresent()) {
throw new AwsException("AlreadyExistsException", "Catalog already exists: " + name, 400);
}
GlueCatalog catalog = new GlueCatalog();
catalog.setName(name);
catalog.setCatalogId(qualified(name));
catalog.setResourceArn(arn(region, name));
Instant now = Instant.now();
catalog.setCreateTime(now);
catalog.setUpdateTime(now);
apply(catalog, input, region);
catalogStore.put(name, catalog);
if (tags != null && !tags.isEmpty()) {
try {
tagging.tagResources(List.of(catalog.getResourceArn()), tags, region);
} catch (RuntimeException e) {
catalogStore.delete(name);
throw e;
}
}
LOG.debugv("Created Glue catalog {0}", name);
}
public GlueCatalog getCatalog(String catalogId, String region) {
CatalogPath path = path(catalogId);
if (path.isDefault()) {
throw notFound(catalogId);
}
GlueCatalog parent = catalogStore.get(path.name()).orElseThrow(() -> notFound(catalogId));
if (path.child() == null) {
return parent;
}
return bucketCatalog(parent, bucket(parent, path.child(), region).orElseThrow(() -> notFound(catalogId)),
region);
}
/**
* The catalogs beneath {@code parentCatalogId} (the default catalog when null): the catalogs created in the
* account, or a federated S3 Tables catalog's table buckets; with {@code recursive}, the catalogs beneath those
* too, and with {@code includeRoot} the parent itself first.
*/
public GlueService.Page<GlueCatalog> getCatalogs(String parentCatalogId, boolean recursive, boolean includeRoot,
Integer maxResults, String nextToken, String region) {
List<GlueCatalog> catalogs = new ArrayList<>();
CatalogPath parent = path(parentCatalogId);
if (parent.isDefault()) {
List<GlueCatalog> created = catalogStore.scan(key -> true).stream()
.sorted(Comparator.comparing(GlueCatalog::getName)).toList();
for (GlueCatalog catalog : created) {
catalogs.add(catalog);
if (recursive) {
catalogs.addAll(children(catalog, region));
}
}
} else {
GlueCatalog catalog = getCatalog(parentCatalogId, region);
if (includeRoot) {
catalogs.add(catalog);
}
if (parent.child() == null) {
catalogs.addAll(children(catalog, region));
}
}
PaginatedResult<GlueCatalog> page = Pagination.paginate(catalogs, GlueCatalog::getCatalogId, maxResults,
nextToken, DEFAULT_PAGE_SIZE, MAX_PAGE_SIZE, "InvalidInputException", "NextToken");
return new GlueService.Page<>(page.items(), page.nextToken());
}
public synchronized void updateCatalog(String catalogId, JsonNode input, String region) {
requireInput(input);
// Applied to a copy, so a refused input leaves the stored catalog as it was.
GlueCatalog catalog = mapper.convertValue(createdCatalog(catalogId), GlueCatalog.class);
apply(catalog, input, region);
catalog.setUpdateTime(Instant.now());
catalogStore.put(catalog.getName(), catalog);
}
public synchronized void deleteCatalog(String catalogId, String region) {
GlueCatalog catalog = createdCatalog(catalogId);
catalogStore.delete(catalog.getName());
tagging.deleteResources(List.of(catalog.getResourceArn()), region);
}
// ─── Databases and tables of a federated catalog ────────────────────────────
/**
* Whether {@code catalogId} names the account's default catalog: none at all, the account id, or the default
* catalog of the account named in front of nothing else. A catalog that is neither the default nor a table bucket
* of a federated S3 Tables catalog is answered as one that holds no databases.
*/
public boolean isDefaultCatalog(String catalogId) {
return path(catalogId).isDefault();
}
public GlueService.Page<Database> getDatabases(String catalogId, Integer maxResults, String nextToken,
String region) {
Optional<Federated> federated = federated(catalogId, region);
List<Database> databases = new ArrayList<>();
federated.ifPresent(found -> s3Tables.listNamespaces(found.bucket().getArn(), null, region).stream()
.sorted(Comparator.comparing(Namespace::getName))
.forEach(namespace -> databases.add(database(found.catalogId(), namespace))));
PaginatedResult<Database> page = Pagination.paginate(databases, Database::getName, maxResults, nextToken,
DEFAULT_PAGE_SIZE, MAX_PAGE_SIZE, "InvalidInputException", "NextToken");
return new GlueService.Page<>(page.items(), page.nextToken());
}
public Database getDatabase(String catalogId, String name, String region) {
Federated federated = requireFederated(catalogId, name, region);
return database(federated.catalogId(), requireNamespace(federated, name, region));
}
public GlueService.Page<Table> getTables(String catalogId, String databaseName, Integer maxResults,
String nextToken, String region) {
Federated federated = requireFederated(catalogId, databaseName, region);
requireNamespace(federated, databaseName, region);
List<S3Table> s3TableList = s3Tables.listTables(federated.bucket().getArn(), databaseName, null, region)
.stream().sorted(Comparator.comparing(S3Table::getName)).toList();
// Paged before any metadata file is read, so a page reads only its own tables'.
PaginatedResult<S3Table> page = Pagination.paginate(s3TableList, S3Table::getName, maxResults, nextToken,
DEFAULT_PAGE_SIZE, MAX_PAGE_SIZE, "InvalidInputException", "NextToken");
return new GlueService.Page<>(page.items().stream()
.map(table -> table(federated.catalogId(), databaseName, table)).toList(), page.nextToken());
}
public Table getTable(String catalogId, String databaseName, String name, String region) {
Federated federated = requireFederated(catalogId, databaseName, region);
requireNamespace(federated, databaseName, region);
S3Table table;
try {
table = s3Tables.getTable(federated.bucket().getArn(), databaseName, name, region);
} catch (AwsException e) {
throw new AwsException("EntityNotFoundException", "Table " + name + " not found.", 400);
}
return table(federated.catalogId(), databaseName, table);
}
// ─── Helpers ──────────────────────────────────────────────────────────────────
/** A catalog id split into the catalog it names and, for a child, the child's name. */
private record CatalogPath(String name, String child) {
boolean isDefault() {
return name == null;
}
}
/**
* The catalog {@code catalogId} names: {@code [<account>:]<catalog>[/<child>]}. An account other than the
* caller's names a catalog the caller cannot see here.
*/
private CatalogPath path(String catalogId) {
if (catalogId == null || catalogId.isBlank()) {
return new CatalogPath(null, null);
}
String id = catalogId;
int colon = id.indexOf(':');
if (colon > 0 && ACCOUNT_ID.matcher(id.substring(0, colon)).matches()) {
if (!id.substring(0, colon).equals(regionResolver.getAccountId())) {
throw notFound(catalogId);
}
id = id.substring(colon + 1);
}
if (id.isEmpty() || id.equals(regionResolver.getAccountId())) {
return new CatalogPath(null, null);
}
int slash = id.indexOf('/');
return slash < 0 ? new CatalogPath(id, null) : new CatalogPath(id.substring(0, slash), id.substring(slash + 1));
}
private GlueCatalog createdCatalog(String catalogId) {
CatalogPath path = path(catalogId);
if (path.isDefault() || path.child() != null) {
throw invalid("Only a catalog created with CreateCatalog can be changed or deleted: " + catalogId);
}
return catalogStore.get(path.name()).orElseThrow(() -> notFound(catalogId));
}
private static boolean federatesS3Tables(GlueCatalog catalog) {
return catalog.getFederatedCatalog() != null
&& S3_TABLES_CONNECTION.equals(catalog.getFederatedCatalog().get("ConnectionName"));
}
private List<GlueCatalog> children(GlueCatalog catalog, String region) {
return federatedBuckets(catalog, region).stream()
.sorted(Comparator.comparing(TableBucket::getName))
.map(bucket -> bucketCatalog(catalog, bucket, region))
.toList();
}
private Optional<TableBucket> bucket(GlueCatalog parent, String bucketName, String region) {
return federatedBuckets(parent, region).stream()
.filter(bucket -> bucket.getName().equals(bucketName))
.findFirst();
}
/**
* The table buckets a catalog federates: every bucket of the account in the Region for the identifier
* {@code arn:aws:s3tables:<region>:<account>:bucket/*}, or the one bucket its ARN names.
*/
private List<TableBucket> federatedBuckets(GlueCatalog catalog, String region) {
if (!federatesS3Tables(catalog)) {
return List.of();
}
String identifier = String.valueOf(catalog.getFederatedCatalog().get("Identifier"));
return s3Tables.listTableBuckets(null, region).stream()
.filter(bucket -> identifier.endsWith(":bucket/*")
? bucket.getArn().startsWith(identifier.substring(0, identifier.length() - 1))
: bucket.getArn().equals(identifier))
.toList();
}
/**
* A federated S3 Tables catalog's identifier: all the account's table buckets in the Region
* ({@code arn:aws:s3tables:<region>:<account>:bucket/*}) or one of them, by its ARN.
*/
private void validateS3TablesIdentifier(Map<String, Object> federatedCatalog, String region) {
Object identifier = federatedCatalog.get("Identifier");
String prefix = AwsArnUtils.Arn.of("s3tables", region, regionResolver.getAccountId(), "bucket/").toString();
if (!(identifier instanceof String value) || !value.startsWith(prefix) || value.length() == prefix.length()
|| (value.endsWith("*") && !value.equals(prefix + "*"))) {
throw invalid("FederatedCatalog.Identifier for the aws:s3tables connection must be " + prefix
+ "* or a table bucket ARN of this account and Region: " + identifier);
}
}
/** A table bucket a federated catalog id names, and the catalog's id as catalogs answer it. */
private record Federated(TableBucket bucket, String catalogId) {
}
/** The table bucket a federated catalog id names; empty for the default catalog or any other catalog. */
private Optional<Federated> federated(String catalogId, String region) {
CatalogPath path = path(catalogId);
if (path.isDefault() || path.child() == null) {
return Optional.empty();
}
return catalogStore.get(path.name()).flatMap(parent -> bucket(parent, path.child(), region))
.map(bucket -> new Federated(bucket, qualified(path.name() + "/" + path.child())));
}
private Federated requireFederated(String catalogId, String databaseName, String region) {
return federated(catalogId, region).orElseThrow(() -> new AwsException("EntityNotFoundException",
"Database " + databaseName + " not found.", 400));
}
private GlueCatalog bucketCatalog(GlueCatalog parent, TableBucket bucket, String region) {
String path = parent.getName() + "/" + bucket.getName();
GlueCatalog catalog = new GlueCatalog();
catalog.setName(bucket.getName());
catalog.setCatalogId(qualified(path));
catalog.setResourceArn(arn(region, path));
catalog.setFederatedCatalog(Map.of("Identifier", bucket.getArn(),
"ConnectionName", S3_TABLES_CONNECTION));
catalog.setCreateTime(instant(bucket.getCreatedAt()));
catalog.setUpdateTime(instant(bucket.getCreatedAt()));
return catalog;
}
private Namespace requireNamespace(Federated federated, String name, String region) {
try {
return s3Tables.getNamespace(federated.bucket().getArn(), name, region);
} catch (AwsException e) {
throw new AwsException("EntityNotFoundException", "Database " + name + " not found.", 400);
}
}
private static Database database(String catalogId, Namespace namespace) {
Database database = new Database(namespace.getName());
database.setCatalogId(catalogId);
database.setCreateTime(instant(namespace.getCreatedAt()));
return database;
}
/**
* An S3 table as Glue shows it: an Iceberg table whose {@code metadata_location} and columns come from its current
* metadata file, or a table with only its location while S3 Tables holds no metadata for it yet.
*/
private Table table(String catalogId, String databaseName, S3Table s3Table) {
String metadataLocation = s3Table.getMetadataLocation();
Table table = null;
if (metadataLocation != null && !metadataLocation.isBlank()) {
try {
table = IcebergGlueTables.glueTable(s3Table.getName(),
IcebergGlueTables.readMetadata(s3, mapper, metadataLocation), metadataLocation);
} catch (IOException | RuntimeException e) {
// One unreadable metadata file should not fail a whole listing: the table keeps its location.
LOG.warnv("Cannot read metadata {0} of S3 table {1}; its columns are left out: {2}",
metadataLocation, s3Table.getName(), e.getMessage());
}
}
if (table == null) {
table = new Table();
table.setName(s3Table.getName());
table.setTableType("EXTERNAL_TABLE");
Map<String, String> parameters = new HashMap<>(Map.of("table_type", "ICEBERG"));
if (metadataLocation != null && !metadataLocation.isBlank()) {
parameters.put("metadata_location", metadataLocation);
}
table.setParameters(parameters);
StorageDescriptor descriptor = new StorageDescriptor();
descriptor.setLocation(s3Table.getWarehouseLocation());
table.setStorageDescriptor(descriptor);
}
table.setDatabaseName(databaseName);
table.setCatalogId(catalogId);
table.setCreateTime(instant(s3Table.getCreatedAt()));
table.setUpdateTime(instant(s3Table.getModifiedAt()));
return table;
}
private static void requireInput(JsonNode input) {
if (input == null || input.isNull() || !input.isObject()) {
throw invalid("CatalogInput is required.");
}
}
/** Applies a {@code CatalogInput}, after checking all of it, so a refused one changes nothing. */
private void apply(GlueCatalog catalog, JsonNode input, String region) {
String allowFullTableExternalDataAccess = null;
if (input.has("AllowFullTableExternalDataAccess")) {
allowFullTableExternalDataAccess = input.get("AllowFullTableExternalDataAccess").asText();
if (!allowFullTableExternalDataAccess.equals("True") && !allowFullTableExternalDataAccess.equals("False")) {
throw invalid("AllowFullTableExternalDataAccess must be True or False: "
+ allowFullTableExternalDataAccess);
}
}
Map<String, Object> federatedCatalog = input.has("FederatedCatalog")
? mapper.convertValue(input.get("FederatedCatalog"), new TypeReference<>() {})
: catalog.getFederatedCatalog();
if (federatedCatalog != null && S3_TABLES_CONNECTION.equals(federatedCatalog.get("ConnectionName"))) {
validateS3TablesIdentifier(federatedCatalog, region);
}
if (allowFullTableExternalDataAccess != null) {
catalog.setAllowFullTableExternalDataAccess(allowFullTableExternalDataAccess);
}
if (input.has("Description")) {
catalog.setDescription(input.get("Description").asText(null));
}
if (input.has("Parameters")) {
catalog.setParameters(mapper.convertValue(input.get("Parameters"), new TypeReference<>() {}));
}
catalog.setFederatedCatalog(federatedCatalog);
if (input.has("TargetRedshiftCatalog")) {
catalog.setTargetRedshiftCatalog(mapper.convertValue(input.get("TargetRedshiftCatalog"),
new TypeReference<>() {}));
}
if (input.has("CatalogProperties")) {
catalog.setCatalogProperties(mapper.convertValue(input.get("CatalogProperties"), new TypeReference<>() {}));
}
if (input.has("CreateTableDefaultPermissions")) {
catalog.setCreateTableDefaultPermissions(mapper.convertValue(input.get("CreateTableDefaultPermissions"),
new TypeReference<>() {}));
}
if (input.has("CreateDatabaseDefaultPermissions")) {
catalog.setCreateDatabaseDefaultPermissions(mapper.convertValue(
input.get("CreateDatabaseDefaultPermissions"), new TypeReference<>() {}));
}
}
private String qualified(String path) {
return regionResolver.getAccountId() + ":" + path;
}
private String arn(String region, String path) {
return AwsArnUtils.Arn.of("glue", region, regionResolver.getAccountId(), "catalog/" + path).toString();
}
private static Instant instant(String timestamp) {
if (timestamp == null || timestamp.isBlank()) {
return null;
}
try {
return Instant.parse(timestamp);
} catch (DateTimeParseException e) {
return null;
}
}
private static AwsException notFound(String catalogId) {
return new AwsException("EntityNotFoundException", "Catalog not found: " + catalogId, 400);
}
private static AwsException invalid(String message) {
return new AwsException("InvalidInputException", message, 400);
}
}
@@ -47,6 +47,7 @@ public class GlueJsonHandler {
private final GlueCrawlerRunService crawlerRunService;
private final GlueTriggerService triggerService;
private final GlueSchemaRegistryService schemaRegistryService;
private final GlueCatalogService catalogService;
private final ObjectMapper mapper;
@Inject
@@ -55,12 +56,14 @@ public class GlueJsonHandler {
GlueCrawlerRunService crawlerRunService,
GlueTriggerService triggerService,
GlueSchemaRegistryService schemaRegistryService,
GlueCatalogService catalogService,
ObjectMapper mapper) {
this.glueService = glueService;
this.jobRunService = jobRunService;
this.crawlerRunService = crawlerRunService;
this.triggerService = triggerService;
this.schemaRegistryService = schemaRegistryService;
this.catalogService = catalogService;
this.mapper = mapper;
}
@@ -86,12 +89,39 @@ public class GlueJsonHandler {
glueService.createDatabase(db, tags, region);
yield Response.ok().build();
}
case "CreateCatalog" -> {
catalogService.createCatalog(request.path("Name").asText(null), request.get("CatalogInput"),
request.has("Tags") ? mapper.convertValue(request.get("Tags"), STRING_MAP) : null, region);
yield Response.ok(Map.of()).build();
}
case "GetCatalog" -> Response.ok(Map.of("Catalog",
catalogService.getCatalog(request.path("CatalogId").asText(null), region))).build();
case "GetCatalogs" -> {
GlueService.Page<GlueCatalog> page = catalogService.getCatalogs(
request.path("ParentCatalogId").asText(null), request.path("Recursive").asBoolean(false),
request.path("IncludeRoot").asBoolean(false),
Pagination.parseMaxResults(request, "MaxResults", "InvalidInputException"),
Pagination.parseNextToken(request, "NextToken", "InvalidInputException"), region);
yield Response.ok(pageResponse("CatalogList", page.items(), page.nextToken())).build();
}
case "UpdateCatalog" -> {
catalogService.updateCatalog(request.path("CatalogId").asText(null), request.get("CatalogInput"),
region);
yield Response.ok(Map.of()).build();
}
case "DeleteCatalog" -> {
catalogService.deleteCatalog(request.path("CatalogId").asText(null), region);
yield Response.ok(Map.of()).build();
}
case "GetDatabase" -> {
String name = request.get("Name").asText();
Database db = glueService.getDatabase(name);
String catalogId = request.path("CatalogId").asText(null);
Database db = catalogService.isDefaultCatalog(catalogId)
? glueService.getDatabase(name)
: catalogService.getDatabase(catalogId, name, region);
yield Response.ok(Map.of("Database", db)).build();
}
case "GetDatabases" -> handleGetDatabases(request);
case "GetDatabases" -> handleGetDatabases(request, region);
case "DeleteDatabase" -> {
String name = request.get("Name").asText();
glueService.deleteDatabase(name, region);
@@ -112,7 +142,10 @@ public class GlueJsonHandler {
case "GetTable" -> {
String dbName = request.get("DatabaseName").asText();
String tableName = request.get("Name").asText();
Table table = glueService.getTable(dbName, tableName);
String catalogId = request.path("CatalogId").asText(null);
Table table = catalogService.isDefaultCatalog(catalogId)
? glueService.getTable(dbName, tableName)
: catalogService.getTable(catalogId, dbName, tableName, region);
yield Response.ok(Map.of("Table", GlueService.withReadDefaults(table))).build();
}
case "CreatePartitionIndex" -> {
@@ -143,8 +176,10 @@ public class GlueJsonHandler {
Integer maxResults = Pagination.parseMaxResults(request, "MaxResults", "InvalidInputException");
String nextToken = Pagination.parseNextToken(request, "NextToken", "InvalidInputException");
String dbName = request.get("DatabaseName").asText();
GlueService.Page<Table> page = glueService.getTables(
dbName, maxResults, nextToken);
String catalogId = request.path("CatalogId").asText(null);
GlueService.Page<Table> page = catalogService.isDefaultCatalog(catalogId)
? glueService.getTables(dbName, maxResults, nextToken)
: catalogService.getTables(catalogId, dbName, maxResults, nextToken, region);
List<Table> tables = page.items().stream().map(GlueService::withReadDefaults).toList();
yield Response.ok(pageResponse("TableList", tables, page.nextToken())).build();
}
@@ -1112,10 +1147,13 @@ public class GlueJsonHandler {
return vn.asLong();
}
private Response handleGetDatabases(JsonNode request) {
GlueService.Page<Database> page = glueService.getDatabases(
Pagination.parseMaxResults(request, "MaxResults", "InvalidInputException"),
Pagination.parseNextToken(request, "NextToken", "InvalidInputException"));
private Response handleGetDatabases(JsonNode request, String region) {
Integer maxResults = Pagination.parseMaxResults(request, "MaxResults", "InvalidInputException");
String nextToken = Pagination.parseNextToken(request, "NextToken", "InvalidInputException");
String catalogId = request.path("CatalogId").asText(null);
GlueService.Page<Database> page = catalogService.isDefaultCatalog(catalogId)
? glueService.getDatabases(maxResults, nextToken)
: catalogService.getDatabases(catalogId, maxResults, nextToken, region);
return Response.ok(pageResponse("DatabaseList", page.items(), page.nextToken())).build();
}
@@ -1805,6 +1805,7 @@ public class GlueService {
Table copy = new Table();
copy.setName(source.getName());
copy.setDatabaseName(source.getDatabaseName());
copy.setCatalogId(source.getCatalogId());
copy.setDescription(source.getDescription());
copy.setOwner(source.getOwner());
copy.setCreateTime(source.getCreateTime());
@@ -8,9 +8,7 @@ import io.floci.aws.core.common.AwsException;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.services.glue.GlueService;
import io.floci.aws.services.glue.GlueTableResolver;
import io.floci.aws.services.glue.model.Column;
import io.floci.aws.services.glue.model.Database;
import io.floci.aws.services.glue.model.StorageDescriptor;
import io.floci.aws.services.glue.model.Table;
import io.floci.aws.services.s3.S3Service;
import jakarta.enterprise.context.ApplicationScoped;
@@ -18,7 +16,6 @@ import jakarta.inject.Inject;
import org.jboss.logging.Logger;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.time.Clock;
import java.util.ArrayList;
@@ -47,11 +44,10 @@ public class GlueIcebergCatalog {
private static final Logger LOG = Logger.getLogger(GlueIcebergCatalog.class);
static final String DEFAULT_CATALOG = ":";
private static final String PARAM_TABLE_TYPE = "table_type";
private static final String PARAM_METADATA_LOCATION = "metadata_location";
private static final String PARAM_TABLE_TYPE = IcebergGlueTables.PARAM_TABLE_TYPE;
private static final String PARAM_METADATA_LOCATION = IcebergGlueTables.PARAM_METADATA_LOCATION;
private static final String PARAM_PREVIOUS_METADATA_LOCATION = "previous_metadata_location";
private static final String ICEBERG = "ICEBERG";
private static final Pattern S3_LOCATION = Pattern.compile("^s3[an]?://([^/]+)/?(.*)$");
private static final String ICEBERG = IcebergGlueTables.ICEBERG;
private static final Pattern METADATA_VERSION = Pattern.compile("^(\\d+)-.*\\.metadata\\.json$");
/** The REST routes the endpoint answers, as {@code GetConfig}'s {@code endpoints} names them. */
private static final List<String> ENDPOINTS = List.of(
@@ -453,43 +449,24 @@ public class GlueIcebergCatalog {
* {@code existing} holds beyond those (a description, other parameters) carries over.
*/
private Table icebergTable(String name, ObjectNode metadata, String metadataLocation, Table existing) {
Table table = new Table();
table.setName(name);
table.setTableType("EXTERNAL_TABLE");
Map<String, String> parameters = new HashMap<>();
Table table = IcebergGlueTables.glueTable(name, metadata, metadataLocation);
if (existing != null) {
table.setDescription(existing.getDescription());
table.setOwner(existing.getOwner());
if (existing.getParameters() != null) {
parameters.putAll(existing.getParameters());
Map<String, String> parameters = new HashMap<>(existing.getParameters());
parameters.putAll(table.getParameters());
table.setParameters(parameters);
}
}
parameters.put(PARAM_TABLE_TYPE, ICEBERG);
parameters.put(PARAM_METADATA_LOCATION, metadataLocation);
table.setParameters(parameters);
StorageDescriptor descriptor = new StorageDescriptor();
descriptor.setLocation(metadata.path("location").asText());
List<Column> columns = new ArrayList<>();
for (JsonNode field : IcebergTableMetadata.currentSchema(metadata).path("fields")) {
Column column = new Column(field.path("name").asText(), IcebergTypes.hiveType(field.path("type")));
if (field.hasNonNull("doc")) {
column.setComment(field.get("doc").asText());
}
column.setParameters(Map.of(
"iceberg.field.id", field.path("id").asText(),
"iceberg.field.optional", String.valueOf(!field.path("required").asBoolean(false)),
"iceberg.field.current", "true"));
columns.add(column);
}
descriptor.setColumns(columns);
table.setStorageDescriptor(descriptor);
return table;
}
private ObjectNode readMetadata(String metadataLocation) {
S3Location location = s3Location(metadataLocation);
try (InputStream stream = s3.openObjectStream(location.bucket(), location.key(), null)) {
return (ObjectNode) mapper.readTree(stream);
try {
return (ObjectNode) IcebergGlueTables.readMetadata(s3, mapper, metadataLocation);
} catch (IllegalArgumentException e) {
throw IcebergRestException.badRequest(e.getMessage());
} catch (AwsException e) {
throw IcebergRestException.badRequest("Cannot read table metadata " + metadataLocation + ": "
+ e.getMessage());
@@ -503,7 +480,7 @@ public class GlueIcebergCatalog {
private String writeMetadata(ObjectNode metadata, int version) {
String fileName = String.format("%05d-%s.metadata.json", version, UUID.randomUUID());
String metadataLocation = metadata.path("location").asText() + "/metadata/" + fileName;
S3Location location = s3Location(metadataLocation);
IcebergGlueTables.S3Location location = s3Location(metadataLocation);
try {
s3.putObject(location.bucket(), location.key(),
mapper.writeValueAsString(metadata).getBytes(StandardCharsets.UTF_8), "application/json", null);
@@ -519,7 +496,7 @@ public class GlueIcebergCatalog {
/** Removes a metadata file a failed commit wrote; a failure to is logged, since the commit's error matters more. */
private void deleteMetadata(String metadataLocation) {
S3Location location = s3Location(metadataLocation);
IcebergGlueTables.S3Location location = s3Location(metadataLocation);
try {
s3.deleteObject(location.bucket(), location.key());
} catch (RuntimeException e) {
@@ -546,12 +523,12 @@ public class GlueIcebergCatalog {
return kept;
}
private static S3Location s3Location(String location) {
Matcher matcher = S3_LOCATION.matcher(location == null ? "" : location);
if (!matcher.matches() || matcher.group(2).isEmpty()) {
throw IcebergRestException.badRequest("Not an S3 object location: " + location);
private static IcebergGlueTables.S3Location s3Location(String location) {
try {
return IcebergGlueTables.s3Location(location);
} catch (IllegalArgumentException e) {
throw IcebergRestException.badRequest(e.getMessage());
}
return new S3Location(matcher.group(1), matcher.group(2));
}
private static IcebergRestException translated(AwsException e, String database, String table) {
@@ -562,7 +539,4 @@ public class GlueIcebergCatalog {
default -> IcebergRestException.badRequest(e.getMessage());
};
}
private record S3Location(String bucket, String key) {
}
}
@@ -0,0 +1,85 @@
package io.floci.aws.services.glue.iceberg;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.floci.aws.services.glue.model.Column;
import io.floci.aws.services.glue.model.StorageDescriptor;
import io.floci.aws.services.glue.model.Table;
import io.floci.aws.services.s3.S3Service;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** The Glue table AWS Glue shows for an Iceberg table, built from the table's metadata file. */
public final class IcebergGlueTables {
static final String PARAM_TABLE_TYPE = "table_type";
static final String PARAM_METADATA_LOCATION = "metadata_location";
static final String ICEBERG = "ICEBERG";
private static final Pattern S3_LOCATION = Pattern.compile("^s3[an]?://([^/]+)/?(.*)$");
private IcebergGlueTables() {
}
/**
* The Glue table holding an Iceberg table: {@code table_type} and {@code metadata_location} in its parameters,
* its location and its current schema's columns, with their Hive types and Iceberg field ids, in its storage
* descriptor, as AWS Glue keeps one.
*/
public static Table glueTable(String name, JsonNode metadata, String metadataLocation) {
Table table = new Table();
table.setName(name);
table.setTableType("EXTERNAL_TABLE");
Map<String, String> parameters = new HashMap<>();
parameters.put(PARAM_TABLE_TYPE, ICEBERG);
parameters.put(PARAM_METADATA_LOCATION, metadataLocation);
table.setParameters(parameters);
StorageDescriptor descriptor = new StorageDescriptor();
descriptor.setLocation(metadata.path("location").asText());
List<Column> columns = new ArrayList<>();
for (JsonNode field : IcebergTableMetadata.currentSchema(metadata).path("fields")) {
Column column = new Column(field.path("name").asText(), IcebergTypes.hiveType(field.path("type")));
if (field.hasNonNull("doc")) {
column.setComment(field.get("doc").asText());
}
column.setParameters(Map.of(
"iceberg.field.id", field.path("id").asText(),
"iceberg.field.optional", String.valueOf(!field.path("required").asBoolean(false)),
"iceberg.field.current", "true"));
columns.add(column);
}
descriptor.setColumns(columns);
table.setStorageDescriptor(descriptor);
return table;
}
/**
* Reads the metadata file at {@code metadataLocation}, an {@code s3://} object.
*
* @throws IllegalArgumentException when the location is not an S3 object
*/
public static JsonNode readMetadata(S3Service s3, ObjectMapper mapper, String metadataLocation) throws IOException {
S3Location location = s3Location(metadataLocation);
try (InputStream stream = s3.openObjectStream(location.bucket(), location.key(), null)) {
return mapper.readTree(stream);
}
}
/** The bucket and key an {@code s3://} (or {@code s3a://}, {@code s3n://}) object location names. */
static S3Location s3Location(String location) {
Matcher matcher = S3_LOCATION.matcher(location == null ? "" : location);
if (!matcher.matches() || matcher.group(2).isEmpty()) {
throw new IllegalArgumentException("Not an S3 object location: " + location);
}
return new S3Location(matcher.group(1), matcher.group(2));
}
record S3Location(String bucket, String key) {
}
}
@@ -0,0 +1,79 @@
package io.floci.aws.services.glue.model;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.quarkus.runtime.annotations.RegisterForReflection;
import java.time.Instant;
import java.util.List;
import java.util.Map;
/** A Glue Data Catalog catalog ({@code Catalog}): a federated catalog such as {@code s3tablescatalog}, or one of its children. */
@RegisterForReflection
@JsonInclude(JsonInclude.Include.NON_NULL)
public class GlueCatalog {
@JsonProperty("CatalogId")
private String catalogId;
@JsonProperty("Name")
private String name;
@JsonProperty("ResourceArn")
private String resourceArn;
@JsonProperty("Description")
private String description;
@JsonProperty("Parameters")
private Map<String, String> parameters;
@JsonProperty("CreateTime")
@JsonFormat(shape = JsonFormat.Shape.NUMBER)
private Instant createTime;
@JsonProperty("UpdateTime")
@JsonFormat(shape = JsonFormat.Shape.NUMBER)
private Instant updateTime;
@JsonProperty("TargetRedshiftCatalog")
private Map<String, Object> targetRedshiftCatalog;
@JsonProperty("FederatedCatalog")
private Map<String, Object> federatedCatalog;
@JsonProperty("CatalogProperties")
private Map<String, Object> catalogProperties;
@JsonProperty("CreateTableDefaultPermissions")
private List<Map<String, Object>> createTableDefaultPermissions;
@JsonProperty("CreateDatabaseDefaultPermissions")
private List<Map<String, Object>> createDatabaseDefaultPermissions;
@JsonProperty("AllowFullTableExternalDataAccess")
private String allowFullTableExternalDataAccess;
public GlueCatalog() {}
public String getCatalogId() { return catalogId; }
public void setCatalogId(String catalogId) { this.catalogId = catalogId; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getResourceArn() { return resourceArn; }
public void setResourceArn(String resourceArn) { this.resourceArn = resourceArn; }
public String getDescription() { return description; }
public void setDescription(String description) { this.description = description; }
public Map<String, String> getParameters() { return parameters; }
public void setParameters(Map<String, String> parameters) { this.parameters = parameters; }
public Instant getCreateTime() { return createTime; }
public void setCreateTime(Instant createTime) { this.createTime = createTime; }
public Instant getUpdateTime() { return updateTime; }
public void setUpdateTime(Instant updateTime) { this.updateTime = updateTime; }
public Map<String, Object> getTargetRedshiftCatalog() { return targetRedshiftCatalog; }
public void setTargetRedshiftCatalog(Map<String, Object> targetRedshiftCatalog) {
this.targetRedshiftCatalog = targetRedshiftCatalog;
}
public Map<String, Object> getFederatedCatalog() { return federatedCatalog; }
public void setFederatedCatalog(Map<String, Object> federatedCatalog) { this.federatedCatalog = federatedCatalog; }
public Map<String, Object> getCatalogProperties() { return catalogProperties; }
public void setCatalogProperties(Map<String, Object> catalogProperties) { this.catalogProperties = catalogProperties; }
public List<Map<String, Object>> getCreateTableDefaultPermissions() { return createTableDefaultPermissions; }
public void setCreateTableDefaultPermissions(List<Map<String, Object>> permissions) {
this.createTableDefaultPermissions = permissions;
}
public List<Map<String, Object>> getCreateDatabaseDefaultPermissions() { return createDatabaseDefaultPermissions; }
public void setCreateDatabaseDefaultPermissions(List<Map<String, Object>> permissions) {
this.createDatabaseDefaultPermissions = permissions;
}
public String getAllowFullTableExternalDataAccess() { return allowFullTableExternalDataAccess; }
public void setAllowFullTableExternalDataAccess(String value) { this.allowFullTableExternalDataAccess = value; }
}
@@ -1,6 +1,7 @@
package io.floci.aws.services.glue.model;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.quarkus.runtime.annotations.RegisterForReflection;
@@ -14,6 +15,9 @@ public class Table {
private String name;
@JsonProperty("DatabaseName")
private String databaseName;
@JsonProperty("CatalogId")
@JsonInclude(JsonInclude.Include.NON_NULL)
private String catalogId;
@JsonProperty("Description")
private String description;
@JsonProperty("Owner")
@@ -52,6 +56,8 @@ public class Table {
public void setName(String name) { this.name = name; }
public String getDatabaseName() { return databaseName; }
public void setDatabaseName(String databaseName) { this.databaseName = databaseName; }
public String getCatalogId() { return catalogId; }
public void setCatalogId(String catalogId) { this.catalogId = catalogId; }
public String getDescription() { return description; }
public void setDescription(String description) { this.description = description; }
public String getOwner() { return owner; }