Create an external table. The following example creates an external table for your EVENT data:
create external table spectrum.event(
eventid integer,
venueid smallint,
catid smallint,
dateid smallint,
eventname varchar(200),
starttime timestamp
)
row format delimited
fields terminated by '|'
stored as textfile
location 's3://bucket_name/tickit/spectrum/event/';
Note: Replace bucket_name with your S3 bucket's name. To use AWS Glue to create an external table, add table definitions to your Data Catalog.
To use Amazon Athena to create an external table, add table definitions.
Example table definitions with Athena:
CREATE EXTERNAL TABLE spectrum.event (
eventid int,
venueid smallint,
catid smallint,
dateid smallint,
eventname varchar(max),
starttime timestamp)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '|'
STORED AS INPUTFORMAT
'org.apache.hadoop.mapred.TextInputFormat'
OUTPUTFORMAT
To view external tables that your external schema references, run the following SVV_EXTERNAL_TABLES query:
select schemaname , tablename , location from svv_external_tables where schemaname = 'spectrum';
schemaname | tablename | location
----------------+---------------------------------+-----------------------------------------------------------------------
spectrum | event | s3://bucket-name/file-location
Note: Replace bucket-name with your S3 bucket's name and file-location with your file location.
To query the external tables as external Redshift Spectrum tables, use the following SELECT statement:
select top 3 spectrum.sales.eventid, sum(spectrum.sales.pricepaid) from spectrum.sales, spectrum.event
where spectrum.sales.eventid = spectrum.event.eventid
and spectrum.sales.pricepaid > 30
group by spectrum.sales.eventid
order by 2 desc;
eventid | sum
---------+----------
289 | 51846.00
7895 | 51049.00
1602 | 50301.00
The preceding example query combines the external SALES table with an external EVENT table.