rbbigquery 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d3c6dccb9ac37ba7d802bb76407111aaa8950abe
4
- data.tar.gz: 82bde743df55391a5fd3f73ec9ddab3ae5cf00ad
3
+ metadata.gz: 0a38bb0cb98e9729c1911cc9691c8caf95770be9
4
+ data.tar.gz: e6c8d395dce39e7815499f47a8b5aff5b9f11e7d
5
5
  SHA512:
6
- metadata.gz: 96674a56caf964edc94456d00668683e42b594738ef0a6773a9d8712d36e4b7273333adbe7fac1601e2757f62d7a67703a6e04056ee8fd4320cf1423d4e97fea
7
- data.tar.gz: 64dfd128bc5a4824f823abad8293a550347f59f371e3aef170b2a500801ffe0537f91fbc8b55d0dea5350d685e5cc172a34a645124198d91b842daf6ae4e6685
6
+ metadata.gz: 2ae2996daebbac4462844c0db8f6ba2748c9680d66829c62cc4282383e344b600621f2b1df2941797ebf62d1ebb101587b865f2821c373b49202d8ceceb7cb11
7
+ data.tar.gz: 4429b951a3792db9fff2f57396ef9cbcefedf4b69900e64e404844b2f27074ecd61488781ea35f2170492d065fe1dfd428826501c7acad7120afdf438f6b8df0
@@ -8,28 +8,91 @@ module RbBigQuery
8
8
  application_name: opts[:application_name],
9
9
  application_version: opts[:application_version]
10
10
  )
11
+ @bq = @client.discovered_api("bigquery", "v2")
12
+ @project_id = opts[:project_id]
13
+ authorize(opts[:service_email], opts[:key_path])
14
+ end
11
15
 
12
- key = Google::APIClient::PKCS12.load_key(File.open(
13
- opts[:key_path], mode: 'rb'),
14
- "notasecret"
15
- )
16
+ # @return [RbBigQuery::Table]
17
+ def find_or_create_table(dataset, table_id, schema)
18
+ RbBigQuery::Table.new(self, dataset, table_id, schema)
19
+ end
20
+
21
+ # Executes provided query.
22
+ # @param [String] query
23
+ # @return [String] row response string
24
+ def query(query)
25
+ response = @client.execute({
26
+ :api_method => @bq.jobs.query,
27
+ :parameters => {
28
+ 'projectId' => @project_id,
29
+ },
30
+ :body_object => {
31
+ 'query' => query
32
+ }
33
+ })
34
+
35
+ build_rows_from_response(response)
36
+ end
16
37
 
17
- @asserter = Google::APIClient::JWTAsserter.new(
18
- opts[:service_email],
38
+ private
39
+
40
+ def authorize(service_email, key_path)
41
+ key = Google::APIClient::PKCS12.load_key(File.open(key_path, mode: 'rb'), "notasecret")
42
+
43
+ asserter = Google::APIClient::JWTAsserter.new(
44
+ service_email,
19
45
  "https://www.googleapis.com/auth/bigquery",
20
46
  key
21
47
  )
22
48
 
23
- @client.authorization = @asserter.authorize
24
-
25
- @bq = @client.discovered_api("bigquery", "v2")
26
-
27
- @project_id = opts[:project_id]
49
+ @client.authorization = asserter.authorize
28
50
  end
29
51
 
30
- # @return [RbBigQuery::Table]
31
- def find_or_create_table(dataset, table_id, schema)
32
- RbBigQuery::Table.new(self, dataset, table_id, schema)
52
+ # Sample response
53
+ #
54
+ #{"kind"=>"bigquery#queryResponse",
55
+ #"schema"=>
56
+ # {"fields"=>
57
+ # [{"name"=>"screen_name", "type"=>"STRING", "mode"=>"NULLABLE"},
58
+ # {"name"=>"text", "type"=>"STRING", "mode"=>"NULLABLE"}]},
59
+ # "jobReference"=>
60
+ # {"projectId"=>"#{SOME_PROJECTID}", "jobId"=>"#{SOME_JOBID}"},
61
+ # "totalRows"=>"15",
62
+ # "rows"=>
63
+ # [{"f"=>[{"v"=>"huga"}, {"v"=>"text: 5"}]},
64
+ # {"f"=>[{"v"=>"huga"}, {"v"=>"text: 2"}]},
65
+ # {"f"=>[{"v"=>"huga"}, {"v"=>"text: 4"}]},
66
+ # {"f"=>[{"v"=>"huga"}, {"v"=>"text: 3"}]},
67
+ # {"f"=>[{"v"=>"huga"}, {"v"=>"text: 3"}]},
68
+ # {"f"=>[{"v"=>"huga"}, {"v"=>"text: 1"}]},
69
+ # {"f"=>[{"v"=>"huga"}, {"v"=>"text: 1"}]},
70
+ # {"f"=>[{"v"=>"huga"}, {"v"=>"text: 4"}]},
71
+ # {"f"=>[{"v"=>"huga"}, {"v"=>"text: 2"}]},
72
+ # {"f"=>[{"v"=>"huga"}, {"v"=>"text: 5"}]},
73
+ # {"f"=>[{"v"=>"huga"}, {"v"=>"text: 5"}]},
74
+ # {"f"=>[{"v"=>"huga"}, {"v"=>"text: 3"}]},
75
+ # {"f"=>[{"v"=>"huga"}, {"v"=>"text: 1"}]},
76
+ # {"f"=>[{"v"=>"huga"}, {"v"=>"text: 4"}]},
77
+ # {"f"=>[{"v"=>"huga"}, {"v"=>"text: 2"}]}],
78
+ # "totalBytesProcessed"=>"225",
79
+ # "jobComplete"=>true,
80
+ # "cacheHit"=>false
81
+ #}
82
+ # @return [Array<Hash>]
83
+ def build_rows_from_response(response)
84
+ return unless response
85
+ body = JSON.parse(response.body)
86
+ schema = body["schema"]["fields"]
87
+
88
+ body["rows"].map do |row|
89
+ row_hash = {}
90
+ row["f"].each_with_index do |field, index|
91
+ name = schema[index]["name"]
92
+ row_hash[name] = field["v"]
93
+ end
94
+ row_hash
95
+ end
33
96
  end
34
97
  end
35
98
  end
@@ -11,7 +11,12 @@ module RbBigQuery
11
11
  create
12
12
  end
13
13
 
14
- # @return row response json
14
+ # @return [String] GQL style table name. (dataset.table_id)
15
+ def sql_name
16
+ "#{@dataset}.#{@table_id}"
17
+ end
18
+
19
+ # @return [Hash] row response json
15
20
  def create
16
21
  response = @client.client.execute({
17
22
  :api_method => @client.bq.tables.insert,
@@ -36,11 +41,9 @@ module RbBigQuery
36
41
 
37
42
  # insert rows
38
43
  # @param rows [Array<Hash>] [{#{column_name}=>value}]
39
- # @return row response json
44
+ # @return [Hash] row response json
40
45
  def insert(rows)
41
- rows = rows.map do |row|
42
- {'json' => row}
43
- end
46
+ rows = rows.map { |row| {'json' => row} }
44
47
 
45
48
  response = @client.client.execute({
46
49
  :api_method => @client.bq.tabledata.insert_all,
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbbigquery
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kai Inui