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 +4 -4
- data/lib/rbbigquery/client.rb +77 -14
- data/lib/rbbigquery/table.rb +8 -5
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0a38bb0cb98e9729c1911cc9691c8caf95770be9
|
4
|
+
data.tar.gz: e6c8d395dce39e7815499f47a8b5aff5b9f11e7d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2ae2996daebbac4462844c0db8f6ba2748c9680d66829c62cc4282383e344b600621f2b1df2941797ebf62d1ebb101587b865f2821c373b49202d8ceceb7cb11
|
7
|
+
data.tar.gz: 4429b951a3792db9fff2f57396ef9cbcefedf4b69900e64e404844b2f27074ecd61488781ea35f2170492d065fe1dfd428826501c7acad7120afdf438f6b8df0
|
data/lib/rbbigquery/client.rb
CHANGED
@@ -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
|
-
|
13
|
-
|
14
|
-
|
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
|
-
|
18
|
-
|
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 =
|
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
|
-
#
|
31
|
-
|
32
|
-
|
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
|
data/lib/rbbigquery/table.rb
CHANGED
@@ -11,7 +11,12 @@ module RbBigQuery
|
|
11
11
|
create
|
12
12
|
end
|
13
13
|
|
14
|
-
# @return
|
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
|
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,
|