Access OSS Tables by using Gravitino

Updated at:

OSS Tables supports Iceberg REST Catalog and does not require an external catalog service such as Hive Metastore. Apache Gravitino is an open source unified metadata management platform. You can deploy a custom backend on the Gravitino Iceberg REST Catalog Server to mount the Iceberg REST Catalog of OSS Tables as a catalog federated by Gravitino. This allows you to centrally manage and access OSS Tables metadata.

Prerequisites

  • An Apache Gravitino service is deployed.

  • A table bucket is created in OSS Tables. If no table bucket is available, see OSS Tables.

  • Spark 3.5 or later is installed to access OSS Tables data through Gravitino.

Step 1: Configure environment variables

Credentials are required by both the Gravitino Iceberg REST service and the Spark client that subsequently accesses OSS Tables. The environment variable names use the AWS_ prefix because the underlying Iceberg SigV4 signing module and S3FileIO use the standard AWS SDK credential provider chain. Specify Alibaba Cloud account credentials as the variable values.

Configure environment variables for the Gravitino service

Set the following environment variables before you start the Gravitino Iceberg REST service. After the variables are configured, you do not need to specify rest.access-key-id or rest.secret-access-key when you create a catalog.

export AWS_ACCESS_KEY_ID=<Alibaba Cloud AccessKey ID>
export AWS_SECRET_ACCESS_KEY=<Alibaba Cloud AccessKey secret>
export AWS_REGION=<region, such as cn-hangzhou>
# Optional. Configure this variable if you use temporary STS credentials.
export AWS_SESSION_TOKEN=<Alibaba Cloud STS token>

Configure environment variables for Spark

Set the following environment variables before you start the Spark driver and executors. After the variables are configured, you do not need to specify spark.sql.catalog.oss_cata.s3.access-key-id or spark.sql.catalog.oss_cata.s3.secret-access-key.

export AWS_ACCESS_KEY_ID=<Alibaba Cloud AccessKey ID>
export AWS_SECRET_ACCESS_KEY=<Alibaba Cloud AccessKey secret>
export AWS_REGION=<region, such as cn-hangzhou>
# Optional. Configure this variable if you use temporary STS credentials.
export AWS_SESSION_TOKEN=<Alibaba Cloud STS token>
export AWS_REQUEST_CHECKSUM_CALCULATION=WHEN_REQUIRED
Note

In production environments, use environment variables or temporary STS credentials to avoid storing long-term AccessKey pairs in plaintext in configuration files. In a distributed deployment, make sure that the Gravitino service process and the Spark driver and executors can read their required environment variables.

Step 2: Prepare dependency packages

The OSSFileIO implementation in the current Apache Iceberg community version has limited performance. We recommend that you configure S3FileIO for the client. To prevent the io-impl value returned by Gravitino from overriding the client setting, modify the Gravitino source code. Edit iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/iceberg/service/CatalogWrapperForREST.java and remove the IcebergConstants.IO_IMPL line from catalogPropertiesToClientKeys:

private static final Set<String> catalogPropertiesToClientKeys =
    ImmutableSet.of(
        // IcebergConstants.IO_IMPL,   // Remove this line.
        IcebergConstants.AWS_S3_REGION,
        IcebergConstants.ICEBERG_S3_ENDPOINT,
        IcebergConstants.ICEBERG_OSS_ENDPOINT,

You must also deploy the following dependency package to the libs directory of Gravitino:

Dependency package

Version

Purpose

gravitino-iceberg-aws-bundle-xxx.jar

A version that matches your Gravitino version

Loads S3-related dependencies. To build the package, we recommend that you run ./gradlew :bundles:iceberg-aws-bundle:shadowJar in the Gravitino source code.

Step 3: Create OSS Tables resources

Before you configure the Gravitino catalog, prepare a table bucket in OSS Tables and obtain its endpoint information.

Create a table bucket

If no table bucket is available, create one by using ossutil or the AWS CLI. For more information, see OSS Tables. The ARN of a table bucket is in the acs:osstables:{region}:{accountId}:bucket/{bucketName} format. Use this ARN as the value of the warehouse parameter for the Gravitino catalog in Step 3.

Obtain endpoints

OSS Tables provides an Iceberg REST Catalog endpoint. Gravitino uses this endpoint to connect to and manage table metadata in OSS Tables. Use the endpoint as the value of the uri parameter for the Gravitino catalog in Step 3. The endpoint formats are as follows:

  • Internal endpoint: https://{region}-internal.oss-tables.aliyuncs.com/iceberg

  • Public endpoint: https://{region}.oss-tables.aliyuncs.com/iceberg

Note

S3FileIO accesses underlying data files through the OSS data plane. The OSS endpoint is in the https://oss-{region}-internal.aliyuncs.com format for internal access or the https://oss-{region}.aliyuncs.com format for public access. The OSS Tables REST Catalog returns this endpoint for use by the client-side S3FileIO. You do not need to separately specify the endpoint in the Gravitino catalog. For information about related dependencies and source code changes, see Step 1.

Step 4: Create a Gravitino REST catalog based on OSS Tables

In the following examples, the Metalake is named my_metalake. The Gravitino Iceberg REST service is also bound to this Metalake.

Configure the Gravitino Iceberg REST service

Configure the Metalake for Iceberg REST Catalog in the Gravitino configuration file:

# ===== Iceberg REST Auxiliary Service =====
gravitino.auxService.names = iceberg-rest
# Change the path as needed. You can place the preceding dependency package in this directory.
gravitino.iceberg-rest.classpath = iceberg-rest-server/libs, iceberg-rest-server/conf
gravitino.iceberg-rest.host = 0.0.0.0
gravitino.iceberg-rest.httpPort = 9001
gravitino.iceberg-rest.catalog-config-provider = dynamic-config-provider
gravitino.iceberg-rest.gravitino-metalake = my_metalake

Create a Metalake

curl -X POST http://localhost:8090/api/metalakes \
  -H "Content-Type: application/json" \
  -d '{
        "name": "my_metalake",
        "comment": "Main metalake",
        "properties": {}
      }'

Create a catalog

Create a catalog based on OSS Tables:

curl -X POST http://localhost:8090/api/metalakes/my_metalake/catalogs \
  -H "Content-Type: application/json" \
  -d '{
    "name": "osstables_catalog",
    "type": "RELATIONAL",
    "provider": "lakehouse-iceberg",
    "comment": "OSS Tables via Iceberg REST",
    "properties": {
      "catalog-backend": "custom",
      "catalog-backend-impl": "org.apache.iceberg.rest.RESTCatalog",
      "uri": "https://cn-hangzhou-internal.oss-tables.aliyuncs.com/iceberg",
      "warehouse": "acs:osstables:cn-hangzhou:{accountId}:bucket/your-table-bucket-name",
      "rest.auth.type": "sigv4",
      "rest.signing-region": "cn-hangzhou",
      "rest.signing-name": "osstables"
    }
  }'
Note

Change the AccessKey pair, endpoint, and region in the configuration based on your environment. The uri value is the Iceberg REST Catalog endpoint of OSS Tables. The internal endpoint is in the https://{region}-internal.oss-tables.aliyuncs.com/iceberg format. The warehouse value is the ARN of the table bucket and is in the acs:osstables:{region}:{accountId}:bucket/{bucketName} format.

Step 5: Access OSS Tables through Gravitino

After you complete the preceding steps, a catalog named osstables_catalog is available in Gravitino. Configure the following parameters in Spark to access OSS Tables through Gravitino:

spark.sql.catalog.oss_cata=org.apache.iceberg.spark.SparkCatalog
spark.sql.catalog.oss_cata.type=rest
spark.sql.catalog.oss_cata.io-impl=org.apache.iceberg.aws.s3.S3FileIO
spark.sql.catalog.oss_cata.uri=http://127.0.0.1:9001/iceberg/
spark.sql.catalog.oss_cata.warehouse=osstables_catalog
spark.sql.catalog.oss_cata.s3.region=<region, such as cn-hangzhou>
spark.sql.catalog.oss_cata.s3.endpoint=https://oss-{region}-internal.aliyuncs.com
Note

You must explicitly set spark.sql.catalog.oss_cata.io-impl=org.apache.iceberg.aws.s3.S3FileIO. In Step 1, the forwarding of io-impl by Gravitino is removed. Therefore, the client must specify S3FileIO. Otherwise, the client falls back to HadoopFileIO and returns an error because HadoopFileIO cannot recognize oss:// paths. You must also point s3.endpoint to the OSS data-plane endpoint and set AWS_REQUEST_CHECKSUM_CALCULATION=WHEN_REQUIRED before you start Spark. This setting disables chunked upload encoding, which is not supported by OSS.

After the configuration is complete, you can use standard SQL statements to manage data in OSS Tables. In the following examples, the catalog is named oss_cata.

Manage namespaces

A namespace logically groups tables and serves a purpose similar to that of a database.

-- List existing namespaces.
SHOW NAMESPACES IN oss_cata;

-- Create a namespace.
CREATE NAMESPACE oss_cata.my_namespace;

-- Delete a namespace. Before you delete the namespace, delete all tables in it.
DROP NAMESPACE oss_cata.my_namespace;

Create and manage tables

-- Create an unpartitioned table.
CREATE TABLE oss_cata.my_namespace.users (
    id BIGINT NOT NULL COMMENT 'User ID',
    name STRING COMMENT 'Username',
    email STRING COMMENT 'Email address',
    created_at TIMESTAMP COMMENT 'Creation time'
) USING iceberg;

-- Create a table partitioned by day.
CREATE TABLE oss_cata.my_namespace.events (
    id BIGINT NOT NULL,
    event_type STRING,
    data STRING,
    ts TIMESTAMP
) USING iceberg
PARTITIONED BY (days(ts));

-- List all tables in the namespace.
SHOW TABLES IN oss_cata.my_namespace;

-- View the table schema.
DESCRIBE TABLE oss_cata.my_namespace.users;

-- Delete a table. OSS Tables requires the PURGE keyword. Otherwise, the following error is returned:
-- OSS Tables only supports dropping tables with purge enabled.
DROP TABLE oss_cata.my_namespace.users PURGE;

Write and query data

-- Insert data.
INSERT INTO oss_cata.my_namespace.users VALUES
    (1, 'Alice', 'alice@example.com', TIMESTAMP '2024-01-15 10:30:00'),
    (2, 'Bob', 'bob@example.com', TIMESTAMP '2024-01-16 14:20:00'),
    (3, 'Carol', 'carol@example.com', TIMESTAMP '2024-01-17 09:15:00');

-- Query the entire table.
SELECT * FROM oss_cata.my_namespace.users;

-- Run a conditional query.
SELECT * FROM oss_cata.my_namespace.users WHERE id = 2;

-- Run an aggregate query.
SELECT COUNT(*) AS total FROM oss_cata.my_namespace.users;

-- Group and aggregate data.
SELECT name, COUNT(*) AS cnt FROM oss_cata.my_namespace.users GROUP BY name;

-- Update data.
UPDATE oss_cata.my_namespace.users SET name = 'Dave' WHERE id = 3;

-- Delete data.
DELETE FROM oss_cata.my_namespace.users WHERE id = 1;

-- Verify the query result.
SELECT * FROM oss_cata.my_namespace.users ORDER BY id;

Manage partitioned tables

-- Insert partitioned data.
INSERT INTO oss_cata.my_namespace.events VALUES
    (1, 'click', '{"page": "home"}', TIMESTAMP '2024-01-15 10:30:00'),
    (2, 'view', '{"page": "product"}', TIMESTAMP '2024-01-15 11:00:00'),
    (3, 'click', '{"page": "detail"}', TIMESTAMP '2024-01-16 09:00:00');

-- Run a partition-pruning query that scans only matching partitions.
SELECT * FROM oss_cata.my_namespace.events
WHERE ts >= TIMESTAMP '2024-01-15 00:00:00'
  AND ts < TIMESTAMP '2024-01-16 00:00:00';

-- Aggregate data.
SELECT event_type, COUNT(*) AS cnt
FROM oss_cata.my_namespace.events
GROUP BY event_type;

Run time travel queries

Iceberg supports time travel queries, which allow you to read a historical snapshot of data at a specific point in time.

-- View the snapshot history.
SELECT snapshot_id, committed_at, operation
FROM oss_cata.my_namespace.users.snapshots;

-- Query historical data by snapshot ID.
SELECT * FROM oss_cata.my_namespace.users
VERSION AS OF <snapshot_id>;

-- Query data at a specified point in time.
SELECT * FROM oss_cata.my_namespace.users
TIMESTAMP AS OF TIMESTAMP '2024-01-16 00:00:00';

-- View data file distribution.
SELECT * FROM oss_cata.my_namespace.users.files;