Athena
Amazon Athena ノート。
Intro
- Athena
- 標準 SQL を使用 して Amazon S3 のデータを簡単 に分析 できるインタラクティブクエリサービス
- サーバーレスのため、インフラ管理 不要 、実行 したクエリに対 してのみ課金
- Presto を使用 して SQL クエリを実行
- AWS Glue Data Catalog と標準 で統合
Presto
- Facebook が開発 、非 オープンソース
- Presto は Hadoop 上 で動作 する分散 システムで、従来 の MPP(超 並列 処理 )データベース管理 システムと似 たアーキテクチャを使用
- 1 つのコーディネーターノードが複数 のワーカーノードと同期 して動作
Glue Data Catalog
メタデータの内容 :
データの場所
スキーマ
データ型
データ分類
永続 メタデータストア
- クエリやデータ変換 に使用 できるメタデータを保存 、注釈 、共有 できるマネージドサービス
- AWS リージョンごとに 1 つの Glue Data Catalog
- データガバナンスに使用 可能
Athena SQL Query
Create Database
create database demo_data;Create Table
CREATE EXTERNAL TABLE IF NOT EXISTS customers
(
customerid bigint,
firstname string,
lastname string,
fullname string
)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
STORED AS TEXTFILE
LOCATION 's3://your-bucket/data/customers/';CREATE EXTERNAL TABLE IF NOT EXISTS employees(
employeeid bigint,
managerid bigint,
firstname string,
lastname string ,
fullname string,
jobtitle string,
organizationlevel int,
maritalstatus string,
gender string,
territory string,
country string,
group string
)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
STORED AS TEXTFILE
LOCATION 's3://your-bucket/data/employees/';CREATE EXTERNAL TABLE IF NOT EXISTS orders(
salesorderid bigint,
salesorderdetailid int,
orderdate string,
duedate string,
shipdate string,
employeeid bigint,
customerid bigint,
subtotal decimal(17,4),
taxamt decimal(17,4),
freight decimal(17,4),
totaldue decimal(17,4),
productid int,
orderqty int,
unitprice decimal(17,4),
unitpricediscount decimal(17,4),
linetotal decimal(17,4)
)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
STORED AS TEXTFILE
LOCATION 's3://your-bucket/data/orders/';Query Data
SELECT * FROM customers;SELECT firstname, lastname from customers limit 5;Concat
SELECT concat(firstname,' ',lastname) as full_name from customers limit 5;WHERE
SELECT * FROM customers WHERE firstname = 'John' limit 5;OR / AND
SELECT * FROM customers WHERE firstname = 'John' AND lastname = 'Arthur' limit 5;SELECT * FROM customers WHERE firstname = 'John' OR lastname = 'Arthur' limit 5;IN
SELECT * FROM customers WHERE customerid in (371, 377);WILD CARDS
SELECT * FROM customers WHERE fullname like 'J%' limit 5;UNION
SELECT * FROM customers WHERE customerid = 371
UNION
SELECT * FROM customers WHERE customerid in (371, 377);INSERT DATA
INSERT INTO customers (customerid, firstname, lastname, fullname)
values (1221, 'Lex', 'Luthor', 'Lex Luthor');DISTINCT
SELECT DISTINCT firstname FROM customers WHERE fullname like 'J%';COUNT
SELECT COUNT(distinct firstname) FROM customers WHERE firstname like 'J%';GROUP BY
SELECT firstname, COUNT(firstname) as countJFirstnames
FROM customers
WHERE firstname like 'J%'
GROUP BY firstname;Nested Query
SELECT * FROM customers
WHERE customerid IN (SELECT customerid FROM customers);Common Table Expression
WITH cte as
(
SELECT firstname as first_name, lastname as last_name
FROM customers
)
SELECT *
FROM cte;