rbbigquery 0.0.1

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: d3c6dccb9ac37ba7d802bb76407111aaa8950abe
4
+ data.tar.gz: 82bde743df55391a5fd3f73ec9ddab3ae5cf00ad
5
+ SHA512:
6
+ metadata.gz: 96674a56caf964edc94456d00668683e42b594738ef0a6773a9d8712d36e4b7273333adbe7fac1601e2757f62d7a67703a6e04056ee8fd4320cf1423d4e97fea
7
+ data.tar.gz: 64dfd128bc5a4824f823abad8293a550347f59f371e3aef170b2a500801ffe0537f91fbc8b55d0dea5350d685e5cc172a34a645124198d91b842daf6ae4e6685
@@ -0,0 +1,35 @@
1
+ module RbBigQuery
2
+ class Client
3
+ attr_accessor :client, :project_id, :bq
4
+
5
+ # @params opts [Hash] {:application_name, :application_version, :key_path, :service_email, :project_id}
6
+ def initialize(opts = {})
7
+ @client = Google::APIClient.new(
8
+ application_name: opts[:application_name],
9
+ application_version: opts[:application_version]
10
+ )
11
+
12
+ key = Google::APIClient::PKCS12.load_key(File.open(
13
+ opts[:key_path], mode: 'rb'),
14
+ "notasecret"
15
+ )
16
+
17
+ @asserter = Google::APIClient::JWTAsserter.new(
18
+ opts[:service_email],
19
+ "https://www.googleapis.com/auth/bigquery",
20
+ key
21
+ )
22
+
23
+ @client.authorization = @asserter.authorize
24
+
25
+ @bq = @client.discovered_api("bigquery", "v2")
26
+
27
+ @project_id = opts[:project_id]
28
+ end
29
+
30
+ # @return [RbBigQuery::Table]
31
+ def find_or_create_table(dataset, table_id, schema)
32
+ RbBigQuery::Table.new(self, dataset, table_id, schema)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,32 @@
1
+ module RbBigQuery
2
+ # Schema Builder
3
+ class Schema
4
+ attr_accessor :schema
5
+
6
+ class << self
7
+ # Builds schema for BigQuery
8
+ # @param &blk [Proc] RbBigQuery schema DSL
9
+ # @return [Array<Hash>]
10
+ def build(&blk)
11
+ instance = new
12
+ instance.schema = []
13
+ instance.instance_eval &blk
14
+ instance.schema
15
+ end
16
+ end
17
+
18
+ def string(name)
19
+ self.schema.push({
20
+ type: 'STRING',
21
+ name: name
22
+ })
23
+ end
24
+
25
+ def integer(name)
26
+ self.schema.push({
27
+ type: 'INTEGER',
28
+ name: name
29
+ })
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,60 @@
1
+ module RbBigQuery
2
+ class Table
3
+ attr_accessor :schema
4
+
5
+ def initialize(client, dataset, table_id, schema)
6
+ @client = client
7
+ @dataset = dataset
8
+ @table_id = table_id
9
+ @schema = schema
10
+
11
+ create
12
+ end
13
+
14
+ # @return row response json
15
+ def create
16
+ response = @client.client.execute({
17
+ :api_method => @client.bq.tables.insert,
18
+ :parameters => {
19
+ 'projectId' => @client.project_id,
20
+ 'datasetId' => @dataset
21
+ },
22
+ :body_object => {
23
+ 'tableReference' => {
24
+ 'projectId' => @client.project_id,
25
+ 'datasetId' => @dataset,
26
+ 'tableId' => @table_id
27
+ },
28
+ 'schema' => {
29
+ 'fields' => @schema
30
+ }
31
+ }
32
+ })
33
+
34
+ JSON.parse(response.body)
35
+ end
36
+
37
+ # insert rows
38
+ # @param rows [Array<Hash>] [{#{column_name}=>value}]
39
+ # @return row response json
40
+ def insert(rows)
41
+ rows = rows.map do |row|
42
+ {'json' => row}
43
+ end
44
+
45
+ response = @client.client.execute({
46
+ :api_method => @client.bq.tabledata.insert_all,
47
+ :parameters => {
48
+ 'projectId' => @client.project_id,
49
+ 'datasetId' => @dataset,
50
+ 'tableId' => @table_id,
51
+ },
52
+ :body_object => {
53
+ "rows" => rows
54
+ }
55
+ })
56
+
57
+ JSON.parse(response.body)
58
+ end
59
+ end
60
+ end
data/lib/rbbigquery.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'google/api_client'
2
+ require 'json'
3
+
4
+ require_relative 'rbbigquery/client'
5
+ require_relative 'rbbigquery/table'
6
+ require_relative 'rbbigquery/schema'
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rbbigquery
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kai Inui
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-03 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'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: "[WIP ]Provides a easier way to handle BigQuery with Ruby."
28
+ email: me@kaiinui.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/rbbigquery.rb
34
+ - lib/rbbigquery/client.rb
35
+ - lib/rbbigquery/schema.rb
36
+ - lib/rbbigquery/table.rb
37
+ homepage: https://github.com/kaiinui/rbbigquery
38
+ licenses:
39
+ - MIT
40
+ metadata: {}
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 2.3.0
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: "[WIP] Ruby BigQuery client."
61
+ test_files: []
62
+ has_rdoc: