步骤 3:创建外部架构和外部表
在外部 schema 中创建外部表。外部 schema 引用了外部数据目录中的数据库并提供了 IAM 角色 ARN [代表您授权您的集群访问 Simple Storage Service(Amazon S3)]。您可在 Amazon Athena Data Catalog、Amazon Glue Data Catalog 或 Apache Hive 元存储(如 Amazon EMR)中创建外部数据库。在此示例中,您将在创建外部 schema Amazon Redshift 时在 Amazon Athena Data Catalog 中创建外部数据库。有关更多信息,请参阅为 Amazon Redshift Spectrum 创建外部 schema。
创建外部 schema 和外部表
-
要创建外部架构,请将以下命令中的 IAM 角色 ARN 替换为您在步骤 1 中创建的角色 ARN。然后在 SQL 客户端中运行该命令。
create external schema myspectrum_schema from data catalog database 'myspectrum_db' iam_role 'arn:aws:iam::123456789012:role/myspectrum_role' create external database if not exists;
-
要创建外部表,请运行以下 CREATE EXTERNAL TABLE 命令。
注意 您的集群和 Amazon S3 桶必须位于同一个 Amazon Web Services 区域中。对于此示例 CREATE EXTERNAL TABLE 命令,包含示例数据的 Amazon S3 桶位于美国东部(弗吉尼亚州北部)Amazon Web Services 区域中。要查看源数据,请下载
sales_ts.000
文件。在中国区域,源数据位于中国(北京)Amazon Web Services 区域 中。 您可以修改此示例以在不同的 Amazon Web Services 区域中运行。在您所需的 Amazon Web Services 区域中创建 Amazon S3 桶。使用 Amazon S3 copy 命令复制销售数据。然后,将示例
CREATE EXTERNAL TABLE
命令中的 location 选项更新到您的桶。aws s3 cp s3://redshift-downloads/tickit/spectrum/sales/ s3://
bucket-name
/tickit/spectrum/sales/ --copy-props none --recursiveAmazon S3 copy 命令的输出确认文件已复制到所需 Amazon Web Services 区域中的
bucket-name
。copy: s3://redshift-downloads/tickit/spectrum/sales/sales_ts.000 to s3://
bucket-name
/tickit/spectrum/sales/sales_ts.000create external table myspectrum_schema.sales( salesid integer, listid integer, sellerid integer, buyerid integer, eventid integer, dateid smallint, qtysold smallint, pricepaid decimal(8,2), commission decimal(8,2), saletime timestamp) row format delimited fields terminated by '\t' stored as textfile location 's3://redshift-downloads/tickit/spectrum/sales/' table properties ('numRows'='172000');