athena-utils 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +74 -1
- data/athena-utils.gemspec +1 -1
- data/bin/athena +4 -4
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a6cfc30203a5a7011e98d08203bb667a82635fef01f429aea57816ed26e2f162
|
4
|
+
data.tar.gz: 8f54fbfdaa0decb20edadf57a01ff93507d42ec97c7ca033811be22d03339e22
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eb94223f2bc84ae9363c64ba9fadfa24b4afa6003c41b498979b1b0673dbf0f97d330b7ce4d8c55abefe29718ce9191aba35229d4c9feb6515f2cd391605a55a
|
7
|
+
data.tar.gz: eecb9e9020be2969c47ed309d41c1a001e1910fd6bba0bc7956855507b1d4aa73497fdcd9964f7e7ea3d9ba9fdf85391519c7b6be5884842178d3d68a8a8f630
|
data/README.md
CHANGED
@@ -1,3 +1,76 @@
|
|
1
1
|
# AWS Athena Utils
|
2
2
|
|
3
|
-
|
3
|
+
Purpose of this utility is to execute Athena queries and wait for results.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
Install the gem:
|
8
|
+
|
9
|
+
```
|
10
|
+
gem install 'athena-utils'
|
11
|
+
```
|
12
|
+
|
13
|
+
Create an Athena client and wait for results.
|
14
|
+
|
15
|
+
```
|
16
|
+
require 'athena-utils'
|
17
|
+
|
18
|
+
athena_database = 'test_db'
|
19
|
+
athena_workspace = 'primary'
|
20
|
+
athena_client = AthenaUtils::AthenaClient.new(athena_database, athena_workspace)
|
21
|
+
|
22
|
+
results = athena_client.query("SELECT * FROM users WHERE created_at >= Date('2022-01-01')")
|
23
|
+
results.first
|
24
|
+
# => {"id"=>"8", "name"=>"Foo", "email"=>"foo@example.com", "created_at"=>"2022-02-01"}
|
25
|
+
```
|
26
|
+
|
27
|
+
Execute multiple queries and wait for results.
|
28
|
+
|
29
|
+
```
|
30
|
+
require 'athena-utils'
|
31
|
+
|
32
|
+
athena_database = 'test_db'
|
33
|
+
athena_workspace = 'primary'
|
34
|
+
athena_client = AthenaUtils::AthenaClient.new(athena_database, athena_workspace)
|
35
|
+
|
36
|
+
# contains table_name => query_execution_id
|
37
|
+
query_executions = {}
|
38
|
+
query_executions['users'] = athena_client.query_async("SELECT * FROM users WHERE created_at >= Date('2022-01-01')")
|
39
|
+
query_executions['groups'] = athena_client.query_async("SELECT * FROM groups WHERE created_at >= Date('2022-01-01')")
|
40
|
+
query_executions['activities'] = athena_client.query_async("SELECT * FROM activities WHERE created_at >= Date('2022-01-01')")
|
41
|
+
|
42
|
+
# given an array of query_execution_id(s)
|
43
|
+
# waits for each query to successfully complete
|
44
|
+
# and returns the results of each in a hash
|
45
|
+
# key is the query_execution_id and the value is the results
|
46
|
+
results = athena_client.wait(query_executions.values)
|
47
|
+
|
48
|
+
users_results = results[query_executions['users']]
|
49
|
+
groups_results = results[query_executions['groups']]
|
50
|
+
activities_results = results[query_executions['activities']]
|
51
|
+
|
52
|
+
users_results.first
|
53
|
+
# => {"id"=>"8", "name"=>"Foo", "email"=>"foo@example.com", "created_at"=>"2022-02-01"}
|
54
|
+
```
|
55
|
+
|
56
|
+
## AthenaUtils::AthenaQueryResults
|
57
|
+
|
58
|
+
Streams the results of the query. Avoids loading the results into memory.
|
59
|
+
|
60
|
+
The class implements the Enumerable module. For easy of use the _each_ method returns each row in a hash where the header names are the keys.
|
61
|
+
|
62
|
+
How to access the rows as an array
|
63
|
+
|
64
|
+
```
|
65
|
+
# given the above query logic
|
66
|
+
headers = users_results.csv.shift
|
67
|
+
|
68
|
+
while(row = users_results.csv.shift)
|
69
|
+
id = row[0]
|
70
|
+
name = row[1]
|
71
|
+
emai = row[2]
|
72
|
+
created_at = row[3]
|
73
|
+
|
74
|
+
# do something
|
75
|
+
end
|
76
|
+
```
|
data/athena-utils.gemspec
CHANGED
data/bin/athena
CHANGED
@@ -11,17 +11,17 @@ options = {
|
|
11
11
|
console: false
|
12
12
|
}
|
13
13
|
OptionParser.new do |opts|
|
14
|
-
opts.banner =
|
14
|
+
opts.banner = 'Usage: athena [options]'
|
15
15
|
|
16
|
-
opts.on(
|
16
|
+
opts.on('-d', '--database DATABASE', 'Athena DB') do |v|
|
17
17
|
options[:database] = v
|
18
18
|
end
|
19
19
|
|
20
|
-
opts.on(
|
20
|
+
opts.on('-w', '--work-group WORK_GROUP', 'Athena Work Group, default: primary') do |v|
|
21
21
|
options[:work_group] = v
|
22
22
|
end
|
23
23
|
|
24
|
-
opts.on(
|
24
|
+
opts.on('-e', '--execute QUERY', 'Execute SQL Query') do |v|
|
25
25
|
options[:query] = v
|
26
26
|
end
|
27
27
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: athena-utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Doug Youch
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-02-
|
11
|
+
date: 2022-02-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: aws-sdk-athena
|