athena-utils 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3f4fe05a4b845153c14b1b655a590a0983b8264853c44289ffb1498247b526d8
4
- data.tar.gz: 3685bc1f2542f2e02d6a31a7a7e2acf12ea5f78a13f6b526418f59465939f45e
3
+ metadata.gz: f60738b7ecb0c8155fbdf4f85e0bc0371905840808af83eace67328b9a65177b
4
+ data.tar.gz: 8055dfa958fe0e4a3d8d7364a75e7a6ee500ea32316677472c80c20525c47d82
5
5
  SHA512:
6
- metadata.gz: 9b42c88bd431a5f3ee5fa5ee8ef889028f7943ebf2df6fec2b0aec982fdb90c0ffdc87338859147c2cff9f10541debcbec5e3f30b5d4963385d4faf00fe9c4e7
7
- data.tar.gz: c3a2a4b4bd08c7aeda35552c7caf2abd352eb5b2c206135cc5d4853798948507078166b459bf22d42d33bfd79d9f67c44379a5fb6f61476e729030bbcbcbc0ec
6
+ metadata.gz: aeccd09b1b52e06d5c4f7da44ae6b0538f2c779e959d22a2aa3b117709da72c463d409fa640af51208f1e5fc8fecb2cb8e891a45868f4ac9131f01202fbe5b24
7
+ data.tar.gz: 9485169a4b35602f55b7cf60321cc4629a2798b7380f6ec3d8edf4045c03debe20c362f520136fbd858726d65af15c1bf6b4c04cba16473c8dda4d39c6d1d7ce
data/Gemfile CHANGED
@@ -4,4 +4,3 @@ source 'http://rubygems.org'
4
4
 
5
5
  gem 'aws-sdk-athena'
6
6
  gem 'aws-sdk-s3'
7
- gem 'csv-utils'
data/README.md CHANGED
@@ -1 +1,3 @@
1
1
  # AWS Athena Utils
2
+
3
+ A simple query wrapper around Aws::Athena::Client.
data/athena-utils.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'athena-utils'
5
- s.version = '0.1.0'
5
+ s.version = '0.1.1'
6
6
  s.licenses = ['MIT']
7
7
  s.summary = 'Athena Utils'
8
8
  s.description = 'Tools for querying AWS Athena'
@@ -15,5 +15,4 @@ Gem::Specification.new do |s|
15
15
 
16
16
  s.add_runtime_dependency 'aws-sdk-athena'
17
17
  s.add_runtime_dependency 'aws-sdk-s3'
18
- s.add_runtime_dependency 'csv-utils'
19
18
  end
data/bin/athena CHANGED
@@ -20,7 +20,7 @@ OptionParser.new do |opts|
20
20
  options[:output_location] = v
21
21
  end
22
22
 
23
- opts.on("-q", "--query QUERY", "SQL Query") do |v|
23
+ opts.on("-e", "--execute QUERY", "Execute SQL Query") do |v|
24
24
  options[:query] = v
25
25
  end
26
26
 
@@ -2,14 +2,23 @@ require 'aws-sdk-athena'
2
2
 
3
3
  module AthenaUtils
4
4
  class AthenaClient
5
+ DEFAULT_WAIT_TIME = 3 # seconds
6
+
5
7
  # database is the name of the Athena DB
6
8
  # output_location is the full S3 path to store the results of Athena queries
7
9
  attr_reader :database,
8
10
  :output_location
9
11
 
10
- def initialize(database, output_location)
12
+ # wait_time is time to wait before checking query results again
13
+ attr_accessor :wait_time
14
+
15
+ attr_writer :aws_athena_client,
16
+ :aws_s3_client
17
+
18
+ def initialize(database, output_location, wait_time = DEFAULT_WAIT_TIME)
11
19
  @database = database
12
20
  @output_location = output_location
21
+ @wait_time = wait_time
13
22
  end
14
23
 
15
24
  def aws_athena_client
@@ -20,6 +29,14 @@ module AthenaUtils
20
29
  Aws::Athena::Client.new
21
30
  end
22
31
 
32
+ def aws_s3_client
33
+ @aws_s3_client ||= create_aws_s3_client
34
+ end
35
+
36
+ def create_aws_s3_client
37
+ Aws::S3::Client.new
38
+ end
39
+
23
40
  def query(query)
24
41
  query_execution_id = query_async(query)
25
42
  wait([query_execution_id])[query_execution_id]
@@ -52,16 +69,17 @@ module AthenaUtils
52
69
 
53
70
  case query_status[:query_execution][:status][:state]
54
71
  when 'SUCCEEDED'
55
- results[query_execution_id] = AthenaQueryResults.new(query_status)
72
+ results[query_execution_id] = AthenaQueryResults.new(query_status, aws_s3_client)
56
73
  when 'RUNNING',
57
74
  'QUEUED'
58
75
  # no-op
76
+ next
59
77
  else
60
78
  raise(AthenaQueryError.new("Query failed #{query_status}"))
61
79
  end
62
80
  end
63
81
 
64
- sleep(3) if results.size != query_execution_ids.size
82
+ sleep(wait_time) if results.size != query_execution_ids.size
65
83
  end
66
84
 
67
85
  results
@@ -1,14 +1,16 @@
1
1
  require 'aws-sdk-s3'
2
- require 'csv-utils'
2
+ require 'csv'
3
3
 
4
4
  module AthenaUtils
5
5
  class AthenaQueryResults
6
6
  include Enumerable
7
7
 
8
- attr_reader :query_status
8
+ attr_reader :query_status,
9
+ :aws_s3_client
9
10
 
10
- def initialize(query_status)
11
+ def initialize(query_status, aws_s3_client)
11
12
  @query_status = query_status
13
+ @aws_s3_client = aws_s3_client
12
14
  end
13
15
 
14
16
  def s3_url
@@ -38,20 +40,21 @@ module AthenaUtils
38
40
  )
39
41
  end
40
42
 
41
- def aws_s3_client
42
- @aws_s3_client ||= create_aws_s3_client
43
+ def csv
44
+ @csv ||= CSV.new(s3_object.body)
43
45
  end
44
46
 
45
- def create_aws_s3_client
46
- Aws::S3::Client.new
47
+ def headers
48
+ csv.rewind
49
+ csv.shift
47
50
  end
48
51
 
49
- def csv_iterator
50
- @csv_iterator ||= CSVUtils::CSVIterator.new(CSV.new(s3_object.body))
51
- end
52
-
53
- def each(&block)
54
- csv_iterator.each(&block)
52
+ def each
53
+ csv.rewind
54
+ headers = csv.shift
55
+ while (row = csv.shift)
56
+ yield Hash[headers.zip(row)]
57
+ end
55
58
  end
56
59
  end
57
60
  end
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.1.0
4
+ version: 0.1.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-21 00:00:00.000000000 Z
11
+ date: 2022-02-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk-athena
@@ -38,20 +38,6 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
- - !ruby/object:Gem::Dependency
42
- name: csv-utils
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: '0'
48
- type: :runtime
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
41
  description: Tools for querying AWS Athena
56
42
  email: dougyouch@gmail.com
57
43
  executables: