Now that we know the schema, we can work with the dataset as if it was a regular SQL table. To prove the point, let's try to do some data aggregation. Let's get an average flow rate per site for sites that measure flow.
SELECT
site_no Site_no, AVG(Flow) Avg_Flow
FROM (
LOCATION='/s3/td-usgs-public.s3.amazonaws.com/CSVDATA/'
) AS d
GROUP BY
site_no
HAVING
Avg_Flow IS NOT NULL;
Result:
Site_no Avg_Flow
-------- ---------
09380000 11
09423560 73
09424900 93
09429070 81
To register your ad hoc exploratory activity as a permanent source, create it as a foreign table:
-- If you are running this sample as dbc user you will not have permissions
-- to create a table in dbc database. Instead, create a new database and use
-- the newly create database to create a foreign table.
CREATE DATABASE Riverflow
AS PERMANENT = 60e6, -- 60MB
SPOOL = 120e6; -- 120MB
-- change current database to Riverflow
DATABASE Riverflow;
CREATE FOREIGN TABLE riverflow
USING ( LOCATION('/s3/td-usgs-public.s3.amazonaws.com/CSVDATA/') );
SELECT top 10 * FROM riverflow;
Result:
Location GageHeight2 Flow site_no datetime Precipitation GageHeight
------------------------------------------------------------------- ----------- ---- ------- ------------------- ------------- ----------
/S3/s3.amazonaws.com/td-usgs-public/CSVDATA/09429070/2018/07/02.csv null null 9429070 2018-07-02 14:40:00 1.21 null
/S3/s3.amazonaws.com/td-usgs-public/CSVDATA/09400815/2018/07/10.csv null 0.00 9400815 2018-07-10 00:30:00 0.00 -0.01
/S3/s3.amazonaws.com/td-usgs-public/CSVDATA/09400815/2018/07/10.csv null 0.00 9400815 2018-07-10 00:45:00 0.00 -0.01
/S3/s3.amazonaws.com/td-usgs-public/CSVDATA/09400815/2018/07/10.csv null 0.00 9400815 2018-07-10 01:00:00 0.00 -0.01
/S3/s3.amazonaws.com/td-usgs-public/CSVDATA/09400815/2018/07/10.csv null 0.00 9400815 2018-07-10 00:15:00 0.00 -0.01
/S3/s3.amazonaws.com/td-usgs-public/CSVDATA/09429070/2018/07/02.csv null null 9429070 2018-07-02 14:38:00 1.06 null
This time, the SELECT statement looks like a regular select against an in-database table. If you require subsecond response time when querying the data, there is an easy way to bring the CSV data into Teradata to speed things up. Read on to find out how.