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;