Code Monkey home page Code Monkey logo

athena-sql's Introduction

Athena SQL

What

Command line tool to query AWS Athena using SQL.

Steps

Install AWSCLI

pip install --upgrade awscli

Configure Credentials

Configure ~/.aws/credentials with your AWS Access Key ID and Secret Access Key.

Install Athena Driver

Download Athena driver.

aws s3 cp s3://athena-downloads/drivers/AthenaJDBC41-1.0.0.jar .

Make sure you have maven installed.

Install Athena driver into local maven repo.

mvn install:install-file \
  -Dfile=AthenaJDBC41-1.0.0.jar \
  -DgroupId=com.amazonaws \
  -DartifactId=athena-jdbc \
  -Dversion=1.0.0 \
  -Dpackaging=jar

Download Athena-SQL

git clone https://github.com/asimjalis/athena-sql
cd athena-sql
export ATHENA_HOME=$(pwd)
export PATH=$PATH:$ATHENA_HOME/bin

Configure Athena-SQL

Create S3 staging directory. Replace this bucket name with your own unique bucket name.

aws s3 mb s3://asimj-athena-s3-staging-dir

Edit config.edn to specify an S3 staging directory that you have write access to.

{:default 
 {:s3-staging-dir "s3://asimj-athena-s3-staging-dir"
  :log-path "/tmp/athena.log"
  :output :csv } }

You can configure the output format to be CSV, JSON, or EDN.

The default is CSV.

Run

athena-sql "DESCRIBE DATABASES"
athena-sql "DESCRIBE TABLES IN default"

Demo

Create Sales Data

#ID,Date,Store,State,Product,Amount
cat << END > sales.csv
101,2014-11-13,100,WA,331,300.00
104,2014-11-18,700,OR,329,450.00
102,2014-11-15,203,CA,321,200.00
106,2014-11-19,202,CA,331,330.00
103,2014-11-17,101,WA,373,750.00
105,2014-11-19,202,CA,321,200.00
END

Make Bucket

Replace this bucket name with your own unique bucket name.

aws s3 mb s3://asimj-athena-example

Stage Data

aws s3 cp sales.csv s3://asimj-athena-example/data/sales.csv

Verify Data

aws s3 cp s3://asimj-athena-example/data/sales.csv -

Create Table

athena-sql "
  CREATE EXTERNAL TABLE IF NOT EXISTS sales (
    id INT,
    date DATE,
    store INT,
    state STRING,
    product INT,
    amount DOUBLE) 
  ROW FORMAT DELIMITED
  FIELDS TERMINATED BY ','
  STORED AS TEXTFILE
  LOCATION 's3://asimj-athena-example/data/'
"

athena-sql "SHOW TABLES IN DEFAULT"
athena-sql "SELECT * FROM SALES LIMIT 10"

States With Highest Transaction Count

athena-sql "
  SELECT state, COUNT(*) AS count 
  FROM sales GROUP BY state ORDER BY count DESC
"

States With Highest Revenue

athena-sql "
  SELECT state, SUM(amount) AS revenue
  FROM sales GROUP BY state ORDER BY revenue DESC
"

Drop Table

athena-sql "DROP TABLE sales"

Troubleshooting

In conf/log4j.properties replace OFF with ERROR or WARN to get full stack trace.

athena-sql's People

Contributors

asimjalis avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.