bq 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/README.md +15 -6
- data/bq.gemspec +1 -0
- data/examples/bq_example.rb +50 -0
- data/lib/bq.rb +35 -28
- data/lib/bq/version.rb +1 -1
- metadata +17 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b1ab2f61b813ccc10f7bb45b14db0cbb4619b1c3
|
4
|
+
data.tar.gz: 728a9a4762d67894bae2378f6a471d25d2f5f918
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0828abac3d00b8dc16357c51149695616bdc8a437c702b49d6af462ef1bf39bf492ea14c001e4ca0fc308b3d77c1fae3be734ee2b13c8efbbf32dbca65367288
|
7
|
+
data.tar.gz: 373d61ba2f320ffbc8b5747759c7489ebf362c3f788faf9f9c9ba0759acca5c27834e3601ac2336f8a50c7007d183b6f3f620b6b27c9f58179edb1c566de4111
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -19,15 +19,24 @@ Or install it yourself as:
|
|
19
19
|
## Usage
|
20
20
|
|
21
21
|
require "bq"
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
#
|
26
|
-
|
27
|
-
|
22
|
+
|
23
|
+
# authorize and store access-token to file.
|
24
|
+
bq = Bq::InstalledApp.new(:token_storage=>".bq_secret_token.json")
|
25
|
+
bq.authorize # please locate client_secrets.json in load path.
|
26
|
+
|
27
|
+
...
|
28
|
+
|
29
|
+
# restore access-token from file.
|
30
|
+
bq2 = Bq::InstalledApp.new(:token_storage=>".bq_secret_token.json", :project_id=>"your-project-id")
|
31
|
+
|
32
|
+
# execute query
|
28
33
|
pp bq2.datasets.to_hash
|
29
34
|
pp bq2.query("SELECT 12345").to_hash
|
30
35
|
|
36
|
+
## Example
|
37
|
+
|
38
|
+
bq_project_id=your-project-id bundle exec ruby examples/bq_example.rb
|
39
|
+
|
31
40
|
## Contributing
|
32
41
|
|
33
42
|
1. Fork it ( https://github.com/takeru/bq/fork )
|
data/bq.gemspec
CHANGED
@@ -0,0 +1,50 @@
|
|
1
|
+
require "pp"
|
2
|
+
require "json"
|
3
|
+
require "bq"
|
4
|
+
require "terminal-table"
|
5
|
+
|
6
|
+
project_id = nil
|
7
|
+
project_id ||= ENV["bq_project_id"]
|
8
|
+
raise "please set your project_id" unless project_id
|
9
|
+
token_file = ".bq_secret_token.json"
|
10
|
+
|
11
|
+
bq = Bq::InstalledApp.new(:project_id=>project_id, :token_storage=>token_file)
|
12
|
+
unless bq.authorized?
|
13
|
+
unless bq.authorize
|
14
|
+
raise "failed to authorize."
|
15
|
+
end
|
16
|
+
end
|
17
|
+
q = DATA.read.strip
|
18
|
+
result = bq.query(q).to_hash
|
19
|
+
|
20
|
+
if result["error"] || result["rows"].nil?
|
21
|
+
pp result
|
22
|
+
exit
|
23
|
+
end
|
24
|
+
|
25
|
+
["kind","jobReference","totalRows","totalBytesProcessed","jobComplete","cacheHit"].each do |k|
|
26
|
+
puts "%20s : %s" % [k, result[k]]
|
27
|
+
end
|
28
|
+
|
29
|
+
# pp [result["rows"], result["schema"]]
|
30
|
+
rows = result["rows"]
|
31
|
+
fields = result["schema"]["fields"]
|
32
|
+
headings = [""] + fields.map{|f| f["name"] + "\n" + f["type"] + "\n" + f["mode"] }
|
33
|
+
table = Terminal::Table.new :headings=>headings do |t|
|
34
|
+
rows.each_with_index do |r,i|
|
35
|
+
t.add_row [i] + r['f'].map{|hash| hash['v'] }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
table.align_column(0, :right)
|
39
|
+
fields.each_with_index do |s,i|
|
40
|
+
if %w(INTEGER FLOAT).include?(s["type"])
|
41
|
+
table.align_column(i+1, :right)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
puts table
|
45
|
+
|
46
|
+
__END__
|
47
|
+
--- SELECT weight_pounds, state, year, gestation_weeks FROM publicdata:samples.natality ORDER BY weight_pounds DESC LIMIT 10;
|
48
|
+
--- SELECT word FROM publicdata:samples.shakespeare WHERE word="huzzah";
|
49
|
+
--- SELECT corpus FROM publicdata:samples.shakespeare GROUP BY corpus;
|
50
|
+
SELECT corpus, sum(word_count) AS wordcount FROM publicdata:samples.shakespeare GROUP BY corpus ORDER BY wordcount DESC;
|
data/lib/bq.rb
CHANGED
@@ -2,31 +2,50 @@ require "bq/version"
|
|
2
2
|
require 'google/api_client'
|
3
3
|
require 'google/api_client/client_secrets'
|
4
4
|
require 'google/api_client/auth/installed_app'
|
5
|
+
require 'google/api_client/auth/file_storage'
|
5
6
|
require 'json'
|
6
7
|
|
7
8
|
module Bq
|
8
9
|
class Base
|
9
|
-
attr_reader :token
|
10
10
|
attr_accessor :project_id
|
11
11
|
|
12
12
|
def initialize(opts={})
|
13
13
|
@project_id = opts[:project_id]
|
14
14
|
application_name = opts[:application_name] || "Bq"
|
15
|
-
application_version = opts[:application_version] ||
|
15
|
+
application_version = opts[:application_version] || Bq::VERSION
|
16
16
|
|
17
17
|
@client = Google::APIClient.new(
|
18
18
|
:application_name => application_name,
|
19
19
|
:application_version => application_version
|
20
20
|
)
|
21
|
-
|
22
|
-
|
21
|
+
|
22
|
+
self.token_storage = opts[:token_storage] if opts[:token_storage]
|
23
|
+
if @token_storage && @token_storage.authorization
|
24
|
+
@client.authorization = @token_storage.authorization
|
25
|
+
elsif opts[:token]
|
26
|
+
@client.authorization = opts[:token]
|
27
|
+
end
|
28
|
+
|
29
|
+
@bq_client = @client.discovered_api('bigquery', 'v2')
|
30
|
+
end
|
31
|
+
|
32
|
+
def token_storage=(storage)
|
33
|
+
if storage.kind_of?(String)
|
34
|
+
storage = Google::APIClient::FileStorage.new(storage)
|
35
|
+
end
|
36
|
+
if storage.respond_to?(:load_credentials) && storage.respond_to?(:write_credentials)
|
37
|
+
@token_storage = storage
|
38
|
+
else
|
39
|
+
raise "invalid storage"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def token
|
44
|
+
@client.authorization
|
23
45
|
end
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
@client.authorization.scope = token["scope"]
|
28
|
-
@client.authorization.refresh_token = token["refresh_token"]
|
29
|
-
@client.authorization.access_token = token["access_token"]
|
46
|
+
|
47
|
+
def authorized?
|
48
|
+
!@client.authorization.access_token.nil?
|
30
49
|
end
|
31
50
|
end
|
32
51
|
|
@@ -41,25 +60,17 @@ module Bq
|
|
41
60
|
:client_secret => credential.client_secret,
|
42
61
|
:scope => ['https://www.googleapis.com/auth/bigquery']
|
43
62
|
)
|
44
|
-
@client.authorization = flow.authorize
|
63
|
+
@client.authorization = flow.authorize(@token_storage)
|
45
64
|
# Here, will be opened authorization web page.
|
46
65
|
# Click [Authorize] button, and I see "Error: redirect_uri_mismatch".
|
47
66
|
# But it may be succeed, authorize arguments is available.
|
48
67
|
|
49
68
|
unless @client.authorization
|
50
69
|
puts "failed to authorize. Canceled?"
|
51
|
-
return
|
70
|
+
return false
|
52
71
|
end
|
53
72
|
|
54
|
-
|
55
|
-
"scope" => @client.authorization.scope,
|
56
|
-
"client_id" => @client.authorization.client_id,
|
57
|
-
"client_secret" => @client.authorization.client_secret,
|
58
|
-
"access_token" => @client.authorization.access_token,
|
59
|
-
"refresh_token" => @client.authorization.refresh_token
|
60
|
-
}.freeze
|
61
|
-
|
62
|
-
return @token
|
73
|
+
return true
|
63
74
|
end
|
64
75
|
|
65
76
|
def datasets
|
@@ -97,20 +108,16 @@ if __FILE__ == $0
|
|
97
108
|
"datasets", # list datasets
|
98
109
|
"query:" # execute query
|
99
110
|
)
|
111
|
+
bq = Bq::InstalledApp.new(:token_storage=>token_file)
|
100
112
|
if opts["authorize"]
|
101
113
|
puts "will be open authorization page in web browser..."
|
102
|
-
bq = Bq::InstalledApp.new
|
103
114
|
bq.authorize(opts["client_secrets_json"])
|
104
|
-
|
105
|
-
JSON.dump(bq.token,f)
|
106
|
-
end
|
107
|
-
puts "wrote access token to file: .bq_secret_token.json"
|
115
|
+
puts "wrote access token to file: #{token_file}"
|
108
116
|
end
|
109
117
|
|
110
118
|
project_id = opts["project_id"]
|
111
119
|
raise "project_id missing." unless project_id
|
112
|
-
|
113
|
-
bq = Bq::InstalledApp.new(:token=>token, :project_id=>project_id)
|
120
|
+
bq.project_id = project_id
|
114
121
|
|
115
122
|
if opts["datasets"]
|
116
123
|
pp bq.datasets.to_hash
|
data/lib/bq/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bq
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- sasaki takeru
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-06-
|
11
|
+
date: 2014-06-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: google-api-client
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: terminal-table
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
55
69
|
description: ''
|
56
70
|
email:
|
57
71
|
- sasaki.takeru@gmail.com
|
@@ -65,6 +79,7 @@ files:
|
|
65
79
|
- README.md
|
66
80
|
- Rakefile
|
67
81
|
- bq.gemspec
|
82
|
+
- examples/bq_example.rb
|
68
83
|
- lib/bq.rb
|
69
84
|
- lib/bq/version.rb
|
70
85
|
homepage: ''
|