bq_fake_view 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b9b3cd29abd815321af3cb724be1768fc8baee68
4
+ data.tar.gz: 339d15edb49527ded87f26b60118dd445bf7ac99
5
+ SHA512:
6
+ metadata.gz: b8a5b2a50d5db698945b6af77d6cacab61d9c597d6ff8fa5ab7fff681973b4ef53fa96420fe3365458db3680b8e6496045f49a484aae4e004fe8e50ae5cbda56
7
+ data.tar.gz: ad9ef7d63bf2f7b6c7fdb4f310b455b9ef29c0ab8bda7706a511b053b3b7729afc71b585fb116a233fa5602eeb711883e741e523289a932df09a0eec5d3e4ba1
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bq_fake_view.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # BqFakeView
2
+ This gem create Static SQL View on Google Bigquery from Hash data.
3
+
4
+ It is main purpose to create fake data for testing.
5
+
6
+ Inspired by [BigQuery で無からリレーションを出現させる - Qiita](http://qiita.com/yancya/items/9af89b6f8d7975ef5892 "BigQuery で無からリレーションを出現させる - Qiita").
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'bq_fake_view'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install bq_fake_view
23
+
24
+ ## Usage
25
+
26
+ ```ruby
27
+ bq_fake_view = BqFakeView.new(Google::Auth.get_application_default(["https://www.googleapis.com/auth/bigquery"]))
28
+
29
+ data = [
30
+ {foo: 1, bar: "bar", time: Time.now},
31
+ {foo: 2, bar: nil, time: Time.now}
32
+ ]
33
+ schema = [
34
+ {name: "foo", type: "INTEGER"},
35
+ {name: "bar", type: "STRING"},
36
+ {name: "time", type: "TIMESTAMP"}
37
+ ]
38
+ bq_fake_view.create_view("your_project_id", "your_dataset_id", "test_data", data, schema)
39
+ # =>
40
+ # create "test_data" view.
41
+ # view query is
42
+ # SELECT * FROM (SELECT 1 as foo, "bar" as bar, TIMESTAMP('2016-1-8 10:02:01') as time), (SELECT 2 as foo, CAST(NULL as STRING) as bar, TIMESTAMP('2016-1-8 10:02:01') as time)
43
+
44
+ bq_fake_view.view_query(data, schema) # => return Query string used by View definition
45
+ ```
46
+
47
+ ## Development
48
+
49
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
50
+
51
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
52
+
53
+ ## Contributing
54
+
55
+ Bug reports and pull requests are welcome on GitHub at https://github.com/joker1007/bq_fake_view.
56
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "bq_fake_view"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'bq_fake_view/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bq_fake_view"
8
+ spec.version = BqFakeView::VERSION
9
+ spec.authors = ["joker1007"]
10
+ spec.email = ["kakyoin.hierophant@gmail.com"]
11
+
12
+ spec.summary = %q{This gem create Static SQL View on Google Bigquery from Hash data}
13
+ spec.description = %q{This gem create Static SQL View on Google Bigquery from Hash data}
14
+ spec.homepage = "https://github.com/joker1007/bq_fake_view"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "google-api-client", "~> 0.9.pre3"
22
+ spec.add_development_dependency "bundler", "~> 1.11"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ end
@@ -0,0 +1,63 @@
1
+ require "bq_fake_view/version"
2
+
3
+ require 'google/apis/bigquery_v2'
4
+
5
+ class BqFakeView
6
+ class CastError < StandardError; end
7
+ class FieldNotFound < StandardError; end
8
+
9
+ def initialize(auth)
10
+ @bigquery = Google::Apis::BigqueryV2::BigqueryService.new
11
+ @bigquery.authorization = auth
12
+ @bigquery
13
+ end
14
+
15
+ def create_view(project_id, dataset_id, name, rows, schema)
16
+ query = view_query(rows, schema)
17
+
18
+ Bigquery.client.insert_table(project_id, dataset_id, Google::Apis::BigqueryV2::Table.new({
19
+ table_reference: {
20
+ project_id: project_id,
21
+ dataset_id: dataset_id,
22
+ table_id: name,
23
+ },
24
+ view: {
25
+ query: query
26
+ },
27
+ }))
28
+ end
29
+
30
+ def view_query(rows, schema)
31
+ subqueries = rows.map do |r|
32
+ "(#{sql_from_hash(r, schema)})"
33
+ end
34
+ "SELECT * FROM #{subqueries.join(", ")}"
35
+ end
36
+
37
+ def sql_from_hash(row, schema)
38
+ cols = row.map do |k, v|
39
+ field = schema.find { |f| f[:name] == k }
40
+ raise FieldNotFound, "#{k} is not found from schema" unless field
41
+ "#{cast_to_sql_value(v, field[:type])} as #{k}"
42
+ end
43
+
44
+ "SELECT #{cols.join(", ")}"
45
+ end
46
+
47
+ private
48
+
49
+ def cast_to_sql_value(value, type)
50
+ case value
51
+ when String
52
+ %Q{"#{value.gsub(/"/, %q{\"})}"}
53
+ when Numeric
54
+ value.to_s
55
+ when Time, Date
56
+ "TIMESTAMP(\"#{value.to_s(:db)}\")"
57
+ when nil
58
+ "CAST(NULL AS #{type})"
59
+ else
60
+ raise CastError, "#{value} is unsupported type"
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,3 @@
1
+ class BqFakeView
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bq_fake_view
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - joker1007
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-01-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: google-api-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.9.pre3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.9.pre3
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: This gem create Static SQL View on Google Bigquery from Hash data
56
+ email:
57
+ - kakyoin.hierophant@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - README.md
65
+ - Rakefile
66
+ - bin/console
67
+ - bin/setup
68
+ - bq_fake_view.gemspec
69
+ - lib/bq_fake_view.rb
70
+ - lib/bq_fake_view/version.rb
71
+ homepage: https://github.com/joker1007/bq_fake_view
72
+ licenses: []
73
+ metadata: {}
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 2.5.1
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: This gem create Static SQL View on Google Bigquery from Hash data
94
+ test_files: []